Data Types
A large part of computer science is handling data. Working with data, storing, fetching and providing data is major part of computer science field.
In this section we are going to learn about data types. Learning data types is a bit boring. But bare with me as we are going to learn some use cases and fun stuff we can do with datas.
Now let us learn about data types. Java has 8 primitive data types which means they are predefined in java.
1. boolean data type
- Boolean data type has two possible values.
true/false
. - Default value
true
boolean flag = true;
2. byte data type
- Byte data type is basically integer values in the range of -128 to 127.
- Default value
0
byte number1 = 24;
3. short data type
- This is also a similar data type which holds integer values between the range of -32768 to 32767.
- Default value
0
short number2 = 24540;
4. int data type
- int data type also carries integer values but in range of -2^31 to (2^31)-1 numbers.
- Default value
0
int number3 = -51432187;
5. long data type
- long data types holds integer values between -2^63 to (2^63)-1 numbers.
- Need to use L after the number digits
- Default value
0
long number4 = 514321874325L;
Note - These are all integer number data types. However, different data types are used to save memory. If we are working on small numbers we can use small range data types accordingly.
6. double data type
- It holds decimal values of double precision 64 bit floating point in IEEE standard form.
- Default value
0.0
double decimal1 = 22.3;
7. float data type
- float holds decimal values of single precision 32 bit floating point. To know more about single precision and double precision click here.
- Default value '0.0f'
float decimal2 = 23.3f;
8. char data type
- It's a 16-bit Unicode character. So, we can use unicode between single quote from '\u0000' to '\uffff'.
char letter = '\u0051';
System.out.println(letter); //prints Q
Unicode value of '\u0051' is Q
- char also carries a single character between single quotes.
char letter1 = 'C';
- char data type also accepts ASCII values.
char letter = 65;
System.out.println(letter); //prints A
- Default value
'\u0000'
.
String data type
There is also another data type called String which we have learned earlier. String data type is not predefined in java. String is an object acts like a data type. Anything written between double quotes is a String data type.
String greeting = "Hello, World!";
Let us quickly look into the code that we can execute in a java file.
class DataTypes {
public static void main(String[] args) {
//boolean data type
boolean flag = true;
System.out.println(flag); //prints true
//byte data type
byte number1 = 24;
System.out.println(number1); //prints 24
//short data type
short number2 = 24540;
System.out.println(number2); //prints 24540
//int data type
int number3 = -51432187;
System.out.println(number3); //prints -51432187
//long data type
long number4 = 514321874325L;
System.out.println(number4); //prints 514321874325
//double data type
double decimal1 = 22.3;
System.out.println(decimal1); //prints 22.3
//float data type
float decimal2 = 23.3f;
System.out.println(decimal2); //prints 23.3
//char data type
char letter1 = 'C';
System.out.println(letter1); //prints C
char letter2 = '\u0051';
System.out.println(letter2); //prints Q
char letter3 = 65;
System.out.println(letter3); //prints A
//String data type
String greeting = "Hello, World!";
System.out.println(greeting); //prints Hello, World!
}
}
Appendix
- Primitive data types starts with lowercase letter and objects like String starts with uppercase letter following lowercase letters.
int number = 10;
String name = "Nihal";
- In a String data type, if we want to use double quotes inside a String, we can use a back slash before the quotes to count them as a String.
String work = "I am a \"Java\" developer.";
System.out.println(work); //prints I am a "Java" developer.
Happy Coding
Top comments (0)