Understanding Primitive Types in Java: A Beginner's Guide to Data Types


Data Types in Java

Data Types in Java

What data type means?

    When you do programming it doesn't matter which language you choose, you will come across this data type and it is essential. But why do we need this?
    We have different types of data. Take your name, which type of data it is? It is a collection of characters. Right? What is your age? It is in a positive integer. Right? We have different forms of data and for each, we need different types of data types to store it. That is why we have the following data types in Java.

1. byte

    In byte, you can store from -128 to 127. 
    Suppose you want to print a table of 10 where you don't need any huge number. In this case, the short data type will be a great choice.
public class DataTypes {
public static void main(String[] args) {
byte noOfSubject = 10;
}
}

2. short

    This data type can store values ranging from -32,768 to +32,767.  
public class DataTypes {
public static void main(String[] args) {
short price = 30000;
}
}

3. int

    This data type is an integer type that stores values ranging from -2,147,483,648 to +2,174,483,647. It's a huge number! Values can be positive or negative.
public class DataTypes {
public static void main(String[] args) {
int housePrice = 1500000;    
}
}

4. long

    This data type is the same as above but has more capacity for storing values ranging from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.
public class DataTypes {
public static void main(String[] args) {
long peopleLiveInCity = 10245135;
}
}

5. float

    In this data type, we can store decimal values also like 123.02. This data type stores values ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
public class DataTypes {
public static void main(String[] args) {
float noOfTransaction = 50541254;
}
}

6. double

    In this data type, we can store values ranging from 4.94065645841246544e-324d to 1.79769313486231570e+308d. 
public class DataTypes {
public static void main(String[] args) {
double temperature = 30.10;
}
}

7. char

    This data type is just any literal it can be a letter or any number. 
    Suppose you have to take the division of the student. In this case, the char data type will be used. Look at the following example.
public class DataTypes {
public static void main(String[] args) {
double classDivision = 'A';
}
}

8. boolean

    This data type just has two values that are either true or false. The default value is false.
    Boolean is always used where there are only two outcomes are expected. Either this or that. 

public class DataTypes {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean doYouLikeCoding;
System.out.println("Do You Like Coding?");
doYouLikeCoding = scanner.nextBoolean();
System.out.println("Answer: " + doYouLikeCoding);
}
}

Output:
Do You Like Coding?
true
Answer: true

    What if you entered a different value than expected? In this case, you will get an error and your program will crash.

    I hope this blog has helped you understand the different data types in Java and how to use them effectively in your code. Share it with others who might find it useful, and happy coding!

Comments