2. Working With Java data types
2.1. Primitive Variables
Boolean variables can only store one of two values: true or false.
boolean answer = false;
In this case, the variable answer is assigned a fixed value with a literal (false). A literal is a fixed value that doesn’t need calculations to be assigned to any variable. true and false are the boolean literals.
When the value can be counted in whole numbers, it is an integer. They can be stored in different data types:
The difference between those types is the range of values they can support:
- byte –128 to 127, inclusive
- short –32,768 to 32,767, inclusive
- int –2,147,483,648 to 2,147,483,647, inclusive
- long –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807, inclusive
For designation of an integer literal value as a long value, the suffix L or l must be added to the literal:
long bigNumber = 584677658897L;
Integer variables can be defined as binary, decimal, octal or hexadecimal numbers. In order to specify the number system, a prefix must be used:
- decimal use no prefix: int value = 267;
- octal use the prefix 0: int octVal = 0413;
- hexadecimal use the prefix 0x or 0X: int hexVal = 0x10B;
- binary use the prefix 0b or 0B : int binVal = 0b100001011;
Java version 7 allows the use of underscores to help group individual digits or letters of literal values:
long baseDecimal = 100_267_760; long octVal = 04_13; long hexVal = 0x10_BA_75; long binVal = 0b1_0000_10_11;
Underscores cannot be placed at the start or the end of a literal value, cannot be placed right after prefixes for binary and hexadecimal values (0b, 0B, 0x and 0X) but it can be placed right after the prefix for octal values (0). Underscores cannot be placed prior to a long suffix (l or L), finally, they cannot be placed in positions where a string of digits is expected
int i = Integer.parseInt(“3_14”); //invalid use of underscore
126.96.36.199. Float and Double
When expecting decimal numbers in Java float and double primitive data types can be used. Float has smaller range than double and requires less space. To initialize a decimal literal value as a float value, the suffix F or f must be used because the default type of a decimal literal in java is double.
float average = 17.925F;
After Java version 7, underscores can be used with floating-point literal values following these rules:
- You can’t place an underscore prior to a D, d, F, or f suffix (these suffixes are used to mark a floating-point literal as double or float).
- You can’t place an underscore adjacent to a decimal point.
A char can store a single character from all the existing scripts and languages. To assign a value to a char variable, single quotes must be used instead of double quotes, otherwise the code will fail to compile.
char v1 = ‘v’;
Java stores char data as a positive integer, therefore it is acceptable to assign an integer to a char.
char a1 = 97; // a assigned to a1
Casting is used to forcefully convert data to another data type and must be used between compatible data types, when casting a bigger value to a smaller data type the compiler chops the extra bits that does not fit into the smaller variable. Negative integer numbers can be casted into a char as follows:
char a2 = (char) -97; //successfully compiled
Identifiers are names of packages, classes, interfaces, methods, and variables.
2.2.1. Valid and Invalid Identifiers
188.8.131.52. Java reserved words and keywords:
2.3. Object Reference Variables
An Object Reference is a memory address that points to a memory area where an object’s data is located.
Class1 refVal = new Class1(); //refVal is the name of the object reference variable
In the previous line, a reference variable refVal of type Class1 is created and an object is assigned to it. The literal value of reference variables is null, it can also be assigned explicitly:
Class1 refVal = null;
2.3.1. Object reference variables and primitive variables
The main difference between these variables is that primitive variables store the values and reference variables store the memory address of the object they refer to.
2.4.1. Assignment Operators
The most frequently used operator is the = and it is used to initialize variables with values and to reassign new values to them. The +=, -=, *=, and /=operators are short forms of addition, subtraction, multiplication and division with assignment and work as follows: a -= b is equal to a = a – b a += b is equal to a = a + b a *= b is equal to a = a * b a /= b is equal to a = a / b Multiple values can be assigned in the same line of code as follows:
int a = 7, b = 10, c = 8; a = b = c; // here a = 8 because the assignment starts from right to left.
2.4.2. Arithmetic Operators
184.108.40.206. ++ And – (Unary increment and decrement operators)
Used to increment or decrement the value of a single variable by 1. Can be used in prefix or postfix notation:
int a = 10; ++a; // a = 11 int b = 10; b++; //b = 11
When a unary operator is used in a expression, its placement with respect to its operand decides whether its value will increment or decrement before the evaluation of the expression or after the evaluation of the expression.
int a = 20; int b = 10; int c = a - ++b; System.out.println(c); // c = 9 System.out.println(b); // b = 11 int a = 20; int b = 10; int c = a - b++; System.out.println(c); // c = 10 System.out.println(b); // b = 11 int a = 10; a = a++ + a + a-- - a-- + ++a; System.out.println(a); //a = 32(a = 10 + 11 + 11 - 10 + 10)evaluates from left to right
2.4.3. Relational Operators
Relational operators are used to check a condition. The result of the relational operator is always a Boolean value. Usually subdivided in two categories:
- Comparing greater (>, >=) and lesser values (<, <=)
- Comparing values for equality (==) and nonequality (!=)
The operators (>, >=) and (<, <=) work with all types of numbers, char and floating point variables that can be added and subtracted.
int a = 10; int b = 15; System.out.println(a >= b); //prints false
The operators (==) and (!=) can be used to compare all types of primitives: char, byte, short, int, long, float, double, and boolean. != returns true if the values compared are not equal and false otherwise. For the same set of values if == returns true, != will return false. These operators cannot be used to compare incomparable types, for example comparing an int with a boolean will fail to compile.
2.4.4. Logical Operators
These Operators are used to evaluate one or more expressions and should return a boolean value.
- And (&&) operator in Java returns true when both conditions are true.
- Or (||) operator in Java returns true when at least one condition is true.
- Not (!) operator reverses the outcome of a Boolean value.
int x = 1; int y = 5; System.out.println(x == y); //prints false System.out.println(x != y); //prints true boolean b1 = false; System.out.println(b1 == true); //prints false System.out.println(b1 != true); //prints true System.out.println(b1 == false); //prints true System.out.println(b1 != false); //prints false