Java Constants, Variables, and Datatypes

Java Constants

A constant is a value that cannot be changed.

Java Variables

A variable has these attributes:

Java Primitive Datatypes

Datatype Meaning Size in Bytes Min Value Max Value Sig. Digits
byte Integer 1 0 255  
short Integer 2 -32,768 32,767  
int Integer 4 -2 billion 2 billion  
long Integer 8 -9.2x1018 9.2x1018  
float Floating Point 4 -3.4x1038 3.4x1038 7
double Floating Point 8 -1.8x10308 1.8x10308 15
char Character 2 Any Unicode character  
boolean boolean 1 false true  

Long and Float Constants

The default datatype for an integer constant is int.
A long constant uses an L suffix: 345L.

The default datatype for a floating point constant is double.
A float constant uses an F suffix: 3.14159F.

Use a narrowing cast to convert a numeric constant to byte or short.

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", "$%&*^%!!", "354", " " (space), "" (null string)

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