Join David Gassner for an in-depth discussion in this video Working with primitive variables, part of Java 8 Essential Training.
- As with nearly all programming languages, Java supports the use of variables to store data in memory. There are two major classes of variables or data types in Java, known as primitives and objects. Primitive variables or data types are used to store numbers, individual characters and boolean values. They're stored in the fastest available memory, so you can get to the data as quickly as possible. Data type names for primitives are all in lowercase and that's how you can distinguish them from complex object data types, which always have an initial uppercase character.
The next part of the declaration is the variable identifier, or name. Java naming conventions require that variable names or identifiers always start with an initial lowercase character. And then you can use camel case, using uppercase characters in the middle of the name to distinguish certain names. In fact, certain IDEs, including IntelliJ IDEA, know how to read camel case. They do spell checking to make sure you don't type something very badly, but they know how to read variables as individual names based on where you placed the uppercase characters.
Finally, the value is placed on the right side of the assignment operator, the equals operator. The first two parts of this declaration are required. You could simply say int myVar and finish the statement with a semicolon, and that would be establishing the variable but not explicitly assigning its initial value. If you want to assign the initial value, you just add the equals operator and the initial literal value you're assigning. Here are the primitive data types for numbers starting with the smallest amount of memory and going to the largest amount of memory.
All primitive values are signed, meaning that their ranges extend from negative to positive numbers. The byte data type stores eight bits of memory. Its minimum value is -128 and its maximum is positive 127. You also have short, int and long integers. Each of them takes an increasing amount of memory and has an increasing range. The most common of these data types that you'll see is the int.
It's a 32-bit value and its range goes from -2 billion to positive 2 billion. If you need to store very large integers, you can use the long data type instead. There are two data types that you can use to store fractional values: float and double. Float is a 32-bit value and double is a 64-bit value. Once again, they both support minimum and maximum values in the negative and positive ranges, and you can look at the Java docs to see the exact values.
Double values tend to be used most commonly. They give you the largest range and the highest level of precision. Each primitive has something called a helper class that's a part of the Java runtime library. Each of these classes can be used for converting values from one primitive data type to another and to format values using very simple logic. Here are the helper classes. The primitive byte value can be helped by the Byte class.
Notice that the name of the class always starts with an initial uppercase character and that's how you can distinguish the class name from the primitive data type name. The helper class for the short is Short, and the helper class for int is Integer. And here are the others, Long for long and Float for float. In fact, for most of the primitive data types, their helper classes are exactly the same name but with that initial uppercase character.
And where there is a difference is in the integer, where the primitive is int and the helper class is Integer. Here's an example of how you might use a helper class. Each of the helper classes is a member of a package called java.lang. Just like your own code, Java classes in the runtime library are organized in packages. All classes that are members of the java.lang package are available to you always without you having to add any special declarations.
The Double class supports primitive double values. Here's an example of declaring a double value. The first part is double, the data type. The second is the variable identifier or name, doubleValue, and the third part is the literal value that I'm assigning it. Notice the letter d at the end. This is saying that this value, which could be interpreted by the compiler as either a float or a double is explicitly set as a double.
You'll see the use of these alpha characters on all numeric literals where the compiler can't figure it out on its own. So now I have a value called doubleValue and I want to convert it to another value, so I'm declaring a byte named byteValue and then I'm using a method of the double helper class called byteValue wrapped around the doubleValue variable and it converts it to a byte. And if necessary, it truncates any fractional values.
Similarly, the Double class has an intValue method, a floatValue and a toString method. toString is a special case. All Java classes have a method named toString which is in charge of converting that object, whether it's a numeric or boolean or a more complex object to some sort of string. The toString method of the Double helper class converts the double to a simple string value.
All numeric primitives have default values and they always default to a value of zero. Here's some code where I'm declaring a variable named myInt. Data type is an integer or a 32-bit value. I don't have an assignment at the end of the declaration. And then I output that value as a part of a string, starting off with a string literal of The value of myInt is, and then appending or concatenating the int value.
Because it defaults to zero, the output will look like this: The value of myInt is 0. And that's going to be true of all numeric primitives starting from byte, going all the way through double. So that's a brief look at how to declare variables with primitive data types, and a little bit about primitives and their complex helper classes. In the next video, I'll show you how to declare some primitive variables and output their values to the console in IntelliJ IDEA.
- Understanding the history and principles of Java
- Installing Java, IntelliJ IDEA, and BlueJ
- Creating a Java project
- Working with variables, values, and expressions
- Working with object data types
- Building, comparing, and parsing strings
- Debugging and exception handling
- Creating loops and reusable code
- Passing arguments by reference or value
- Using simple and complex arrays
- Creating custom classes
- Understanding inheritance and polymorphism
- Managing files with Java libraries
- Documenting code with Javadoc
- Packaging classes in JAR files