Java Basic Data Types

We will see the basic data types in Java.

We will also write a simple program to assign values to different data types and print their values.

Primitive data types

byte

byte is a 8 bit signed two’s complement integer. Can stores values from -128 to 127.

byte bt = 50;
bt = -129; // this will give compilation error as range is exceeded
bt = 130; // this will give compilation error as range is exceeded

short

short is a 16 bit signed two’s complement integer. Can stores values from -32768 to 32767.

short sh = 50;
sh = -32769; // this will give compilation error as range is exceeded
sh = 32768; // this will give compilation error as range is exceeded

int

int  is a 32 bit signed two’s complement integer. Can stores values from -2147483648 to 2147483647.

int it = 5000000;
it = 2147483649; // this will give compilation error as range is exceeded
it = -2147483649; // this will give compilation error as range is exceeded

long

long is a 64 bit signed two’s complement integer. Can stores values from -9223372036854775808 to 9223372036854775807.
For assigning a value greater than int value(2147483647) we need to append the literal value with a (L or l (smallcase L)), as all the numerical literal values are by default treated as int.

long lo = 1200000000;
lo = 21000000000L;
lo = 21000000000l;

// this will give compilation error as the value is treated as 
// int unless we explicitly add a (l or L) at the end.
lo = 21000000000;

float

A float is a single-precision 32-bit IEEE 754 floating point. While assigning a float value we have to explicitly append an f at the end as by default all decimal numbers are treated as double.

float fo = 1.3f;
fo = 1.3; // this will give compilation error

double

A double is a double-precision 64-bit IEEE 754 floating point. As by default all decimal numbers are treated as double, it is optional to append a d at the end.

double db = 34.50;
db = 50.40d;

boolean

boolean data type have only two values true and false. This data type is used for storing flag values which can have only 2 states.

boolean bn = true;
bn = false;

char
A char is a single 16-bit Unicode character. It has a minimum value of ‘\u0000’ (or 0) and a maximum value of ‘\uffff’ (or 65,535 inclusive). A character value is enclosed by single quote. We cannot represent any character which is above 65535 codepoint as a char in java, as it requires more than 16 bits to represent it.

char cr = 'b';
cr = "s"; // will give compilation error as we cannot use double quotes to enclose a char

 

String

Other than the above basic data types, Java also provides String data type, which is a set of characters enclosed by double quotes.

String str = "hello world";
str = 'hello world'; // will give compilation error as we cannot use single quotes to enclose a String

Java Program

We will write a java program to use all the above data types, assign them values and print the values.

public class BasicDataTypes {

    public static void main(String[] args) {

        // Primitive data types

        // byte
        // 8 bit signed two's complement integer. Can stores values from -128 to 127
        byte bt = 50;
        // bt = -129; this will give compilation error as range is exceeded
        // bt = 130; this will give compilation error as range is exceeded

        System.out.println("byte- " + bt);

        // short
        // 16 bit signed two's complement integer. Can stores values from -32768 to 32767
        short sh = 500;
        // sh = -32769; this will give compilation error as range is exceeded
        // sh = 32768; this will give compilation error as range is exceeded

        System.out.println("short- " + sh);

        // int
        // 32 bit signed two's complement integer. Can stores values from -2147483648 to 2147483647
        int it = 5000000;
        // it = 2147483649; this will give compilation error as range is exceeded
        // it = -2147483649; this will give compilation error as range is exceeded

        System.out.println("int- " + it);

        // long
        // 64 bit signed two's complement integer. 
        // Can stores values from -9223372036854775808 to 9223372036854775807
        long lo = 1200000000;
        // For assigning a value greater than int value we need to append 
        // the literal value with a (L or l (smallcase L))
        lo = 21000000000L;
        lo = 21000000000l;
        // lo = 21000000000; will give compilation error as the value is treated as int unless we explicitly add a (l or L) at the end.

        System.out.println("long- " + lo);

        // float
        // a single-precision 32-bit IEEE 754 floating point
        float fo = 1.3f;
        // fo = 1.3; have to explicitly append an f at the end as by default all decimal numbers are treated as double.

        System.out.println("float- " + fo);

        // double
        // a double-precision 64-bit IEEE 754 floating point
        double db = 34.50;
        db = 500000.45;

        System.out.println("double- " + db);

        // boolean
        // have only two values true and false
        boolean bn = true;
        bn = false;

        System.out.println("boolean- " + bn);

        // char
        // a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) 
        // and a maximum value of '\uffff' (or 65,535 inclusive).
        // A character value is enclosed by single quote.
        // Note: we cannot represent any character which is above 65535 codepoint 
        // as a char in java, as it requires more than 16 bits to represent it.
        char cr = 'b';
        // cr = "s"; will give compilation error as we cannot use double quotes to enclose a String

        System.out.println("char- " + cr);

        // String
        // Java also provides String data type, which is a set of characters enclosed by double quotes.
        String str = "hello world";
        // str = 'hello world'; will give compilation error as we cannot use single quotes to enclose a String

        System.out.println("String- " + str);
    }
}

Running the above program, produces the following output:-

byte- 50
short- 500
int- 5000000
long- 21000000000
float- 1.3
double- 500000.45
boolean- false
char- b
String- hello world

 

Leave a Reply

Your email address will not be published. Required fields are marked *