What are Data Types in Java?

In this tutorial you will learn about different data types in Java.

Data Types in Java
As we already discussed in previous tutorial that every java variable has a data type. Data type tell us the different sizes and values that can be stored in a variable.

Java has two types of Data Types:
  1. Primitive Data Types
  2. Non-Primitive Data Types
1. Primitive Data Types
Primitive data types are built-in with in Java language. No additional methods are required for it. There are 8 primitive data types:
  1. byte
  2. short
  3. int
  4. long
  5. float
  6. double
  7. boolean
  8. char

Data TypeDefault ValueDefault size
byte01 byte
short02 bytes
int04 bytes
long0L8 bytes
float0.0f4 bytes
double0.0d8 bytes
booleanfalse1 bit
char'\u0000'2 bytes
1. byte : 
It's value range lies between -128 to 127. Byte is used in place of int to save memory as byte is 4 times smaller than integer. It's default value is 0.

Example: 
byte x = 10;
byte y = 20;


2. short
The range of short data type lies between -32,768 to 32,767. Short data type can again be used to save memory instead of int. Short is 2 times smaller than int. It's default value is 0.

Example:
short s = 12000;
short t = -10000;


3. int
int is also known as integer. It's range value lies between -2,147,483,648 to 2,147,483,647. It's default value is 0. This is the most commonly used data type for integral values.

Example:
int x = 10;
int y = 2000;


4. long
long data type is of 8 bytes. It's value range lies between -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. long data type is used when a large range of values required which are not provided by int. It's default value is 0.

Example:
long l = 100000L;
long m = -300000L;


5. float :
float data type is used for decimal values. It's range is unlimited. float is recommended over double data type to save memory. It's default value is 0.0F.

Example:
float f = 20.5f;


6. double :
double data type is generally used for decimal values just like float. It's range is unlimited. It's default value is 0.0d.

Example:
double d = 10.5;


7. boolean :
boolean  data type can only stores one of the two values: true or false.

Example:
boolean flag = false;
boolean isJavaInteresting = true;


8. char :
The char data type is used to store characters. Character must be with in single quotes ('a').

Example:
char grade = 'A';

2. Non-Primitive Data Types

Non-Primitive data types are not defined by Java and are created by the programmers except Strings. Unlike primitive, non-primitive data types can be null. Non-Primitive data types are:
  1. Strings
  2. Arrays
  3. Classes
  4. Interface etc.
i. Strings :
The String data type is used to store sequence of characters or text. Strings must be declared within double quotes like "Hello".

Example:
String myName = "John"; 

You will learn about other Non-primitive data types in the upcoming tutorials. Click here to find complete Java Tutorials Series


Java Variables   << Previous     ||     Next >>  Java Modifiers


Follow Us

Quora Space | Facebook Page | Telegram Channel
Feel free to ask queries or share your thoughts in comments or email us.

Comments

Popular posts from this blog

10 Best Demo Websites for Automation Testing Practice

Automate 'User Registration' of E-commerce Website using Selenium

Automate Browser Actions on Godaddy.com using Selenium

Selenium WebDriver Browser and Navigation Commands