Humans know the difference between “54321” and “zyxwv”. But a machine does not know it. We use Data Types in computer programming to dictate what a variable or object can hold. We use Data types in all programming languages including C, C++, Java.
Example :
int mySalary = 50000; // number (whole number)
float mySalary = 50000.00f; // number (floating point number)
char firstLetter = 'A'; // character
boolean isOnline = true; // boolean
String channelName = "Programmer Kansabanik"; // String
We can divide Data Types into 2 groups. They are :
- Primitive data types
- Reference data types
Primitive Data Types :
- byte
- short
- int
- long
- float
- double
- boolean
- char
| Data Type | Size | Description |
|---|---|---|
| byte | 1 byte | Store whole numbers |
| short | 2 bytes | Store whole numbers |
| int | 4 bytes | Store whole numbers |
| long | 8 bytes | Store whole numbers |
| float | 4 bytes | Store fractional numbers |
| double | 8 bytes | Store fractional numbers |
| boolean | 1 bit | Store true or false values |
| char | 2 bytes | Store a single character/letter or ASCII values |
Primitive Numbers :
We can divide primitive numbers into two types. They are:
- Integer types (stores whole numbers, positive or negative, without decimals. They are byte, short, int and long. You have to choose the correct data type based on the numeric value.).
- Floating point types (stores decimal or fractional numbers. They are : float and double.).
Note : int (for integer numbers) and double (for floating numbers) is the most used number variables in Java programming language.
Integer Types
Byte
- -2^7 = -128
- 2^7 -1 = 127
Example :
byte exampleOne = -128; //Ok
byte exampleTwo = 127; //Ok
byte exampleThree = 100; //Ok
byte exampleFour = -129; //error
byte exampleFive = 128; //error
Short
- -2^15 = -32,768
- 2^15 -1 = 32767
Example :
short exampleOne = -32768; //Ok
short exampleTwo = 32767; //Ok
short exampleThree = 4000; //Ok
short exampleFive = 32769; //error
short exampleSix = 32768; //error
Int
The int data type can store whole numbers from – 2,147,483,648 to 2,147,483,647.
- -2^31 = -2,147,483,648
- 2^31 -1 = 2,147,483,647
Long :
Note : Add a “L” to avoid any kind of errors in your program while using a long data type for a very long value.
Example :
long exampleOne = 15000000000L; // Ok
long exampleTwo = 15000000000; // error
long exampleThree = 15000; // Ok
/*
Because 15000 is considered here as an int value
for the exampleThree variable.
*/
Floating Point Types
Float :
Note : You should add an “f” at the end of the value in your Java program. Otherwise, you may face errors.
Example :
float myHeight = 5.2f;
//We have to add f at the end for decimal values.
System.out.println(myHeight);
Double :
double data type can store fractional numbers from 1.7e−308 to 1.7e+308. Double data type is a double-precision 64-bit IEEE 754 floating point.
Note : We need to put “d” at the end of the value.
This data is taken from “www.w3school.com”.
Example :
double myDouble = 19.99d;
System.out.println(myDouble);
Boolean :
Example :
boolean bol1 = true;
boolean bol2 = false;
System.out.println(bol1);
System.out.println(bol2);
Char :
char firstLetter = 'A';
System.out.println(firstLetter);
Reference Data Types :
Example (Print a String variable):
String myChannel = "Programmer Kansabanik";
System.out.println(myChannel);
