From the course: Java 8+ Essential Training: Syntax and Structure

Declare and modify primitive values - Java Tutorial

From the course: Java 8+ Essential Training: Syntax and Structure

Start my 1-month free trial

Declare and modify primitive values

- [Instructor] In Java, you can represent numbers as either primitive values or as complex objects. In this exercise, I'm going to work only with primitives, but I'm also going to use some of the numeric type, wrapper classes, to show you some other information you can get from them. To declare a primitive variable, always start with the type. I'll declare a type of byte, all lowercase. I'll give the variable a name of b, and I'll assign it a value of 1, and in jshell, that results in executing the operation and showing you the result; b is now 1. I'll do the same thing with a short value. I'll make a variable named sh, and give it a value of 1, and then I'll do the same thing with an int. Now these values might all look the same, but internally their taking differing amounts of memory. The byte value is taking a small amount, the short, a slightly larger amount, and the int, a larger amount than that, and you can also declare a long integer. Now each of these types has a maximum and a minimum value, known as the type's range. To find out what the maximum value is, in jshell, you can simply type the name of the wrapper class. So I'll start with the wrapper class Byte.MAX_VALUE, and I'm told that the maximum value of this type is 127. So let's see what happens if you try to exceed that value. I'll reassign the value of b to 126. Notice I'm not re-declaring the type. I don't have to do that because the type of the variable has already been declared. In jshell, if you add in the type again, it'll be fine, the variable would be completely rebuilt, but within a Java class if you do that, it would be a syntax error. In a real java class, you can only declare the type of a variable once. Okay, so, I'm setting the value to 126, one less than the maximum. Now, I'll increment it by one. I'll use the expression ++ b, ++ means add one, and I'm putting it before the variable to mean, execute this operation first, than show me the result, and I see that b is now equal to 127, but watch what happens if I try to increment it again. Now, instead of going up to 128, which would exceed the maximum value, the value of the variable wraps around to the minimum. It's now -128. You can see exactly the same thing with a short value. I'll type Short.MAX_VALUE. I see it's 32767. I'll set the value of sh to one less than that. I'll increment it. I see that I'm now at the maximum value, and then I'll increment it again, and it wraps around to the minimum value, and you'll see the same behavior on all of the primitive variables. So those are the integer types, and you can use the same sort of syntax on the floating types. I'll create a variable of type float and name it f, and give it a value of 1f, and that means, that the literal value is already a floating value. I'll do the same thing with a double, and so that's how you declare the primitive types. Finally, I'll show how to compare a value to a types maximum value, and then only take a particular action if the value doesn't match. I'll use an if clause, the keyword if is always followed by a pair of parenthesis wrapped around a Boolean condition. My condition will be b < Byte.MAX_VALUE, and if that condition is true, than I'll say ++b. Then, in jshell, I can simply type the name of the variable b, and see what its value is. So now, I'll reset the value to 126, one less than the maximum, I'll use my up cursor key to navigate back to that line that incremented the value, but only if the condition is true. I'll execute that line of code, and it's now the maximum value. I'll once again execute the conditional code, and then evaluate the variable again, and this time I see that it doesn't change. So that's the basics of working with primitive values. You have four int types of varying ranges and varying memory usages, and two floating types, float and double, and all of these types, have equivalent wrapper classes that you can use to get information and to convert values in various ways.

Contents