13
DecPrimitive Data Types in Java
Primitive Data Types in Java: An Overview
Primitive Data Types in Java is one of the two types of Data Types, that are Primitive and Non Primitive Data Types in Java, which hold the basic kinds of data in Java Programming Language. In this Java tutorial, we’ll try to understand What are Primitive Data Types in Java with Example Program and What are different types of Java Primitive Data Type. For more tips and guidance to learn Java Programming, try our Java Full Stack Developer Certification.
Java Primitive Data Types
Read More -
1. Boolean Data Type
Syntax
boolean variableName;
Example
public class BooleanExample {
public static void main(String[] args) {
boolean isJavaFun = true;
boolean isCodingHard = false;
System.out.println("Is Java fun? " + isJavaFun);
System.out.println("Is coding hard? " + isCodingHard);
if (isJavaFun) {
System.out.println("Yes, Java is fun!");
} else {
System.out.println("No, Java is not fun.");
}
if (isCodingHard) {
System.out.println("Coding is hard.");
} else {
System.out.println("Coding is not hard.");
}
}
}
Output
Is Java fun? true
Is coding hard? false
Yes, Java is fun!
Coding is not hard.
2. Byte Data Type
Syntax
byte variableName;
Example
public class ByteExample {
public static void main(String[] args) {
byte numberOfStudents = 20;
byte numberOfTeachers = 3;
System.out.println("Number of students: " + numberOfStudents);
System.out.println("Number of teachers: " + numberOfTeachers);
byte totalPeople = (byte) (numberOfStudents + numberOfTeachers);
System.out.println("Total number of people: " + totalPeople);
// Byte data type ranges from -128 to 127
byte valueOutOfRange = (byte) 150;
System.out.println("Value out of range: " + valueOutOfRange);
}
}
Output
Number of students: 20
Number of teachers: 3
Total number of people: 23
Value out of range: -106
3. Char Data Type
Syntax
char variableName;
Example
public class CharExample {
public static void main(String[] args) {
char firstLetter = 'A';
char secondLetter = 'B';
System.out.println("First letter: " + firstLetter);
System.out.println("Second letter: " + secondLetter);
// Char can also hold Unicode characters
char unicodeChar = '\u03A9'; // Greek capital letter omega
System.out.println("Unicode character: " + unicodeChar);
// Char can be used in arithmetic operations (as it's internally represented as
// numbers)
char result = (char) (firstLetter + 1);
System.out.println("Next letter: " + result);
}
}
Output
First letter: A
Second letter: B
Unicode character: Ω
Next letter: B
4. Short Data Type
Just like the Byte Data Type, the Short Data Type is also helpful in saving memory in large arrays where there is a need for memory saving. It is a 16-bit signed two's complement integer. It also has a default value of 0 and can range from -32768 (minimum value) to 32767 (maximum value).
Syntax
short variableName;
Example
public class ShortExample {
public static void main(String[] args) {
short temperatureToday = 25;
short numberOfStudents = 150;
System.out.println("Temperature today: " + temperatureToday + " degrees Celsius");
System.out.println("Number of students: " + numberOfStudents);
short totalNumberOfPeople = (short) (numberOfStudents + 10);
System.out.println("Total number of people: " + totalNumberOfPeople);
// Short data type ranges from -32,768 to 32,767
short valueOutOfRange = (short) 35000; // Corrected to use short and added casting
System.out.println("Value out of range: " + valueOutOfRange);
}
}
On executing the above code in the Java Playground, we'll get the below output.
Output
Temperature today: 25 degrees Celsius
Number of students: 150
Total number of people: 160
Value out of range: -30536
5. Int Data Type
Syntax
int variableName;
Example
public class IntExample {
public static void main(String[] args) {
int numberOfStudents = 100;
int numberOfTeachers = 10;
System.out.println("Number of students: " + numberOfStudents);
System.out.println("Number of teachers: " + numberOfTeachers);
int totalPeople = numberOfStudents + numberOfTeachers;
System.out.println("Total number of people: " + totalPeople);
// Integers can hold very large values
int bigValue = 2147483647;
System.out.println("Big value: " + bigValue);
// Integers overflow if the value exceeds the range
int overflowValue = 2147483647 + 1;
System.out.println("Overflow value: " + overflowValue);
}
}
Output
Number of students: 100
Number of teachers: 10
Total number of people: 110
Big value: 2147483647
Overflow value: -2147483648
6. Long Data Type
The Long Data Type is usually helpful when there is a need for a wide range of values and Int Data Type is not large enough to store them all. Its default value is also 0 and can range from -2^63 (minimum value) to 2^63 -1 (maximum value).
Syntax
long variableName;
Example
public class LongExample {
public static void main(String[] args) {
long populationOfCountry = 1500000000L; // Note the 'L' suffix for long literals
long distanceToMoon = 384400000L;
System.out.println("Population of the country: " + populationOfCountry);
System.out.println("Distance to the moon (in meters): " + distanceToMoon);
long totalDistance = populationOfCountry * distanceToMoon;
System.out.println("Total distance (population x distance to moon): " + totalDistance);
// Long data type can hold even larger values
long bigValue = 9223372036854775807L;
System.out.println("Big value: " + bigValue);
// Long data type can also represent negative values
long negativeValue = -1000000000L;
System.out.println("Negative value: " + negativeValue);
}
}
Output
Population of the country: 1500000000
Distance to the moon (in meters): 384400000
Total distance (population x distance to moon): 576600000000000000
Big value: 9223372036854775807
Negative value: -1000000000
7. Float Data Type
Syntax
float variableName;
Example
public class FloatExample {
public static void main(String[] args) {
float temperatureToday = 25.5f; // Note the 'f' suffix for float literals
float distanceToDestination = 123.45f;
System.out.println("Temperature today: " + temperatureToday + " degrees Celsius");
System.out.println("Distance to destination: " + distanceToDestination + " kilometers");
float totalDistance = temperatureToday + distanceToDestination;
System.out.println("Total distance: " + totalDistance + " units");
// Float data type can represent very large or very small values
float bigValue = 3.4028235E38f; // Maximum positive value for float
float smallValue = 1.4E-45f; // Minimum positive value for float
System.out.println("Maximum positive value for float: " + bigValue);
System.out.println("Minimum positive value for float: " + smallValue);
}
}
Output
Temperature today: 25.5 degrees Celsius
Distance to destination: 123.45 kilometers
Total distance: 148.95 units
Maximum positive value for float: 3.4028235E38
Minimum positive value for float: 1.4E-45
8. Double Data Type
Syntax
double variableName;
Example
public class DoubleExample {
public static void main(String[] args) {
double temperatureToday = 25.5;
double distanceToDestination = 123.45;
System.out.println("Temperature today: " + temperatureToday + " degrees Celsius");
System.out.println("Distance to destination: " + distanceToDestination + " kilometers");
double totalDistance = temperatureToday + distanceToDestination;
System.out.println("Total distance: " + totalDistance + " units");
// Double data type can represent very large or very small values
double bigValue = 1.7976931348623157E308; // Maximum positive value for double
double smallValue = 4.9E-324; // Minimum positive value for double
System.out.println("Maximum positive value for double: " + bigValue);
System.out.println("Minimum positive value for double: " + smallValue);
}
}
Output
Temperature today: 25.5 degrees Celsius
Distance to destination: 123.45 kilometers
Total distance: 148.95 units
Maximum positive value for double: 1.7976931348623157E308
Minimum positive value for double: 4.9E-324
Summary
FAQs
int number = 10;