Sobes.tech
Intern

What are the data types in Java?

sobes.tech AI

Answer from AI

In Java, there are two main types of data: primitive and reference.

Primitive data types store their values directly in memory allocated for the variable.

Type Size (bits) Range
byte 8 -128 to 127
short 16 -32,768 to 32,767
int 32 -2³¹ to 2³¹ - 1
long 64 -2⁶³ to 2⁶³ - 1
float 32 Single-precision floating point
double 64 Double-precision floating point
boolean Not defined true or false
char 16 Unicode character

Reference data types store references to objects in memory. The objects themselves are stored in the heap.

  • Classes: Define the structure and behavior of objects.
    class MyClass {
        // Fields and methods
    }
    
  • Interfaces: Define a contract for classes.
    interface MyInterface {
        // Abstract methods
    }
    
  • Arrays: Collections of elements of the same type.
    int[] numbers = new int[5];
    String[] names = {"Alice", "Bob"};
    
  • Enumerations: Set of named constants.
    enum Day {
        MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
    }
    
  • Annotations: Provide metadata about the code.
    @Override
    
  • Strings: Are objects of the String class.
    String message = "Hello, World!";
    
What are the data types in Java? — Java - sobes.tech