Sobes.tech
Junior

What data types exist in the context of JVM?

sobes.tech AI

Answer from AI

In the context of JVM, there are two types of data: primitive types and reference types.

Primitive Types:

  • Represent basic values (numbers, characters, boolean values).
  • The size and range of these types are strictly defined by the JVM specification.
  • Passed by value.
  • Stored in the stack.

List of primitive types:

  • boolean: true or false.
  • byte: 8-bit signed integer.
  • short: 16-bit signed integer.
  • char: 16-bit Unicode character (unsigned).
  • int: 32-bit signed integer.
  • long: 64-bit signed integer.
  • float: 32-bit single-precision floating point.
  • double: 64-bit double-precision floating point.

Reference Types:

  • Represent references (addresses) to objects in memory (Heap).
  • The size of references usually depends on the JVM architecture (32-bit or 64-bit).
  • Passed by reference (reference passing), although technically a copy of the reference itself is passed.
  • Stored in the heap.

Types of reference types:

  • Class Types: Instances of user-defined or library classes (e.g., String, Object).
  • Interface Types: References to objects implementing a specific interface.
  • Array Types: Container objects for storing ordered collections of elements (both primitive and reference types).

Example usage:

// Primitive type
int primitiveInt = 10;

// Reference type (String object)
String referenceString = new String("Hello");

// Primitive type in array
int[] primitiveArray = {1, 2, 3};

// Reference type in array
String[] referenceArray = {"A", "B"};