Java Programming Tutorial 3 - Variables
Tutorial Name: Java Programming Tutorial 3 - Variables
Submitted By: Jimbo
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(319)
- 02. Java Programming Tutorial 1 - The Basics(575)
- 03. A Simple HTML Document(269)
- 04. [HTML] HTML Basics(294)
- 05. C# Allow only 1 instance of a form to be opened(1,913)
- 06. [VB.NET] How to make and save settings(1,420)
- 07. VB.NET] Open Site when Tool opens(619)
- 08. [VB.NET] Opening external programs / Links(692)
- 09. [VB.NET] Message Box(724)
- 10. [ VB.NET ] How to use progress bars(1,188)
- 11. [VB.NET] How to extend your application size(939)
- 12. [VB.NET] How to make flashing text(2,250)
- 13. [VB.NET] How to use a progress bar(1,195)
- 14. [VB.NET] How to Make a Splash Screen(2,025)
- 15. [VB.NET] How to Make a HTML Editor(1,059)