Primitive Data Types In Java With Examples

Primitive Data Types In Java With Examples

There are total 8 primitive data types available in Java – 

Numeric Data types

These datatypes are used to represent numbers 

Integer Data types

These data types are used to represent Whole Numbers like 2, 23, 435, 454367 etc.

1.    byte

This data type is suitable if you want to handle your data in the form of streams either from file or from the network. The range of this datatype is -128 to +127. So, the maximum possible value for a variable of a byte is 127 and the minimum value is -128.

Size: 1 byte (8 bits)
Range: [-128 to +127]

Examples –

a.    byte b = 127;       (Correct)

Here we assigned ‘127’ to a variable ‘b’ of type byte. The assigned value is in the range of byte data type. Hence, this assignment is correct. 

b.    byte b = 128;       (Incorrect)

Here we assigned ‘128’ to a variable ‘b’ of type byte. The assigned value is not in the range of byte data type. Hence, this assignment is incorrect.

Compile Time Error: 
Possible loss of precision
Found: int
Required: byte
 
c.    byte b = 234.234;    (Incorrect)

Here we assigned a double value to a byte variable. Hence, this assignment is incorrect. 

Compile Time Error:
Possible loss of precision
Found: double
Required: byte

d.    byte b = true;    (Incorrect)

Here we assigned a boolean value to a byte variable. Hence, this assignment is incorrect.

Compile Time Error:
Incompatible types
Found: boolean
Required: byte

e.    byte b = “CodeMeRight”;    (Incorrect)

Here we assigned a String value to a byte variable. Hence, this assignment is incorrect.

Compile Time Error:
Incompatible types
Found: java.lang.String
Required: byte

2.    short

This is the least used data type in Java. It is best suitable if we are using 16-bit processors like 8086. These processors are outdated and hence short datatype is also outdated.

Size: 2 bytes (16 bits)
Range: [-32768 to 32767]

Examples – 

a.    short s = 32767;   (Correct)

b.    short s = -32768;   (Correct)

c.    short s = 32768;   (Incorrect)

Compile Time Error
Possible loss of precision
Found: int
Required: short

d.    short s = 123.456;   (Incorrect)

Compile Time Error: 
Possible loss of precision
Found: double
Required: short

e.    short s = true;   (Incorrect)

Compile Time Error:
Incompatible Types
Found: boolean
Required: short

3.    int

This is the most used data type in Java. Unlike C Programming Language, the size of int is always 4 bytes in Java irrespective of any platform. The advantage of this fixed size in all platforms is that the chance of failure is less due to change in platform. That is why Java is considered as Robust language.

Size: 4 bytes
Range: [-2147483648 to +2147483647]

Examples – 

a.    int i = 2147483647;       (Correct)

Here we assigned ‘2147483647’ to a variable ‘i’ of type int. The assigned value is in the range of int datatype. Hence, this assignment is correct. 

b.    int i = -2147483648;       (Correct)

Here we assigned ‘-2147483648’ to a variable ‘i’ of type int. The assigned value is in the range of int datatype. Hence, this assignment is correct. 

c.    int i = 2147483648;       (Incorrect)

Here we assigned ‘2147483648’ to a variable ‘i’ of type int. The assigned value is not in the range of int datatype. Hence, this assignment is incorrect. 

Compile Time Error: 
Possible loss of precision
Found: long
Required: int

d.    int i = 234.234;    (Incorrect)

Here we assigned a double value to an int variable. Hence, this assignment is incorrect. 

Compile Time Error:
Possible loss of precision
Found: double
Required: int

e.    int i = true;    (Incorrect)

Here we assigned a boolean value to an int variable. Hence, this assignment is incorrect.

Compile Time Error:
Incompatible types
Found: boolean
Required: int

f.    int i = “CodeMeRight”;    (Incorrect)

Here we assigned a String value to an int variable. Hence, this assignment is incorrect.

Compile Time Error:
Incompatible types
Found: java.lang.String
Required: int

4.    long

If you are working with a numeric value out of the range of the int data type, then you should go for long data type.

Size: 8 bytes
Range: [-263 to 263-1]


Floating-Point Datatypes

These datatypes are used to represent Real Numbers like 0.23, 1.23, 43.3345 etc.

5.    float

If you want to handle a decimal value to its 5 to 6 decimal places of accuracy, then we should go for this data type.

Size: 4 bytes
Range: [-3.4e38 to 3.4e38]


6.    double

If you want to handle a decimal value to its 14 to 15 decimal places of accuracy, then we should go for this data type.

Size: 8 bytes
Range:  [-1.7e308 to 1.7e308]

Examples – 

a.    double d = 234.889765543356    (Correct)

Here we assigned a decimal value to a double variable. Hence, this assignment is correct.

b.    double d = 237 (Correct)

Here we assigned an integer value to a double variable. But still this assignment is correct. Why? It’s because of Auto Type Casting. What is it? That’s the story of another chapter. But in short, at the time of compilation, the compiler converts the integer value to a double value. Hence it finally assigns 237.00 to the variable d.

Boolean Datatype

7.    boolean

The only possible values for a boolean variable are true/false. Also, it is case sensitive. You cannot assign True or TRUE to a boolean variable, otherwise you will get a compile time error saying, cannot find symbol. Unlike C++ language you cannot assign 0 as false to a boolean variable in Java.

Size: Not Applicable (Virtual Machine Dependent)
Range: Not Applicable (Allowed values are only true / false)

Examples – 

a.    boolean b = true;     (Correct)

Here we assigned a boolean value true in valid small case. Hence, this assignment is correct

b.    boolean b = 0;     (Incorrect)

Here we assigned an int value to a boolean variable. Hence, it is an incorrect assignment. 

Compile Time Error:
Incompatible types 
Found: int
Required: Boolean

c.    boolean b = True;     (Incorrect)

Here assigned value is not in small case. Hence, it is an incorrect assignment.

Compile Time Error:
Cannot find symbol
Symbol: Variable True
Location: Class <Classname>

d.    boolean b = “false”;     (Incorrect)

Here we assigned a valid boolean value to a boolean variable inside double quotes. The compiler reads it as a String as it is inside double quotes. Hence, this assignment is incorrect.

Compile Time Error:
Incompatible Types
Found: java.lang.String
Required: boolean

Character Datatype

8.    char

Languages like C/C++ use ASCII characters to represent characters for which 8-bits are enough. That’s why the char size in C/C++ is 1 byte.

But Java uses Unicode Characters to represent characters which is a collection of all characters present all around the world. The number of Unicode characters is “>256” & hence 1 byte is not enough to represent all characters.

Size: 2 bytes
Range: 0 to 65535

Example –

a.    char c = ‘z’;     (Correct)

Here, we assigned a single character to a char variable in single quotes. Hence, this assignment is correct.

b.    char c = “z”;     (Incorrect)

Here, we assigned a single character to a char variable, but in double quotes which represents a string. Hence, this assignment is incorrect

So, being a beginner, this is all you need to know about primitive data types. Hopefully, this will let you understand the use of these data types. If you wanna know more, you can always explore the depths of Java if you like. Goodluck! ;)
 


About  |  Privacy Policy  |  Disclaimer  |  © Copyright 2018. All Rights Reserved.