- Joined
- Jan 21, 2007
- Sythe Gold
- 1,234
- Messages
- 14,211
Java Basics [3] - Primitive Variables
By SuF
By SuF
If you have questions or comments feel free to post! If you have a specific issue or want some help with anything Java related, PM me. If you are browsing as a guest, feel free to register so you can post or PM me.
Before reading this guide, make sure you can do the following things:
A primitive variable is basic building block provided by Java that holds specific data. Java is a statically typed (also know as strongly typed) language which simply means that before you use a variable, you must tell Java what type of data that the variable will hold. You are not allowed to put a character in an int because int only holds integers. There are eight different primitive variable types in Java:
In general, you will be using int for most integer values. Long is used only if the values will be quite large and exceed the maximum int can hold. Byte and short are rarely used because they can only hold small values. They use less memory than int but today that really is not a concern. Always use double and not float or your decimals may be very incorrect due to float's low precision. char holds a single character that you must surround with single quotes like 'f'. boolean can only hold two values "true" or "false" (without the quotes).
There is one more data type that Java "pretends" is a primitive because it is very convenient to do so, which is String (note the capitalization). A String is simply a sequence of characters like "This is a string". Be careful when working with Strings because they seem like primitives but actually are not.
In order to use a variable, you must first declare it by specifying a type and an identifier like:
In this example I am declaring an integer variable (with int) called "myFirstVariable". Note the capitalization that I used: This is called camel casing and is the standard convention for naming variables in Java. Remember that Java is case sensitive so "myfirstvariable" is not the same as "myFirstVariable".
There are some rules for what an identifier can be. It must be at least one character long and can contain uppercase letters, lowercase letters, underscores, dollar signs, and digits. However, it may not begin with a digit. The following are valid identifiers:
The following are invalid identifiers:
Back to the previous example, "myFirstVariable" does not hold any data. It has been declared, meaning that it exists but it has not be initialized, meaning it does not yet hold any data. Initializing a variable simply means giving it an initial value like so:
This code snippet assumes that you have already declared "myFirstVariable" as we did above. This is an example of what we call assignment. The value on the right is assigned, to the variable on the left. Another way of saying this is that the variable on the left is given the value on the right. It is also possible to declare and initialize a variable in one statement like:
It is also possible to declare multiple variables of the same type in one statement like:
Additionally, you can declare multiple variables in one statement and initialize the last one:
Counterintuitively, in this example, "first" and "second" are not initialized and do not yet have a value. Only "third" has been initialized and has a value of true.
After we have declared and initialized a variable, we can use it in other statements wherever we would have used a hard coded value of that type like:
In this case, we declare and initialize a long called "a" to 5. We then assign "b" the value that is in "a", which is 5. So in this case, "a" and "b" are set to the same value. To show that this is true, we can print the values in each of these variables to the console like:
If you put these two code snippets into a Java program, 5 would be printed to the console twice like:
System.out.println() is the same method that was used to print a String out in the previous guide. This method is extremely versatile and can be used to print out the value of any type like:
which will output:
Note how String behaves just like the primitives. At this point it will but later there will be differences that can easily catch you out. For now pretend it is a primitive but know that it is actually not.
- Java Development Kit (JDK) installed and configured (guide upcoming)
- Compile and run a Java program (guide upcoming)
- Create a basic Java Program (Java Basics [2] - Your First Program)
A primitive variable is basic building block provided by Java that holds specific data. Java is a statically typed (also know as strongly typed) language which simply means that before you use a variable, you must tell Java what type of data that the variable will hold. You are not allowed to put a character in an int because int only holds integers. There are eight different primitive variable types in Java:
- int - integers between -2,147,483,648 and 2,147,483,647
- long - integers between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807
- float - floating point numbers (like 2.3) with minimal precision
- double - floating point numbers (like 2.3) with much more precision (use this instead of float)
- char - a single character, like 't'
- boolean - true or false
- byte - integers between -128 and 127
- short - integers between -32,768 and 32,767
In general, you will be using int for most integer values. Long is used only if the values will be quite large and exceed the maximum int can hold. Byte and short are rarely used because they can only hold small values. They use less memory than int but today that really is not a concern. Always use double and not float or your decimals may be very incorrect due to float's low precision. char holds a single character that you must surround with single quotes like 'f'. boolean can only hold two values "true" or "false" (without the quotes).
There is one more data type that Java "pretends" is a primitive because it is very convenient to do so, which is String (note the capitalization). A String is simply a sequence of characters like "This is a string". Be careful when working with Strings because they seem like primitives but actually are not.
In order to use a variable, you must first declare it by specifying a type and an identifier like:
Code:
...
int myFirstVariable;
...
In this example I am declaring an integer variable (with int) called "myFirstVariable". Note the capitalization that I used: This is called camel casing and is the standard convention for naming variables in Java. Remember that Java is case sensitive so "myfirstvariable" is not the same as "myFirstVariable".
There are some rules for what an identifier can be. It must be at least one character long and can contain uppercase letters, lowercase letters, underscores, dollar signs, and digits. However, it may not begin with a digit. The following are valid identifiers:
Code:
a
man56
$56
_myVariableThatIsReallyQuiteLongAndYouShouldNotReallyDoThisBecauseTypingTakesForever
The following are invalid identifiers:
Code:
45L
my!
(.)(.)
&myVar
3var
Back to the previous example, "myFirstVariable" does not hold any data. It has been declared, meaning that it exists but it has not be initialized, meaning it does not yet hold any data. Initializing a variable simply means giving it an initial value like so:
Code:
...
myFirstVariable = 5;
...
This code snippet assumes that you have already declared "myFirstVariable" as we did above. This is an example of what we call assignment. The value on the right is assigned, to the variable on the left. Another way of saying this is that the variable on the left is given the value on the right. It is also possible to declare and initialize a variable in one statement like:
Code:
...
int myNewVar = 10;
...
It is also possible to declare multiple variables of the same type in one statement like:
Code:
...
double var1, var2, var3;
...
Additionally, you can declare multiple variables in one statement and initialize the last one:
Code:
...
boolean first, second, third = true;
...
Counterintuitively, in this example, "first" and "second" are not initialized and do not yet have a value. Only "third" has been initialized and has a value of true.
After we have declared and initialized a variable, we can use it in other statements wherever we would have used a hard coded value of that type like:
Code:
...
long a = 5;
long b = a;
...
In this case, we declare and initialize a long called "a" to 5. We then assign "b" the value that is in "a", which is 5. So in this case, "a" and "b" are set to the same value. To show that this is true, we can print the values in each of these variables to the console like:
Code:
...
System.out.println(a);
System.out.println(b);
...
If you put these two code snippets into a Java program, 5 would be printed to the console twice like:
Code:
5
5
System.out.println() is the same method that was used to print a String out in the previous guide. This method is extremely versatile and can be used to print out the value of any type like:
Code:
long c = 10;
double d = 6.4;
boolean e = true;
String f = "Hello";
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
which will output:
Code:
5
5
10
6.4
true
Hello
Note how String behaves just like the primitives. At this point it will but later there will be differences that can easily catch you out. For now pretend it is a primitive but know that it is actually not.



















