OCA Java SE 8 Building Blocks - Data Types








Object References and Primitives

Java applications contain two types of data: primitive types and reference types.

Primitive Types

Java has eight built-in data types, referred to as the Java primitive types.

These eight data types represent the building blocks for Java objects.

The following table shows the Java primitive types together with their size in bytes and the range of values that each holds.

KeywordTypeExample
booleantrue or falsetrue
byte8-bit integral value123
short16-bit integral value123
int32-bit integral value123
long64-bit integral value123
float32-bit floating-point value123.45f
double64-bit floating-point value123.456
char16-bit Unicode value'a'

byte, short, int, and long are used for numbers without decimal points.

float and double are used for floating-point (decimal) values.

A float requires the letter f following the number to mark it as float.

Each numeric type uses twice as many bits as the smaller similar type.

A byte can hold a value from -128 to 127.

The maximum number an int can hold is 2,147,483,647.

public class Main { 
  public static void main(String[] args) { 
    System.out.println(Integer.MAX_VALUE); 
  }
}

When a number is present in the code, it is called a literal.

By default, Java assumes you are defining an int value with a literal for integer.

In the following code, the number listed is out of the range of an int.

long max = 99123456789;  // DOES NOT COMPILE 

Java complains the number is out of range.

The solution is to add the character L to the number:

long max = 99123456789L;  // now Java knows it is a long 

Alternatively, you could add a lowercase l to the number.

The uppercase L is preferred since the lowercase l looks like the number 1.

The code above generates the following result.





Base

In Java we can specify numbers by the "base".

We can create octal number (digits 0-7), which uses the number 0 as a prefix-for example, 017.

We can create hexadecimal numbers (digits 0-9 and letters A-F), which uses the number 0 followed by x or X as a prefix-for example, 0xFF.

We can also create binary numbers (digits 0-1), which uses the number 0 followed by b or B as a prefix-for example, 0b10.

public class Main{
   public static void main(String[] argv){
        System.out.println(56);       // 56 
        /*from   w  w  w . j av  a2s.  co  m*/
        System.out.println(0b11);     // 3 
        
        System.out.println(017);      // 15 
        
        System.out.println(0x1F);     // 31 
   }
}

Starting from Java we can use underscores in numbers to make them easier to read:

int million1 = 1000000; 
int million2 = 1_000_000; 

You can add underscores anywhere except at the beginning of a literal, the end of a literal, right before a decimal point, or right after a decimal point.

The code above generates the following result.





Reference Types

A reference type refers to an object.

The primitive types hold their values in the memory where the variable is allocated.

The reference types points to an object by storing the memory address where the object is located.

A reference type is like a pointer in C or C++.

The following code declares a reference of type java.util.Date and a reference of type String:

java.util.Date today; 
String greeting; 

The today variable is a reference of type Date and can only point to a Date object.

The greeting variable is a reference that can only point to a String object.

A value is assigned to a reference in one of two ways:

  • A reference can be assigned to another object of the same type.
  • A reference can be assigned to a new object using the new keyword.

The following statements assign these references to new objects:

today = new java.util.Date(); 
greeting = "How are you?"; 

today now points to a new Date object in memory, and we can use it to access the various fields and methods of this Date object.

greeting points to a new String object, "How are you?".

Differences between primitives and reference types

The reference types can be assigned to null, which is a Java reserved word and means they do not currently refer to an object.

We cannot assign null to primitive types.

We can use reference types to call methods when they do not point to null.

Primitives do not have methods declared on them.

public class Main{
   public static void main(String[] argv){
        String reference = "hello"; 
        int len = reference.length(); 
   }
}

All the primitive types have lowercase type names.

All classes that come with Java begin with uppercase.