Java Programming Tutorial 3 - Variables
Tutorial Name: Java Programming Tutorial 3 - Variables
Category: PC Tutorials
Submitted By: Jimbo
Related Forum: PC Building Forum
Jimbo's Java Class - Tutorial 3: Variables
Previous Tutorial: Tutorials/id=23700/java-programmi...-pt-2.html
Hello and welcome to my next Java tutorial. I'm professor Jimbo, an IT student at the University of Central Florida experienced in programming especially Java. In this tutorial I will further discuss the basics of Java programming.
In the previous tutorial I went over what the two lines of code we wrote in tutorial 1 meant. As well as continuing to discuss the basics of Java programming which included comments, keywords, and more.
Since you know the basics of Java it's time to use that knowledge and learn variables. During this tutorial I will discuss what a variable is, what variables are used in Java programming, and then at the end of tutorial you will do a fun exercise.
What is a Variable?
A variable is defined as a value that can change depending on conditions or on information passed to the program.
Types of Variables
Starting out with "int" which stands for integer. Integer data types can hold whole numbers such as 5, 10, 20, 50, 100 as well as negative values. The range of the value is -2,147,483,684 to 2,147,483,647.
Here's a basic program using an integer data type:
To explain what this does is simple. First you declare the data type, which in this case is int. Then give it a name and set it equal to a number between the minimum and maximum. For this example, I chose 5. Lastly, I want it to be printed out to the console. So I wrote out a fancy System.out.println(); and placed the variable inside of it.
What's great is the remaining three integer data types all work similar. This being byte, short, and long. The only difference are the range they can hold. Here's a chart to help you memorize which data type holds how many numbers.
The next two data types are float and double. Both being floating point values they represent numbers with a fractional part. Here's another chart to help:
As for the last two variables we have char and boolean. Both very simple to understand. The variable "char" stands for character and is used when you want to use character constants. For example if you'd want to store the letter 'a' then you would use the char variable. But, char is set up differently than number data types. Here's an example:
As you can see there is a small difference between the two. When declaring char you must wrap the letter with ' '. For example, 'A'.
Finally, we have boolean. Boolean data types are used to store a value that can be either true or false.
So now that you know what each variable is and what they do you'll make a fun program with it.
To show examples of most of them and how they print I declared 5 of them at the top then made them print to the console. Here is the output:
As you can see it added both numberOne and numberTwo as well as numberThree and numberFour. Then it printed out the rest of the variables.
But what is that code at the bottom? That you'll find out in the next tutorial of Jimbo's Java Class. The next tutorial you can expect to learn about if statements and their beauty.
Feel free to PM me here OR message me on the TTG discord.
- 01. Java Programming Tutorial 2 - The Basics Pt. 2(546)
- 02. Java Programming Tutorial 1 - The Basics(885)
- 03. SLADE3 Tutorial #5 - Bookmark(553)
- 04. Windows Basics Tutorial #2 - Navigating Windows(560)
- 05. Computer Basics Tutorial #11 - Getting to Know the OS(509)
- 06. Computer Basics Tutorial #8 - Understanding Applications(433)
- 07. Apache Ant Tutorial - Introduction(430)
- 08. Apache Ant Tutorial - Environment Setup(412)
- 09. Apache Ant Tutorial - Creating WAR files(315)
- 10. Apache Ant Tutorial - Building Projects(425)
- 11. Apache Ant Tutorial - Property Files(250)
- 12. Apache Ant Tutorial - Property Task(388)
- 13. Computer Basics Tutorial #7 - Understanding Operating System(624)
- 14. Computer Basics Tutorial #5 - Laptop Computers(489)
- 15. Computer Basics Tutorial #6 - Mobile Devices(396)