Java Data Types


In Java, data types are used to define the type of data that a variable can hold. Java has two categories of data types:

  1. Primitive Data Types: These are the most basic data types in Java and represent single values. They are predefined by the language and have specific memory sizes. There are eight primitive data types in Java:

    • byte: 8-bit signed two's complement integer.
    • short: 16-bit signed two's complement integer.
    • int: 32-bit signed two's complement integer.
    • long: 64-bit signed two's complement integer.
    • float: 32-bit IEEE 754 floating-point number.
    • double: 64-bit IEEE 754 floating-point number.
    • char: 16-bit Unicode character.
    • boolean: Represents true or false values.

    Example:

    int age = 30;
    double salary = 50000.50;
    char grade = 'A';
    boolean isStudent = true;

  2. Reference Data Types: These data types are used to store references (memory addresses) to objects rather than the actual data. Reference data types include:

    • Classes: User-defined data types.
    • Interfaces: Defines a contract for classes to implement.
    • Arrays: Ordered collection of elements of the same data type.

    Example:

    String name = "John";
    Object obj = new SomeClass();
    int[] numbers = {1, 2, 3, 4, 5};

It's important to note that Java is a statically typed language, which means you must declare the data type of a variable before using it. Java's strong typing system enforces strict type checking at compile time, which helps prevent certain types of errors.

Additionally, Java allows you to create user-defined data types through classes and interfaces, which can be used to encapsulate data and behavior in object-oriented programming.


Data types in Java are used to define the type of data that can be stored in a variable. There are two types of data types in Java:

  • Primitive data types are predefined by the Java language and are named by a reserved keyword. Primitive values do not share state with other primitive values. The eight primitive data types supported by the Java programming language are:

    • boolean: Stores a true or false value.
    • byte: Stores a signed 8-bit integer value.
    • char: Stores a single 16-bit Unicode character value.
    • short: Stores a signed 16-bit integer value.
    • int: Stores a signed 32-bit integer value.
    • long: Stores a signed 64-bit integer value.
    • float: Stores a 32-bit floating-point number value.
    • double: Stores a 64-bit floating-point number value.
  • Non-primitive data types are created by the user and are not predefined by the Java language. Non-primitive data types are also known as reference types, because they refer to objects. Some examples of non-primitive data types include:

    • String: Stores a sequence of characters.
    • Array: Stores a collection of elements of the same data type.
    • Class: Stores a definition of a class.



Here are some examples of how to declare variables of different data types in Java:

Java

// Primitive data types
boolean isDogFriendly = true;
byte numberOfLegs = 4;
char myFavoriteCharacter = 'A';
short myAge = 30;
int myFavoriteNumber = 10;
long myBankAccountBalance = 1000000;
float myAverageGrade = 95.5f;
double myHeight = 1.75;

// Non-primitive data types
String myName = "John Doe";
int[] myFavoriteNumbers = {10, 20, 30};
class Dog {
  String name;
  int age;

  public Dog(String name, int age) {
    this.name = name;
    this.age = age;
  }
}

Dog myDog = new Dog("Fido", 5);

When declaring a variable, you must specify the data type of the variable. This helps the Java compiler to check for errors and to generate efficient code.


Enroll Now

  • Java Programming
  • Machine Learning