Java Data Types

Java is a statically typed programming language, which means that the data type of a value is determined at compile-time and cannot change at runtime.

This article will discuss the different Java data types and their uses.

Primitive Data Types:

Primitive data types are basic data types in Java, including:

  • int : used to store integers, such as 123. It takes up 4 bytes of memory and has a minimum value of -2^31 and a maximum value of 2^31-1.
  • long : used to store larger integers, such as 1234567890123456. It takes up 8 bytes of memory and has a minimum value of -2^63 and a maximum value of 2^63-1.
  • float : used to store floating-point numbers, such as 3.14. It takes up 4 bytes of memory and has a precision of 6 to 7 decimal places.
  • double : used to store larger floating-point numbers, such as 3.14159265. It takes up 8 bytes of memory and has a precision of 15 decimal places.
  • char : used to store a single character, such as A. It takes up 2 bytes of memory and can store any Unicode character.
  • boolean : used to store a true or false value. It takes up 1 byte of memory.
  • byte : used to store small integers, such as 12. It takes up 1 byte of memory and has a minimum value of -128 and a maximum value of 127.

Examples:

int x = 123;
long y = 1234567890123456L;
float z = 3.14f;
double w = 3.14159265;
char c = 'A';
boolean b = true;
byte a = 12;

Note: When using the float data type, the value must be suffixed with an f to indicate that it is a float. For example, 3.14f.

Reference Data Types:

Reference data types are objects and arrays, and they are created using the new operator. They are stored in the heap and are automatically garbage collected by the JVM.

Examples of reference data types include:

  • String: used to store sequences of characters, such as "Hello World".
  • Array: used to store a collection of values of the same data type.
  • Object: used to store information and methods related to a particular type or class.

Examples:

String s = "Hello World";
int[] arr = {1, 2, 3, 4, 5};
MyClass obj = new MyClass();

Difference between Primitive and Non-Primitive Data Types:

Primitive Data Types:

  • In Java, primitive data types are the basic building blocks of the language and are stored directly in the memory.
  • Examples of primitive data types include int, float, double, char, and boolean.
  • Primitive data types have a fixed size and can only store a single value.

Non-Primitive Data Types:

  • Non-primitive data types, also known as reference data types, are objects or arrays that store references to data rather than the data itself.
  • Examples of non-primitive data types include String, Array, and Object.
  • Non-primitive data types are stored in the heap and can store multiple values.

Differences:

  • Primitive data types are stored directly in memory, while non-primitive data types store references to the data in memory.
  • Primitive data types have a fixed size and can only store a single value, while non-primitive data types can store multiple values.
  • Primitive data types are directly accessible by the JVM, while non-primitive data types are stored in the heap and are accessible via references.

Conclusion

In Java, data types are used to specify the type of values that a variable can store.

Understanding the different data types and their uses is essential for writing efficient and effective Java code.

This article has introduced the basic data types in Java, including primitive and reference data types, and provided examples of how to use each type.

Java Basics