Java Basic Concepts

Coming Tutorials

>Home>Java Tutorial>Data Types in Java

Java Tutorial

Basic Concepts

Object Oriented Concepts

Coming Tutorials



Primitive Data Types in Java

The data types represent type of data, the data types can be used in variable declaration and return type of a method. The byte, short, int, long, float, double, char, boolean are the basic primitive data types.

The valid Java data type hierarchy is given below.



Data Type Size(byte) Range Corresponding Wrapper class Default Value
byte 1 -128 to 127 Byte 0
short 2 -32768 to 32767 Short 0
int 4 -2147483648 to 2147483647 Integer 0
long 8 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Long 0
float 4 -3.4e38 to 3.4e38 Float 0.0
double 8 -1.7e308 to 1.7e308 Double 0.0
char 2 0 to 65535 Char 0 or \u0000
boolean NA NA(true/false) Boolean false

byte data type

Size: 1 byte or 8 –bits

Minimum value: -128

Maximum value: 127

The most significant bit is called “Sign bit”. 0 means positive value and 1 means negative value.

Positive numbers represented directly in the memory where as negative numbers represented in 2`s complement form.

Example:


byte b=100;// valid

byte b=127;// valid

byte b=130;// compile time exception; Possible loss of precision, found : int. Required : byte.

byte b=123.456;// compile time exception; Possible loss of precision. Found: double. Required : byte.

byte b=true;//  compile time exception; incompatible types. Found :Boolean. Required : byte.

byte b="Topper Skills";// compile time exception; Incompatible types. Found:java.lang.String. Required : byte.

The byte data type is best suitable if you want to handle data in terms of streams either from the file or from the network.


short data type

Size: 2 bytes or 16 bits.

Minimum value: -32768

Maximum value: 32767

Example:


short s=32767;// valid

short s=-32768;// valid

short s=32768;// compile time exception; Possible Loss of Precision

short s=123.456;// compile time exception; Possible Loss of precision. Found: double. Required: short.

short s=true;//  Compile time exception; Incompatible types. Found: Boolean. Required: short.

The short is the very rarely used data type in Java language.

The short data type is the best suitable if we are using 16 – bit processors like 8086 but these processors are outdated and hence corresponding short data type is not is much useful now.


int data type

The int is the most commonly used datatype in Java language.

Size: 4 bytes.

Minimum value: -2147483648

Maximum value: 2147483647

In C language the size of the int is varied from platform to platform, for 16 – bit processor it is 2 – byte but for 32 – bit processor it is 4 bytes.

The main advantage of this approach is, we can perform read and write options very efficiently and performance will be improved. But the main disadvantage of this approach is the chance of failing C program is very high if we are changing platform, hence C is not considered as robust.

But in Java the size of int is always 4 – bytes irrespective of any platform. The main advantage of this approach is the chance of failing java program is very less even if we are changing the underlying operating system.


long data type

Size: 8 bytes.

Minimum value: -9,223,372,036,854,775,808

Maximum value: 9,223,372,036,854,775,807

Whenever int is not enough to hold big values then we should use long data type.

Example:

1. To represent the amount of distance travelled by light in 1000 days, int is not enough then we should use long data type.

2. To count the number of characters present in a big file, int may not enough then we should use long data type.

Note: All the above data types(byte, short, int, long) are used to represent whole values. If we want to represent real numbers then we should go for floating point data types.


float data type

Size: 4 bytes.

Minimum value: -3.4e38

Maximum value: 3.4e38

If we want 5 to 6 decimal places of accuracy then we should go for float data type. The float allows single precision.

To use float data type we need to add f or F at the end of value because by default floating point data is considered of double type.


double data type

Size: 4 bytes.

Minimum value: -1.7e308

Maximum value: 1.7e308

If we want 14 to 15 decimal places of accuracy then we should use double data type. The double data type follows double precision.


boolean data type

Size: Not Applicable( The virtual machine dependent)

Range: Not Applicable ( but allowed values are true /false)

Example:


boolean b=true;// valid

boolean b=0;// //compile time exception: incompatible types. Found: int. Required: boolean.

boolean b=True;// compile time exception; Cannot find symbol.

  boolean True=true;// valid.
  boolean b= True;// valid.
  System.out.println(b);// true

boolean b="false";// compile time exception; incompatible types.

Note: The only allowed values for the boolean data types are “true” or “false” where case is important.


char data type

Size: 2 bytes.

Range: 0 to 65535. (\u0000 to \uffff)

In languages like C and C++ you can use only ASCII characters and to represent all ASCII characters 8 bits are enough, therefore in C language char size is 1 byte.

In java we can use Unicode characters which covers worldwide all alphabets set. The number of Unicode characters are greater than 256 therefore 1 byte is not enough to represent all the characters hence the size of char in the Java is 2 bytes.

Share the article to help your friends