DEV Community

ViGnEsH
ViGnEsH

Posted on

Java Data Types

Primitive Data Types

Primitive data types are the most basic data types available in Java. There are eight primitive data types, each serving a specific purpose:

1. byte:

  • Size: 8-bit
  • Range: -128 to 127
  • Usage: Memory-efficient storage in large arrays.

byte b = 100;

2. short:

  • Size: 16-bit
  • Range: -32,768 to 32,767
  • Usage: Suitable for saving memory in large arrays.

short s = 10000;

3. int:

  • Size: 32-bit
  • Range: -(2 to 231-1
  • Usage: Default choice for integer values.

int i = 100000;

4. long:

  • Size: 64-bit
  • Range: -263 to 263-1
  • Usage: For large integer values.

long l = 100000L;

5. float:

  • Size: 32-bit
  • Usage: For fractional numbers, with single precision.

float f = 234.5f;

6. double:

  • Size: 64-bit
  • Usage: For fractional numbers, with double precision.

double d = 123.4;

7. boolean:

  • Values: true or false
  • Usage: For simple flags and conditions.

boolean flag = true;

8. char:

  • Size: 16-bit
  • Range: 0 to 65,535 (Unicode characters)
  • Usage: For storing characters.

char c = 'A';

Non-Primitive Data Types

Non-primitive data types in Java include Strings, Arrays, Classes, Objects, Interfaces, and Collections. They store references to data, support methods and properties, and are used to represent more complex information than primitive data types.

Java Programming

Output:

Top comments (0)