Java data types..

Java has two types of data types: primitive data types and reference data types.

  1. Primitive Data Types: These are basic data types provided by Java, which are built into the language. They are used to represent simple values, such as numbers and characters. There are eight primitive data types in Java:
  • byte: used to store whole numbers from -128 to 127.
  • short: used to store whole numbers from -32,768 to 32,767.
  • int: used to store whole numbers from -2,147,483,648 to 2,147,483,647.
  • long: used to store whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  • float: used to store floating-point numbers with single precision.
  • double: used to store floating-point numbers with double precision.
  • boolean: used to store true or false values.
  • char: used to store a single character, such as ‘a’ or ‘5’.
  1. Reference Data Types: These data types are used to refer to objects in Java. They don’t hold the actual data but rather a reference to the memory location where the data is stored. Examples of reference data types in Java include String, Arrays, and Classes.
  • String: used to represent a sequence of characters.
  • Arrays: used to store multiple values of the same data type in a single variable.
  • Classes: used to define objects with their own properties and methods.

In Java, variables must be declared with a data type before they can be used. The data type determines the size and type of data that can be stored in the variable. Primitive data types are stored in the stack memory, while reference data types are stored in the heap memory.

Share