Java Constants, Variables, and Datatypes

Java Constants

A constant is a value that cannot be changed.

Java Variables

A variable has these attributes: Example variable declarations:
int maxAge;
int x, y, selectedIndex;
char a, b;
boolean flag;
double maxVal, massInKilos;

Variable initialization in declaration:
int timeInSeconds = 245;
char a = 'K', b = '$';
boolean flag = true;
double maxVal = 35.875;

Java Primitive Datatypes

Datatype Meaning Size in Bytes Min Value Max Value Sig. Digits
int Integer 4 -2 billion 2 billion  
double Floating Point 8 -1.8x10308 1.8x10308 15
char Character 2 Any Unicode character  
boolean Logical 1 false true  

String Objects

A string in Java is not a primitive data type. It is an object from the system defined class String.

  • Examples of string constants:
    "watermelon", "fig", "$%&*^%!!", "354", " " (space), "" (null string)

    The string declaration
    String item = "apple";
    is short for the object declaration
    String item = new String("apple");