on Leave a Comment

Java Primitive Data Types

Java is Statically typed language. In Statically typed language, it is necessary to specify the data type of variables. Data type defines which type of data can be stored in variables. Operating system reserves memory for variables according to their data types. Java has different data types for all type of data such as integer, decimal, character etc. 

Java is strongly typed programming language.

Java has two types of data types:

(1) Primitive Data Type
(2) User-Defined Data Type

Primitive Data Types

Primitive data types are predefined data types and each data type is named by keyword. Java has 8 primitive data types.

boolean

- boolean is keyword in java

- The boolean data types represents one bit of information

- The boolean variables can store either of two values: true and false

- The boolean variables can be used in decision making statements

- Default value of boolean variable is false

Example:

public class datatypes
{
        public static void main(String[] args)
        {
                boolean b1;
                b1 = true;
                System.out.println(b1);
                b1 = false;
                System.out.println(b1);
        }
}

OUTPUT:

true
false

byte

- byte is a keyword in java

- The byte data type is 8-bit signed two’s complement integer

- The byte data type variables can store values between -128 and 127

- The byte variables is used for saving memory in large arrays

- Default value of byte variable is 0

Example:

public class datatypes
{
        public static void main(String[] args)
        {
                byte b1;
                b1 = 127;
                System.out.println("b1 = "+b1);
                System.out.println("After increment, b1 = "+(++b1));
        }
}

OUTPUT:

b1 = 127
After increment, b1 = -128

short

- short is a keyword in java.

- The short data type variables can store values between -32768 and 32767

- The short data type is a 16-bit signed two's complement integer

- Default value of short data type variable is 0

Example:

public class datatypes
{
        public static void main(String[] args)
        {
                short s;
                s = 32767;
                System.out.println("s = "+s);
                System.out.println("After increment, s = "+(++s));
        }
}

OUTPUT:

s = 32767
After increment, s = -32768

int

- int is a keyword in java

- The int data type is 32-bit signed two's complement integer

- The int data type variables can store values between - 2,147,483,648 (-2^31) and  2,147,483,647 (2^31 -1)

- Default value of int type variable is 0

Example:

public class datatypes
{
        public static void main(String[] args)
        {
                int i;
                i = 2147483647 ;
                System.out.println("i = "+i);
                System.out.println("After increment, i = "+(++i));
        }
}

OUTPUT:

i = 2147483647
After increment, i = -2147483648

long

- long is a keyword is java

- The long data type is 64-bit signed two's complement integer

- The long data type variables can store values between -263 and 263-1 

- Default value of long data type is 0.

Example:

public class datatypes
{
        public static void main(String[] args)
        {
                long l;
                l =  9223372036854775807L;
                System.out.println("l = "+l);
                System.out.println("After increment, l = "+(++l));
        }
}

OUTPUT:

l = 9223372036854775807
After increment, l = -9223372036854775808

float

- float is a keyword in java

- The float data type is a single-precision 32-bit floating point

- The float type is used for saving memory in large arrays for storing floating values

- Default value of float type is 0.0f

Example:

public class datatypes
{
        public static void main(String[] args)
        {
                float f;
                f =  -78.97f;
                System.out.println("f = "+f);
        }
}

OUTPUT:

f = -78.97

double

- double is a keyword in java

- The double data type is a double-precision 64-bit floating point

- Default value of double data type is 0.0d

Example:

public class datatypes
{
        public static void main(String[] args)
        {
                double d;
                d =  -778.07d;
                System.out.println("d = "+d);
        }
}

OUTPUT:

d = -778.07

char

- char is a keyword in java

- The char data type is 16-bit Unicode character

- Minimum and maximum value of char data type is '\u0000' (0) and '\uffff'

- Default value of char data type is '\u0000'

Example:

public class datatypes
{
        public static void main(String[] args)
        {
                char c;
                c =  'B';
                System.out.println("c = "+c);
        }
}

OUTPUT:

c = B

0 comments:

Post a Comment