Tutorials Navigation

Tutorials: 15,007 Categories: 40
Total Tutorial Views: 28,897,287

Java Programming Tutorial 3 - Variables

Tutorial Name: Java Programming Tutorial 3 - Variables  

Category: Programming Tutorials

Submitted By: Jimbo

Date Added:

Comments: 0

Views: 236

Share:





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.

Summary

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.

Getting Started

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.

Variables

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
int
short
long
float
double
char
boolean

Examples

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:

[ Register or Signin to view external links. ]

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.

[ Register or Signin to view external links. ]

source: [ Register or Signin to view external links. ]

Moving on..

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:

[ Register or Signin to view external links. ]

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:

[ Register or Signin to view external links. ]

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.

Final Example

So now that you know what each variable is and what they do you'll make a fun program with it.

[ Register or Signin to view external links. ]

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:

[ Register or Signin to view external links. ]

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.

Conclusion

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.

Questions?

Feel free to PM me here OR message me on the TTG discord.







Ratings

Current rating: 9.66 by 3 users
Please take one second and rate this tutorial...

Not a Chance
1
2
3
4
5
6
7
8
9
10
Absolutely

Comments

"Java Programming Tutorial 3 - Variables" :: Login/Create an Account :: 0 comments

If you would like to post a comment please signin to your account or register for an account.