Sobes.tech
Intern

What are primitive data types?

sobes.tech AI

Answer from AI

Primitive data types in Java:

  • boolean: accepts values true or false. Occupies 1 bit.
  • byte: 8-bit signed integer. Range from -128 to 127.
  • short: 16-bit signed integer. Range from -32,768 to 32,767.
  • int: 32-bit signed integer. Range from -2,147,483,648 to 2,147,483,647. The most commonly used integer type.
  • long: 64-bit signed integer. Range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Used for very large numbers.
  • float: 32-bit single-precision floating point number. Used for fractional numbers.
  • double: 64-bit double-precision floating point number. Used for fractional numbers with greater precision.
  • char: 16-bit Unicode character. Used to store characters.

Differences from reference types:

  • The value of a primitive type is stored directly in the variable.
  • Variables of primitive types are stored in the stack.
  • Primitive types have a fixed size.
  • Primitive types do not have methods.

Example of usage:

// Declaration and initialization of primitive types
int age = 30;
double price = 19.99;
boolean isActive = true;
char initial = 'J';

// Operations on primitive types
int sum = age + 5;
double total = price * 2.0;