In Java, primitive data types are the most basic types of data. There are several different types, each serving a specific purpose.
Primitive Types:
int: The int data type is used to store integers, which are whole numbers without a decimal point.
int number = 10;
This data type is used when you need to perform operations with whole numbers, such as counting or indexing.
double: The double data type is used to store decimal numbers. It's great for precision in calculations where fractions are involved.
double pi = 3.14159;
Use double when you need to store numbers with decimals, such as measurements or calculations that require accuracy.
boolean: The boolean data type stores only two possible values: true or false. It's typically used in conditional statements to make decisions in the code.
boolean isJavaFun = true;
Use boolean when you need a simple yes/no or true/false condition.
char: The char data type is used to store a single character, like a letter, number, or symbol. Char values are surrounded by single quotes.
char grade = 'A';
This data type is useful when you need to handle individual characters, such as a letter grade or a single digit.
Non-Primitive Type:
String: Although not a primitive data type, String is a special kind of data in Java that is used to store text. Strings are sequences of characters enclosed in double quotes.
String greeting = "Hello, World!";
Strings can contain letters, numbers, and symbols, and they are commonly used for displaying text and handling user input.
Try it Yourself
Now fix the non-examples for each data type in the code editor below.