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.




Rs. 5.0 Rs. 10.0


Buy Now