In this section we will learn what data types are and how to use them in Java.
What is Data Type in Java? And Why There Are Data Types in Java?(Variables Type)
When you create a variable, a portion of the memory will be set aside for that variable. But the question is how large this memory space should be and what can we store in that variable?
This is where data types come in.
When creating a variable, first and foremost we declare its data type.
There are two reasons why a variable should have a data type:
- It helps the compiler to know how much memory space it should set aside for that particular variable:
Let’s say a program needs to work with integer values. And also we know that the values that the program might need is in a range from 0 to 2000. Armed with this knowledge, we can create a variable that is of type short.
Note: you’ll learn about short data type later in this section but for now just remember that the compiler will set aside enough memory space for a variable of type short to be able to store values range from -32,768 to 32,767.
We could also use a data type like int but because this data type takes more memory space than a data type like short, we decided to go with short data type instead.
So data types signal the compiler how much memory space should be set aside for a variable.
- The compiler and developers know what type of data are allowed to be stored in that variable:
As mentioned before, data has different types and that’s why we have multiple data types in Java. Now if there’s a variable in a program, how can developers and the compiler know what type of value should be stored in that specific variable? This is where data types can help.
By looking at the data type of that variable, we can decide what type of value should be stored in the variable.
For example, a variable of type int only takes integer value. If we attempt to store float values (like 3.4 or 10.542 etc.) we will get compile time error because we’re trying to store a value of different type compared to the type of the variable.
Now that we have a general view of data-types let’s see how many data types are in Java.
Java Data Types Categories
Generally speaking, there are two categories of data types supported in Java language:
- Custom data types (AKA non-primitive data types): those data types that are created via classes, enums and interfaces.
- Primitive data types: these types are prebuilt in Java and their size and use cases are already declared.
Note: there is also compound data type like arrays that is covered in later section.
In this section we will focus on basic data types and custom data types will be covered in later sections.
Primitive Data Types
Here’s the list of basic data types supported in Java language:
Java Numbers
There are two types of primitive numbers in Java:
Integer types: Those types of numbers that don’t have decimals are considered as integer numbers. For example: -213 or 23 is an integer number.
In Java we have these data types for integer numbers:
byte, short, int, long.
Floating point types: Floating point numbers represent numbers with fractional part and basically they have one or more decimals in them. For example, 2.3, 2342.243, -532.43.
In Java there are two data types that are used for floating data types: float and double.
Java Integer Types
In this section we will see those data types in Java that are created to support integer values.
Java Byte
This is a data-type that accepts integer values except the range of the values we can set for a variable of type byte is limited.
Java byte Syntax:
byte variable_name;
Java byte Size:
The memory size of a short variable is 1 bytes
Java byte Values:
The range of values we can set for a variable of type byte is from -127 to 127.
Java byte Default Value:
The default value of a variable of type byte is 0.
Java byte Data type Example:
byte number = 120; byte number = -100;
Java Short
This is a data-type that accepts integer values just like byte datatype except the range of the values we can set for a variables of type short are higher compared to byte data-type.
Java short Syntax:
short variable_name;
Java short Size:
The memory size of a short variable is 2 bytes.
Java short Values:
The range of values we can set for a variable of type byte is from -32,768 to 32,767.
Java short Default Value:
The default value of a variable of type short is 0.
Java short data type Example:
short number = 1000; short number = 30000;
Java Int
A number that does not have fraction part is integer. For example, 1, 200, -300 etc. are integer and values like 2.3, 4.4, and 543.023 are not. Using the int datatype we can create variables that are able to store integer values.
Java int Syntax:
int variable_name;
Java int Size:
A variable of type int will take 4 bytes of memory space.
Java int Values:
When a variable is typed int, the amount of space that will be assigned to this variable is big enough to hold the range of values from -2,147,483,648 to 2,147,483,647.
Java int Default Value:
The default value of a variable of type int is 0.
Java int data type Example:
int number = 2000000; int number = 3220;
You might be wondering what will happen if we surpass the limits and assign a value higher or lower than the range of int data-type.
For example:
public class Simple { public static void main(String[] args){ int number = 2147483648; System.out.println(number); } }
Output:
Error: integer number too large
In such case the compiler will simply return error instead of compiling.
Java Long
This data-type also is used for integers numbers (just like int) but compared to int data-type, a variable that is typed long can hold higher range of numbers.
Java long Syntax:
long variable_name;
Java long Size:
The memory space of a variable of type long is 8 bytes.
Java long Values:
The range of values that we can set for a variable of type long is from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
Java long Default Value:
The default value of a variable of type long is 0L.
Java long data type Example:
long number = 1000000000L; long number = 2500000000L;
Note: at the end of a long value we put the L character.
Java Floating Point Types
In this section we will learn those data types that are used to create variables capable of storing numbers with fraction.
Java Float
The float is a data-type that allows numbers with fraction-part to be stored in a variable of this type. For example, 5.32 or -534.32 are types of numbers that could be stored in a variable of type float.
Note: We should end a value of float type with the character f.
Java float Syntax:
float variable_name;
Java float Size:
The memory space of a variable typed float is 4 bytes.
Java float Values:
A variable typed float has enough memory space that can store values with 7 significant digits.
Java float Default Value:
The default value of a variable of type float is 0.0f.
Java float data type Example:
float fVariable = 123.4567f; float fVar = 1234.321f; float fVar2 = 1.23456f;
Java Double
The double is another data-type that allows numbers with fraction-part to be stored in the variables of this type.
The main difference of this datatype compared to float is that the double datatype is capable of storing higher range of values.
Java double Syntax:
double variable_name;
Java double Size:
The memory space of a variable typed double is 8 bytes.
Java double Values:
A variable typed double has enough memory space that can store values with 15 significant digits.
Java double Default Value:
The default value of a variable of type double is 0.0d.
Java double data type Example:
double dVar1 = 1234567890.12345; double dVar2 = 1.23456789012345; double dVar3 = 12.3456789012345;
Java Boolean
This data-type is used to create a variable that can hold the keywords true or false.
Boolean values are mostly used in conditional statements like if-statement, while-loop, do-white loop etc.
These statements basically have a block of code and a condition. Their conditions first will be checked and if the result is true, in that case only the body of the statement will be executed. Otherwise it will be ignored.
Java boolean Syntax:
boolean variable_name;
Java boolean Size:
The size of a variable of type Boolean is one byte.
Java boolean Values:
The value of a variable of type Boolean is either the keyword true or false.
Java boolean Default Value:
The default value of a variable of type boolean is false.
Java boolean data type Example:
public class Simple { public static void main(String[] args){ boolean f = false; boolean t = true; System.out.println(f+ " "+ t); } }
Output: false true
In later sections you’ll learn more about the Boolean datatype
Java Characters
If for any reason in a program we needed to store a character, we can use this data-type. Characters can be letters, numbers, and punctuation marks. But remember, to handle characters, computers use numerical codes in which certain integer numbers represent certain characters. And for this reason, characters are stored as integers in memory when assigned to a variable that is typed char.
For example, an integer value 65 represents the character A.
Java char Syntax:
char variable_name;
Java char Size:
The memory space for a variable of type char is 2 bytes.
Java char Values:
The value of a variable of type char can be letters, numbers, and punctuation marks. But they should be put in a pair of single quotation mark. Also numbers range from 0 to 65535 could be used for a variable of type char. If you set numbers as the value, then you don’t need to use a pair of single quotation mark.
Java char Default Value:
The default value of a variable of type char is ‘u0000\’
Java character data type Example:
char character ='A'; char character2 =65;
In the example above, even though it seems each variable has different value, behind the scene the real stored value in the memory for both variables will be the binary code of 65.
Notes:
- Any character that we assign to this type of variables should be between two single quotations ‘ ‘ like the way example above showed.
- If you decided to use numbers instead of characters as the value of char variables, you should know that the range is only from 0 to 65535. Also there’s no need to use single quotation mark if you’re using the number code of a character. But if you want to store the number itself into a variable of type char, then in that case you need to use single quotations as well.
Java Char Datatype Example:
public class Simple { public static void main(String[] args){ char cNumber1 = 1; char cNumber2 = '1'; System.out.println(cNumber1); System.out.println(cNumber2); } }
Output:
☺, 1
The variable named cNumber1 stored the value 1 which is the numerical code of smiley face but the cNumber2 stored the character-value 1 because we put this value within two single-quotation.
So when the program ran we got the smiley face as the value stored in the cNumber1 and for the value stored in the cNumber2 we got 1.
Example: char data type in Java
public class Simple { public static void main(String[] args){ char c = 3000; System.out.println(c); } }
Output: ஸ
Java String
String data type is used to create variables that can store string of characters.
For example, your program might need to store the name of a product or a human or an animal in the memory! This is where you can use a variable of type String and store such name in there.
Java String Syntax:
String variable_name;
Java String Size:
The length of a string data type is between 0 to 2147483647. This means you can have an empty string up to 2,147,483,647 characters.
Java String Values:
The value of a string datatype is a set of characters that are surrounded with a pair of double quotation mark.
Notes:
- In order to store character string into a variable we use double quotation ” “.
- String data type is non-primitive data type because it refers to an object. That means a String object has methods that can perform certain operation.
Note: we will explain the String data type in more details in later section.
Java String Default Value:
The default value of a variable of type String is the keyword null which means an empty string variable.
Java String data type Example:
public class Simple { public static void main(String[] args){ String name = "John"; String lastName = "Doe"; System.out.println(name + " "+ lastName); } }