Data Types

Java is a strongly-typed language, meaning that every variable must be declared with a specific data type. Data types in Java can be categorized into two groups: Primitive Data Types and Reference Data Types.

 

Primitive Data Types

There are eight Primitive Data Types in Java:

  • byte: 8-bit signed integer. Range: -128 to 127.
  • short: 16-bit signed integer. Range: -32,768 to 32,767.
  • int: 32-bit signed integer. Range: -231 to 231-1.
  • long: 64-bit signed integer. Range: -263 to 263-1.
  • float: 32-bit floating point. Used for single-precision decimal (has about 7 decimal digits of precision) numbers.
  • double: 64-bit floating point. Used for double-precision decimal (accurate up to sixteen decimal places) numbers.
  • char: 16-bit Unicode character.
  • boolean: Represents one bit of information with two possible values: true and false.

Example:

byte b = 100;
short s = 10000;
int i = 100000;
long l = 100000L;

float f = 10.5f;
double d = 10.5;

char c = 'A';
boolean bool = true;

 

Reference Data Types

Reference Data Types are objects that store references to the memory location where data is stored. They include:

  • Class: User-defined data types. It describes the content of the object.
  • Array: It provides the fixed-size data structure that stores the elements of the same type.
  • Interface: Abstract type used to specify a behavior that classes must implement.
  • String: It is a predefined class. It represents immutable sequence of characters.
  • Enumeration: It is a special kind of class that is type-safe. Each element inside the enum is an instance of that enum.
  • Annotation: It provides a way to associate metadata with program elements.

Example:

String str = "Hello, World!";
int[] arr = {1, 2, 3, 4, 5};
MyClass obj = new MyClass();

Type Casting

Type casting is converting one data type into another. There are two types of casting:

Implicit Type Casting

  • Implicit Type Casting happens when the compiler automatically changes data from one type to another without the programmer needing to specify it. This automatic conversion occurs when a value of one data type is assigned to a variable of a different data type. For example:
int i = 100;
long l = i;

Explicit Type Casting

  • Explicit Type Casting involves changing a value from one data type to another by clearly specifying the desired type within the code. Unlike implicit casting, where the conversion happens automatically, explicit casting requires the programmer to manually indicate the type to which the value should be converted. For example:
double d = 10.5;
int i = (int) d;

 

Reference vs Primitive Data Types

Reference Data TypePrimitive Data Type
It is not pre-defined except the String.It is pre-defined in Java.
All reference type begins with Uppercase letter.All primitive type begins with a lowercase letter.
It is used to invoke or call methods.We cannot invoke the method with a primitive type.
It can be null.It cannot be null. It always has value.
JVM allocates 8 bytes for each reference variable, by default.Its size depends on the data type.

 

Memory Allocation and Garbage Collection

In Java, the new keyword is used to create an instance of the class. In other words, it instantiates a class by allocating memory for a new object and returning a reference to that memory. Objects occupy memory in the Java Heap space.

Here is an example of how a new instance of the class MyClass is created:

MyClass objectName = new MyClass();  

When an object has no references pointing to it, the memory it occupies can be freed up during the garbage collection process.

Last updated on