DATA TYPES IN JAVA

  

Data types are divided into two groups:

  • Primitive data types - includes byte, short, int, long, float, double, boolean and char.
  • Non-primitive data types - such as String, Arrays and Classes

PRIMITIVE DATA TYPE 

A primitive data type specifies the size and type of variable values, and it has no additional methods.

Primitive types are divided into two groups:

  1. BOOLEAN 
  2. NUMERIC 

BOOLEAN 

 BOOLEAN:

A Boolean data type is declared with the Boolean keyword and can only take the value TRUE or FALSE. 

EXAMPLE 1: 

package datatypes;

public class string {

    public static void main (String[]args) (

        boolean t= "true";
        boolean f= "false";

        System.out.printIn(t);
        System.out.printIn(f);

    }

}

OUTPUT


true

false

NUMERIC

Numeric data type are divided into 

  1. CHARACTER 
  2. INTEGRAL

CHARACTER 

Char

The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c':

EXAMPLE 1: 

package datatypes;

public class char {

    public static void main (String[]args) (

        char letter= 'a';

        System.out.printIn(letter);

    }

}

OUTPUT


a


INTEGRAL

INTEGRAL is divided into

  1. INTEGER TYPE
  2. DECIMAL TYPE / FLOATION POINT

INTEGER TYPE

Integer types store whole numbers, positive or negative (such as 123 or -456), without decimals. Valid types are BYTE, SHORT, INT, AND LONG Which type you should use, depends on the numeric value.

BYTE

The Byte data type can store whole numbers from -128 to 127. This can be used instead of int or other integer types to save memory when you are certain that the value will be within -128 and 127:

EXAMPLE: 

package datatypes;

public class Byte {

    public static void main (String [] args) {

        System.out.println(Byte.SIZE/8 + " : Bit");

        System.out.printIn(Byte.MIN_VALUE);

        System.out.printIn(Byte.MAX_VALUE);

    }

}

}

OUTPUT


1 : Bit

-128

127

SHORT

The short data type can store whole numbers from -32768 to 32767:

EXAMPLE: 

Package datatypes;

public class Byte {

    public static void main (String [] args) {

        System.out.println(Short.SIZE/8 + " Byte");

        System.out.printIn(Short.MIN_VALUE);

        System.out.printIn(Short.MAX_VALUE);

    }

}

OUTPUT


1 : Bit

-128

127

INT

The int data type can store whole numbers from -2147483648 to 2147483647. In general, and in our tutorial, the int data type is the preferred data type when we create variables with a numeric value.

EXAMPLE: 

Package datatypes;

public class Byte {

    public static void main (String [] args) {

        System.out.println(Integer.SIZE/8 + " Byte");

        System.out.printIn(Integer.MIN_VALUE);

        System.out.printIn(Integer.MAX_VALUE);

    }

}

OUTPUT


4 : Byte

-2147483648

2147483647

LONG

The Long data type can store whole numbers from -9223372036854775808 to 9223372036854775807. This is used when int is not large enough to store the value. Note that you should end the value with an "L":

EXAMPLE: 

Package datatypes;

public class Byte {

    public static void main (String [] args) {

        System.out.println(Long.SIZE/8 + " Byte");

        System.out.printIn(Long.MIN_VALUE);

        System.out.printIn(Long.MAX_VALUE);

    }

}

OUTPUT


8 : Byte

-9223372036854775808

9223372036854775807


DECIMAL TYPE / FLOATING POINT

Decimal point types represent numbers with a fractional part, containing one or more decimals. There are two types: float and double.

You should use a floating point type whenever you need a number with a decimal, such as 9.99 or 3.14515.

FLOAT

The float data type can store fractional numbers from 4 : Byte, 1.4E-45 to 3.4028235E38. Note that you should end the value with an "f":

EXAMPLE: 

Package datatypes;

public class Byte {

    public static void main (String [] args) {

        System.out.println(Float.SIZE/8 + " Byte");

        System.out.printIn(Float.MIN_VALUE);

        System.out.printIn(Float.MAX_VALUE);

    }

}

OUTPUT


4 : Byte

1.4E-45

3.4028235E38

DOUBLE

The double data type can store fractional numbers from 8 : Byte, 4.9E-324 to 1.7976931348623157E308 Note that you should end the value with a "d":

EXAMPLE: 

Package datatypes;

public class Byte {

    public static void main (String [] args) {

        System.out.println(Double.SIZE/8 + " Byte");

        System.out.printIn(Double.MIN_VALUE);

        System.out.printIn(Double.MAX_VALUE);

    }

}

OUTPUT


8 : Byte

4.9E-324

1.7976931348623157E308


NON-PRIMITIVE DATA TYPE 

Non-primitive data types are called reference types because they refer to objects.

The main difference between primitive and non-primitive data types are:

  • Primitive types are predefined (already defined) in Java. Non-primitive types are created by the programmer and is not defined by Java (except for String).
  • Non-primitive types can be used to call methods to perform certain operations, while primitive types cannot.
  • A primitive type has always a value, while non-primitive types can be null.
  • A primitive type starts with a lowercase letter, while non-primitive types starts with an uppercase letter.
  • The size of a primitive type depends on the data type, while non-primitive types have all the same size.
Examples of non primitive types are String, Arrays, Class, Object and interface etc.

STRING
  • String is used to storing Text. 
  • A String variable contains a collection on characters surrounded by double quotes.
  • An array of characters works same as java string 
EXAMPLE 1: 

package mupackage;

public class string {

    public static void main (String[]args) (

        String s= "ARKREDDY";

        System.out.printIn(s);

    }

}

OUTPUT


ARKREDDY


EXAMPLE 2: 

package mupackage;

public class string {

    public static void main (String[]args) (

        char[] ch={'A',R,''K','R','E','D,''D','Y'};
        String s= new String(ch);

        System.out.printIn(s);

    }
}

OUTPUT


ARKREDDY


Array

Class

Object

Interface