Java – Data types

Data type:

  • In the declaration of every variable, it is mandatory to specify its data type.
  • Data type describes,
    • The size of memory allocated to variable.
    • The type of data allowed to store into variable.

Syntax: 

  • data_type  variable_name = value ;

Example:

  • int sum = 0;    
  • float balance = 3000.0;           
  • String name = “Amar”;

Data types classified into Primitive and Non-Primitive types.

Limits:

  • A data type limit describes the minimum and maximum values we can store into a variable.
  • Every data type is having size and it is represented by bytes
  • One byte equals to 8 bits.

Integer types: We have 4 integer types among eight available primitive types in java.

TypeSize(bytes)Limits
byte1-128(-2^7) to  +127(2^7-1)
short2-32,768(-2^15) to 32,767(2^15-1)
int4-2,147,483,648(-2^31) to 2,147,483,647(2^31-1)
long8-9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63-1)

Note:

  • For every primitive type, there is a corresponding wrapper class.
  • Wrapper classes providing pre-defined variables to find Size and Limits.

Program to print sizes of Integer types:

class Sizes
{
            public static void main(String[] args) {
                        System.out.println(“Byte size : ” + Byte.SIZE/8 + ” bytes”);
                        System.out.println(“Short size : ” + Short.SIZE/8 + ” bytes”);
                        System.out.println(“Integer size : ” + Integer.SIZE/8 + ” bytes”);
                        System.out.println(“Long size : ” + Long.SIZE/8 + ” bytes”);
            }
}

Program to print Limits of Integer types:

class Limits
{
            public static void main(String[] args) {
                        System.out.println(“Byte min val : ” + Byte.MIN_VALUE);
                        System.out.println(“Byte max val : ” + Byte.MAX_VALUE);
                        System.out.println(“Short min val : ” + Short.MIN_VALUE);
                        System.out.println(“Short max val : ” + Short.MAX_VALUE);
            }
}

Character data type: Character type variable is used to store alphabets, digits and symbols.

class Code
{
            public static void main(String[] args) {
                        char x = ‘A’;
                        char y = ‘5’;
                        char z = ‘$’;
                        System.out.println(“x val : ” + x + “\ny val : ” + y + “\nz val : ” + z);
            }
}

ASCII : Representation of each character of language using constant integer value.

A-65
B-66



Z-90
a-97
b-98
….
….
….
z-122
0-48
1-49
..
..
..
9-57
*-34
#-35
$-36


Scroll to Top