Skip to main content

Java Data Types

Java is a statically-typed language which means that all variables must first be declared before they can be used. In Java, every variable has a data type, which tells us the type of data the variable stores like integer, floating point, character, etc. This also puts restrictions on the operations that can be performed on the variables.

Let's get introduced to the different data types available in Java.

Primitive Data Types

Java supports eight primitive data types. They are:

  1. byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive).

  2. short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).

  3. int: The int data type is a 32-bit signed two's complement integer, which has a minimum value of -2^31 and a maximum value of 2^31-1.

  4. long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -2^63 and a maximum value of 2^63-1.

  5. float: The float data type is a single-precision 32-bit IEEE 754 floating point.

  6. double: The double data type is a double-precision 64-bit IEEE 754 floating point.

  7. boolean: The boolean data type has only two possible values: true and false.

  8. char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

byte b = 100;
short s = 10000;
int i = 100000;
long l = 15000L;
float f = 234.5f;
double d = 123.4;
boolean bool = true;
char c = 'a';

Non-Primitive Data Types

Non-primitive data types are created by the programmer and not defined by Java (except for String). Non-primitive types are also called reference types because they refer to objects.

  1. Strings: Strings in Java are not a primitive data type, but they are often treated similarly. They are actually objects of the String class, which has methods that can perform certain operations on strings.

  2. Arrays: An array in Java is a set of variables of the same type that are referenced by a common name.

  3. Classes and Objects: A class is a blueprint from which individual objects are created. Each object can have its own instance variables and methods.

  4. Interface: An interface in Java is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types.

String str = "Hello World";
int[] arr = {1, 2, 3, 4, 5};

By understanding these data types, you can declare and use variables in Java effectively. In the next section, we will learn more about operators and their usage in Java. Stay tuned!