All modern programming languages needs to have concept of variables. Variables are there in a programming language to store data into. There are 8 data type defined in Java which are inbuilt in java core , they are called Java Primitive Data Types. Each data-type variable has a different purpose and this tutorial will explain how to choose the right datatype.
The eight data-type defined by Java are byte
, short
, int
, long
, float
, double
, char
and boolean
.
Before we go into the description of each variable lets first see a Example Java Code which defines all the primitive data types.
package info.5balloons.examples; public class PrimitiveData { public static void main(String args[]){ //To Store Numeric Value byte byteValue = 5; short shortValue = 500; int intValue = 5000000; long longValue = 500000000000L; //To Store Decimal Value float floatValue = 5.0F; double doubleValue = 5.03232; //To Store Single Character or Unicode char charValue1 = 'A'; char charValue2 = '\u00B9'; //To store true or false value boolean booleanValue1 = true; boolean booleanValue2 = false; //Print Statements System.out.println("Byte Value "+byteValue); System.out.println("Short Value "+shortValue); System.out.println("Int value "+intValue); System.out.println("Long value "+longValue); System.out.println("Float value "+floatValue); System.out.println("Double value "+doubleValue); System.out.println("Char value 1 "+charValue1); System.out.println("Char value 2 "+charValue2); System.out.println("Boolean value 1 "+booleanValue1); System.out.println("Boolean value 2 "+booleanValue2); } }
To store numeric (non decimal) data
1. byte
byte is use hold the numeric data. It should be used when to store a smaller numeric data. The size of byte is 1 byte and it can hold data in range of -128 to 127.
2. short
short is also used to hold the numeric data. The size of short is 2 bytes and it can hold data from range -32,768 to 32,767
3. int
int is the most popular data type in java, and you will be using it most frequently and this should be your go-to choice for storing numeric data if the storage data is not a concern. Reason is given below. Size occupied by a int 4 byte and it contain a data from range -2.1Billion to 2.1Billion (-2,147,483,648 to 2,147,483, 647 to be precise)
4. long
long is used to hold very very large numeric values. The size of long is double of int i.e. 8 byte. and it should be used only when you are sure the value you are going to store is very large and won’t fit into an int data type. Long value should be followed by lowercase l or uppercase L.
When deciding to store a numeric data it’s always better to choose int as a default data type. Reason is java always consider’s fixed number expressions as int. Thus while choosing byte or short you will have to specifically cast your expression to the desired data type. See example below.
int intValue = 20; byte byteValue = 10 + intValue; //Type mismatch : cannot convert byte to int short shortValue = 30 + intValue; //Type mismatch : cannot convert byte to int byte byteValue1 = (byte) (10 + intValue); // Cast to byte short shortValue1 = (short) (30 + intValue); //Cast to short
As you can see the above example while doing operations with intValue you have to specifically cast values to byte as Java converts numeric literals into int.
To store floating point (decimal) data
5. float
float is use to store decimal point data. The default size of a float variable is 4 byte or 32bits. A float value should always be followed by f or F. Same as with byte or short , float should be used when there is a need to save the storage , otherwise double is a better choice.
6. double
double should be the default choice when you need to store the decimal value data. The default size of float is 8 byte or 64 bits. A double value needs to be followed by d or D, But this is not a compulsion by default Java considers decimal value as double.
To store single character
7. char
char is used to store single character. The size of char is 16 bit and can hold unicode character. Char can also be used to store unicode characters. Unicode character table is can be referenced from this table (https://unicode-table.com/en/). For example if you wish to store the copyright symbol in the char datatype you can do it like char ch = '\u00A9'
;
To store true or false data
8. boolean
A boolean data type is used to store either a true or false value. Use this data type to store simple flags for true or false conditions.
Apart from these eight data types there is another type provided by Java i.e. String
. But since String is provided via Class it is not technically a primitive data type. You will learn more about Spring in subsequent posts.
Exercise Questions.
- Create a Java Program to store different values related to an Employee using java data types. That includes employee Id, employee age, employee salary, employee gender (can be ‘M’ or ‘F’) , employee Marital status (true if married and false if not married).
- Create a Java program and define all eight data types outside of main method with static keyword, and don’t initialise them. Print all of them inside your main method and see what default value is assigned to them by Java. (Sample program given below)
public class PrimitiveExample { static char intValue; public static void main(String args[]){ System.out.println(intValue); } }