Data Types in Java

Data Types: 1) Predefined Primitive
                   2) User Defined
                      - class
                      - structure
                      - enum

1] Integral    - byte    -     8 bit    -    28 = 256    -128    to 127
                   - short    -    16 bit
                   - int    -    32 bit
                   - long    -    64 bit

2] Floating    - float    -    32 bit
                    - double    64 bit

3] Textual    - char    -    16 bit Unicode

4] Logical    - boolean    1 bit

You can find more about Unicode on www.unicode .org

In ASCII storage system the last 7 bits are used to store data and the first bit is reserved for storing sign. Hence it is difficult to store foreign language symbol, that’s where Unicode comes handy.

1) There are two categories of data types in Java :
    a)    Primitive Data Type
    b)    User Defined Data Types (class, structure, enum)

2)    All data type in Java are is strictly typed (range is fixed)  and pre-defined.
3)    Integral and Floating point data types are signed. The default of Integral category is int and byte and short automatically get promoted to int when any calculation is done on them.
4)    Type casting means forcibly putting a large size data type into a smaller size which might result in loss of precession.

class ByteText
 {
    public static void main(String args[])
    {
        byte a = 10;
        byte b = 13;
        //byte c = (a+b);        // possible loss of precision
        //System.out.println(c);
        int c1 = (a+b);
        System.out.println(c1);    // output : 23
        byte c2 = (byte) (a+b);
        System.out.println(c2);    // output : 23
        byte c3 = (byte)(a*b);
        System.out.println(c3);    // output : -126
    }
}

byte range is -128 to 127. When there is overflow it starts from beginning
-128    -127    -126
128        129        130

Auto conversion – smaller to bigger

byte --> short --> int -->    double

Type casting – bigger to smaller

byte <-- short <-- int <--    double

Long Type : Since the default of Integral category is int while initializing long if the value goes beyond 32 bits we should have a ‘l’ or ‘L’ suffixed to the value.
           
class LongText
{
    public static void main(String args[])
    {
        long a = 10;
        long b = 12345678910;
        System.out.println(a);
        System.out.println(b);
    }
}

Output : Compile Error - Integer number too large    Solution  : suffix ‘L’ or ‘l’
long b = 12345678910L;

Floating Type : The default of the floating type category is double and hence while initializing float
we should have a ‘f’ or ‘F’ suffixed to the value. Double gives more precision.

class FloatText
{
    public static void main(String args[])
    {
        float a = 10;
        float b = 10.00;
        System.out.println(a);
        System.out.println(b);
    }
}

Default of float is double that’s why in float b = 10.0 , 10.0 is treated as double. In this case you are trying to store 64 bit value in 32 bit value. Solution for this is : float b = 10.0F;

OUTPUT :  10.0
                   10.0

char Type:
class CharText
 {
    public static void main(String args[])
    {
        char c = 'a';
        int i = c;
        System.out.println("The ASCI Value of " +c + " is " + i);
    }
}
OUTPUT : The ASCII value of a is 97.

boolean Type :
class BooleanText
 {
    static boolean a;
    public static void main(String args[])
    {
        System.out.println(a);
    }
}

OUTPUT :     false
If static method calls non-static variable, the compile error is :
Non static variable ‘b’ cannot be referenced from a static context

Use of toString() :
class PlusText
 {
    public static void main(String args[])
    {
        int a = 10;
        int b = 10;
        String s = "Hello";
        Integer i = new Integer(a);
        String s1 = i.toString();
        Integer j = new Integer(b);
        String s2 = j.toString();
        System.out.println(a+s+b);
        System.out.println(s+s1+s2);
    }
}
OUTPUT :     1010Hello

If we do not use toString() then it will be 20Hello.
Java.lang.Object - top most class.
    |                        All classes inherit it. It is a Super class.
    v
toString()            - converts object into a string

Data Variables  Wrapper Classes (convert each data variable into object)
byte                     Byte
short                   Short
int                       Integer
long                    Long
float                    Float
double               Double
char                  Character
boolean            Boolea