DEV Community

Veera Ganapathi
Veera Ganapathi

Posted on

Java Data Types

Data Types

Java data types define the type of data a variable can store in a program. They help the compiler allocate memory efficiently and ensure type safety. Java provides two main categories: primitive and non-primitive data types.

Types of Data Types

Data types in Java define the kind of data a variable can hold and the memory required to store it. They are broadly divided into two categories:

  • Primitive Data Types: Store simple values directly in memory.
  • Non-Primitive (Reference) Data Types: Store memory references to objects.

Primitive Data Types

Primitive data types store simple values directly in memory. Java provides eight primitive data types, each with a fixed size and range, which are summarized below:

Non-Primitive (Reference) Data Types

Non-primitive data types store references (memory addresses) rather than actual values. They are created by users and include types like String, Class, Object, Interface, and Array.

1. String

String represents a sequence of characters enclosed in double quotes. Unlike C/C++, Java strings are objects and are immutable.

Syntax:

String str = "Hello";
Enter fullscreen mode Exit fullscreen mode

2. Class

A class is a user-defined blueprint that defines variables and methods. It represents a type of object and forms the foundation of Object-Oriented Programming.

3. Object

An Object is an instance of a class representing real-world entities. It has state (data), behavior (methods), and identity (unique reference).

Top comments (0)