Data Types

The Java programming language is statically-typed, which means that all variables must first be declared before they can be used.

The new keyword isn’t used when initializing a variable of a primitive type. Primitive types are special data types built into the language; they are not objects created from a class.

The Java programming language supports the following data types:

  Type Size Range Default
1 byte signed 8-bit -128 -> 127 0
2 short signed 16-bit -32,768 -> 32,767 0
3 int signed 32-bit, unsigned 32-bit -231 -> 231-1, 0 -> 232-1 0
4 long signed 64-bit, unsigned 64-bit -263 -> 263-1, 0 -> 264-1 0L
5 float 32-bit IEEE 754 floating point ±1.4E-45 -> ±3.4028235E+38 0.0f
6 double 64-bit IEEE 754 floating point ±4.9E-324 -> ±1.7976931348623157E+308 0.0d
7 boolean 1 bit NA false
8 char 16-bit \u0000 -> \uFFFF ‘\u0000’

In addition to the eight primitive data types listed above, the Java programming language also provides special support for character strings via the java.lang.String class. Enclosing your character string within double quotes will automatically create a new String object;

String objects are immutable, which means that once created, their values cannot be changed. The String class is not technically a primitive data type. Default value of String is null. But considering the special support given to it by the language, you’ll probably tend to think of it as such. You’ll learn more about the String class in Simple Data Objects.

For Local variables, the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.