Java Data Types

Java data types

Data Types in Java

Data types are used to identify the types of data in a program. It means what kind of data usage is going to put into a memory location.
In Java, there are two categories of data types
  •    Primitive data types(Pre-define)
  •    Reference data types(Non-primitive)

Primitive data types:

It loads automatically into the memory as soon as Java program executes. Primitives single a single value. Primitive data types are predefined by the language and named by a keyword. 
There are eight primitive data types and it can be divided into four groups.
  1. Integer (byte, short, int, double)
  2. Floating or real number (float, double)
  3. Character (char and it is 16-bit Unicode character)
  4. Boolean (Boolean)


Type
Size
Range of values that can be stored
Default Value
byte
1 byte
−128 to 127
0
short
2 bytes
−32768 to 32767
0
int
4 bytes
−2,147,483,648 to 2,147,483,647
0
long
8 bytes
9,223,372,036,854,775,808 to
9,223,372,036,854,755,807
0L
float
4 bytes
3.4e−038 to 3.4e+038
0.0f
double
8 bytes
1.7e−308 to 1.7e+038
0.0d
char
2bytes
\u0000 to \uFFFF(Maximum Value-65535)
Null(‘\0’)
Boolean
1 bit
true or false
false

Data Types in java
Java Data Types

Examples:

class JavaDataTypesExample{
     public static void main(String args[])
     {
        byte byteVar = 12;
        short shortVar = 37;
        int intVar =80;
        long longVar =898034892149L;
        float floatVar = 20.563423424f;
        double doubleVar = 20.12323423423468326;
        boolean booleanVar = true;
        char charVar ='A';
   
        System.out.println("Value Of byte Variable is       " + byteVar);
        System.out.println("Value Of short Variable is      " + shortVar);
        System.out.println("Value Of int Variable is        " + intVar);
        System.out.println("Value Of long Variable is       " + longVar);
        System.out.println("Value Of float Variable is      " + floatVar);
        System.out.println("Value Of double Variable is     " + doubleVar);
        System.out.println("Value Of boolean Variable is    " + booleanVar);
        System.out.println("Value Of char Variable is       " + charVar);
     }
 }


Outputs:
Value Of byte Variable is       12
Value Of short Variable is      37
Value Of int Variable is        80
Value Of long Variable is       898034892149
Value Of float Variable is      20.563423
Value Of double Variable is     20.123234234234683
Value Of boolean Variable is    true
Value Of char Variable is       A

Reference Data types:

Non-Primitive or Reference data types are formed with the help of primitive data types. It is created by the programmer. It stores the memory address of an object.  The default value of any reference variable is null. Example classes, interface, Arrays, String.
class Employee
{
   String name;
    String aadhar;
    String emailAddress;
    int yearOfBirth;
}
class EmpDetail
{
public static void main(String[] args) {
        Employee emp1 = new Employee(); // Reference Data type
        emp 1.name = "Habib";
        emp 1.aadhar = "4525-4545-4345-3423";
        emp 1.emailAddress = "habib@abc.com";

        Employee emp2 = new Employee();// Reference Data type
        emp 2.name = "Sachin";
        emp 2. aadhar = "4563-7384-9031";
        emp 2.yearOfBirth = 1974;

        System.out.println("Name: " + emp 1.name);
        System.out.println("Aadhar: " + emp 1.aadhar);
        System.out.println("Email Address: " + emp 1.emailAddress);
        System.out.println("Year Of Birth: " + emp 1.yearOfBirth);

        System.out.println("Name: " + emp 2.name);
        System.out.println("Aadhar: " + emp 2.aadhar);
        System.out.println("Email Address: " + emp2.emailAddress);
        System.out.println("Year Of Birth: " + emp2.yearOfBirth);

    }
}


Here emp1 and emp2 are reference data types



SHARE THIS
Previous Post
Next Post