Variables and Data Types

 

 

There are eight primitive data types in Java:

 

  • boolean
  • byte
  • short
  • int
  • long
  • float
  • double
  • char

H owever there are only seven kinds of literals, and one of those is not a primitive data type:

 

  • boolean: true or false
  • int: 89, -945, 37865
  • long: 89L, -945L, 5123567876L
  • float: 89.5f, -32.5f,
  • double: 89.5, -32.5, 87.6E45
  • char: 'c', '9', 't'
  • String: "This is a string literal"

There are no short or byte literals.

Strings are a reference or object type, not a primitive type. However the Java compiler has special support for strings so this sometimes appears not to be the case.

 

class Variables {

  public static void main (String args[]) {
  
    boolean b = true;
    int low = 1;
    long high = 76L;
    long middle = 74;
    float pi = 3.1415292f;
    double e = 2.71828;
    String s = "Hello World!";
    
  }
   
}

 

List | Previous | Next