Data types JAVA

Java has several built-in data types that are used to define the type of data that can be stored in variables.

Here are the primary data types in Java:

1. Primitive Data Types:

  • byte: Represents a signed 8-bit integer (-128 to 127).
  • short: Represents a signed 16-bit integer (-32,768 to 32,767).
  • int: Represents a signed 32-bit integer (-2,147,483,648 to 2,147,483,647).
  • long: Represents a signed 64-bit integer (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807).
  • float: Represents a single-precision 32-bit floating-point number.
  • double: Represents a double-precision 64-bit floating-point number.
  • boolean: Represents a boolean value (true or false).
  • char: Represents a single Unicode character.

2. Reference Data Types:

  • String: Represents a sequence of characters.
  • Arrays: Represents a collection of elements of the same type.
  • Classes: Represents user-defined objects and classes.

It’s important to note that primitive data types are basic and have a fixed size in memory, while reference data types are more complex and can be of variable size.

Additionally, Java also supports type conversion between different data types through casting and provides wrapper classes (e.g., Integer, Double, Boolean) that allow primitive data types to be treated as objects.

Example usage:

int age = 25;
double salary = 5000.0;
boolean isActive = true;
char grade = 'A';

String name = "John Doe";
int[] numbers = {1, 2, 3, 4, 5};
EasyExamNotes © 2023