Data Types in Java.

Java Data Types

Data types define the values a variable can hold in its lifetime. There are basically two types of data types available in Java:

  • Primitive data types: Primitive data types are the most basic data types that hold a scalar value. It includes boolean, char, short, int, byte, long, float, and double. 
  • Non-primitive (Reference) data types: Non-primitive data types are created by programmers that hold an object. It includes Arrays, Classes, and Interfaces.    
List of 8 primitive data types with their byte size and default value:

Data Type Size(in Bytes) Default Value
byte 1 0
short 2 0
int 4 0
long 8 0
float 4 0.0
double 8 0.0
char 2(UNICODE) '\u0000'
boolean depend on JVM false

  • The formula for Signed data type: -2n-1 to 2n-1-1
  • The formula for Unsigned data type: 0 to 2n-1
  • All integer and floating-point data types are Signed.
  • All character data type is unsigned. 

Byte data type: Byte data type value range between -128 to 127. Its default value is 0 and the minimum value it can hold is -128 and the maximum value it can hold is 127. It is used to store the value when memory saving is the most important requirement. 

Example: byte x = 5;

Short data type: Short data type values range between -32,768 to 32,767 and its default value is also 0. It is used to save the memory just like a byte data type. 

Example: short x = 2000;

Int data type: Int data type values range between -231 to 231-1 and its default value is 0. It is used to store integer types of data whose value is there in the given range.

Example: int a = 234534;

Long data type: Long data type values range between  -263 to 263-1 and its default value is 0. It is used when values go out of the integer range.

Example: long a = 100000L; 

Float data type: Float data type values range is unlimited and used to store floating-point number values. It is preferred to use a float data type instead of double when saving memory is important. Its default value is 0.0F. 

Example: float a = 130.0f;

Double data type: Double data type values range is also unlimited and like float, it is also used to store decimal type values. Its default value is 0.0d.

Example: double a  = 23.4;

Char data type: Char data type values are a single 16-bit Unicode character use to store characters. Its range is between '\u0000' to '\uffff'. 

Example: char ch = 'A';

Reference data types.

A reference variable can be used to refer to any object of the declared type or any compatible type. The value of any reference variable is null. 
Example: Employee emp = new Employee();

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS