Join David Gassner for an in-depth discussion in this video Declaring and initializing numeric primitives, part of Java 8 Essential Training.
- You can represent numbers in Java as either primitive values or as complex objects. I'll show you how to represent them as primitives here and then show how to use helper classes to get information about numeric values. I'm working in the project PrimitiveNumbers. It has two classes named Main and MaxValues. Both of these classes have a Main method but no executable code. In the Main class, I'll add code to the Main method. And I'm going to declare six variables each using one of the primitive numeric data types.
I'll start with a byte that I'll name b and assign it a value of one. Then I'll create a short integer. I'll name that sh and I'll give that a value of one as well. Then an int, a 32 bit integer. I'll name that i and give it a value of one. And then a long integer with the type long. I'll assign that the name of l and give it a value of one as well. Notice all of these integer types are using a literal of one.
The compiler looks at the value of one and always starts with an assumption that it represents the smallest range and the smallest amount of memory, a byte. But then when you declare the type it upcasts the value. If you want to be explicit with the literal, that is if you want to say this is a one, but it's a long integer, you need to add an alpha character after the numeric. For a long, that would be the letter l. Technically, you could spell that with a lowercase l, but it's easy to confuse that with the number one.
So typically, developers use an uppercase L. You can do the same thing with floats and doubles. I'll declare a variable with the type of float. I'll name it f and I'll assign it a value of 1f. Notice I'm using a lowercase f here and you could use and uppercase F, but developers typically use lowercase for floats and doubles. And I'll assign a double value named d and I'll give it a value of 1d. Now I'll output those values to the console.
I'll use the S out code template and I'll output a value of Byte, and then I'll append to that the value b. Then I'll run that code. I'll go to the menu and choose Run and then Run again, and I'll choose the new Run configuration for the main class. And in the Run window at the bottom of the screen I see Byte: 1. Now I'm going to duplicate this line of code a few times. I'll copy the line of code to the clipboard then paste it in five times.
I'll change the label for each of these outputs to one of the data types, and then I'll change the values that are being output to match the labels. Now because I've created my Run configuration, I can run the code again just by clicking on the Run button on the toolbar and I see all of the values' output from Byte through Double. Notice for the four integer types I only see the number one. But for the float and double I see .0. Those have been cast as the appropriate types.
Now let's take a look at how to use helper classes. I'll go to the MaxValues class and I'll add code in its Main method. I'll declare a byte value named b, and this time I'll assign it a value of 127. Then I'll output that value with Byte: and I'll append to the variable. I'll run this code by once again going to the Run menu and choosing Run, and this time I'll create a new Run configuration for this class and I see the value byte 127.
Next I'll increment the value by one with the expression b++. The ++ operator means increment by one and then I'll copy and paste the line of code that outputs the value, and then I'll run the code. Notice what happens. 127 is the maximum value of a byte and if I try to add one more it wraps around. And now it has the minimum value for that data type.
To solve this I'll wrap this bit of code where I'm incrementing the value in a conditional block. I'll use an if statement. I'll type if and then press enter or return and then I'll say if (b < Byte.MAX_VALUE). Then I'll add a code block and I'll move this bit of code into the code block by cutting it and pasting it. Let's pick apart this line of code. The if statement is pretty obvious.
It's asking the question, if this condition is true. Then there's a Boolean expression wrapped in parentheses. The expression starts with the name of the variable, b, then the less than operator, then Byte.MAX_VALUE. Byte with an uppercase B is the helper class for byte values and it has a constant or a static field named MAX_VALUE in all uppercase that represents the largest value that this data type can have.
So I'm saying only increment the value if the value of b isn't already at the MAX_VALUE. Then I'll run the code again and this time the byte stays at 127 because this line of code is skipped. You can try this same sort of operation with all of the different primitive numeric data types, set a value, check to see whether it matches the MAX_VALUE, or is less than the MAX_VALUE, and if it isn't then try incrementing it.
Or try incrementing anyway and see what happens. If you exceed the maximum value with a primitive data type, you'll always wrap around to the minimum value and that'll be true for all of the primitive data types.
- 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