Data Types

Data types:
  1. Primitive data type
  2. Non-Primitive data type

Primitive data type:
  1. Integer type (byte, short, int and long)
  2. Decimal type (float and double)
  3. Boolean type (boolean)
  4. Character type (char)

DataTypeValueDefault ValueSize (bits)Range
booleantrue or falsefalse1true and false
charUnicode character\u000016 \u0000 to \uFFFF
byteSigned Integer08 -128 to 127
shortSigned Integer016 -32768 to 32767
intSigned Integer032 -2147483648 to 2147483647
longSigned Integer064 -9223372036854775808 to 9223372036854775807
floatIEEE 754 Floating point 0.032 ±1.4E-45 to ±3.4028235E+38
doubleIEEE 754 Floating point 0.064 ±4.9E-324 to ±1.7976931348623157E+308


Non-Primitive data type:
  1. String
  2. Array
  3. Stack
  4. Vector
package com.java.basics;

public class Variables {

    public static void main(String[] args) {
        byte bmin = -0x80, bmax = 0x7f;
        System.out.println("Minimum value for byte is " + bmin 
                    + " and Maximum value for byte is " + bmax);
        
        short smin = -0x8000, smax = 0x7fff;
        System.out.println("Minimum value for short is " + smin 
                    + " and Maximum value for short is " + smax);
        
        int imin = -0x80000000, imax = 0x7fffffff;
        System.out.println("Minimum value for int is " + imin 
                    + " and Maximum value for int is " + imax);
        
        long lmin = -0x8000000000000000L, lmax = 0x7fffffffffffffffL;
        System.out.println("Minimum value for long is " + lmin 
                    + " and Maximum value for long is " + lmax);
        
        float fmin1 = -1.4e-45f, fmin2 = -3.4028235E+38f;
        float fmax1 = 1.4e-45f, fmax2 = 3.4028235E+38f;
        
        System.out.println("Minimum value for float is " + fmin1 
                + " and Minimum value for float is " + fmin2);
        System.out.println("Maximum value for float is " + fmax1 
                + " and Maximum value for float is " + fmax2);
        
        System.out.println(0.0f / 0.0f);
        System.out.println(1.0f / 0.0f);
        System.out.println(-1.0f / 0.0f);
        double dmin1 = -4.9E-324, dmin2 = -1.7976931348623157E+308;
        double dmax1 = 4.9E-324, dmax2 = 1.7976931348623157E+308;
        
        System.out.println("Minimum value for float is " + dmin1 
                + " and Minimum value for float is " + dmin2);
        System.out.println("Maximum value for float is " + dmax1 
                + " and Maximum value for float is " + dmax2);
        int x = 10, y = 20, z;
        z = x + y;
        
        System.out.println("Addition of two numbers is " + z);
        
        char ch = 'A';
        int unicode = ch;
        System.out.println("Unicode value of " + ch + " is " + unicode);
        
        double percentage = 78.56;
        System.out.println("In 12th standard got " + percentage + "%");
        
        boolean isEven = (x % 2 == 0);
        System.out.println(isEven);
        
        String str = "Hello ";
        str = str + "World";
        
        System.out.println(str);
    }
}

No comments:

Post a Comment