Datatype in Java

·

2 min read

In Java, a data type defines what kind of value a variable can hold and tells the Java Virtual Machine (JVM) how to interpret the variable’s value. Data types in Java are divided into two categories:

1. Primitive Data Types

2. Non-Primitive Data Types

  1. Primitive Data Types

Primitive data types are the most basic types of data in Java. There are 8 primitive data types in Java: • byte • short • int • long • float • double • char • boolean

1.1 byte • Description: Used to store small integer values. • Size: 8-bit signed integer. • Range: From -128 to 127. • Default Value: 0.

1.2 short • Description: Used for numbers larger than byte but smaller than int. • Size: 16-bit signed integer. • Range: From -32,768 to 32,767. • Default Value: 0.

1.3 int • Description: The most commonly used data type for storing non-decimal integers. • Size: 32-bit signed integer. • Range: From -2^31 to 2^31-1. • Default Value: 0.

1.4 long • Description: Used to store large integers. • Size: 64-bit signed integer. • Range: From -2^63 to 2^63-1. • Default Value: 0.

1.5 float • Description: Used to store decimal values (single-precision). • Size: 32-bit floating-point number. • Default Value: 0.0f.

1.6 double • Description: Used for storing larger decimal numbers (double-precision). • Size: 64-bit floating-point number. • Range: From 4.9E-324 to 1.8E+308. • Default Value: 0.0d.

1.7 boolean • Description: Represents two possible values: true or false. • Size: 1 bit. • Default Value: false.

1.8 char • Description: Used to store a single 16-bit Unicode character. • Size: 2 bytes. • Default Value: ‘\u0000’ (null character).

2. Non-Primitive Data Types

Non-primitive data types are user-defined types in Java. These include:

Arrays- Collection of similar type elements

String- Sequence of characters

Collections- Interface and class Ex(list, arrayList , set hashset etc)

Objects (e.g., Employee)