Welcome to The Coding College! In this guide, we’ll explore examples of all Java data types, helping you understand how to use them effectively. Java provides two categories of data types: Primitive and Non-Primitive.
Primitive Data Types in Java
Java has 8 primitive data types:
Type | Size | Default Value | Example |
---|---|---|---|
byte | 1 byte | 0 | byte age = 25; |
short | 2 bytes | 0 | short salary = 15000; |
int | 4 bytes | 0 | int population = 100000; |
long | 8 bytes | 0L | long distance = 123456789L; |
float | 4 bytes | 0.0f | float price = 19.99f; |
double | 8 bytes | 0.0d | double pi = 3.14159; |
char | 2 bytes | '\u0000' | char grade = 'A'; |
boolean | 1 bit | false | boolean isJavaFun = true; |
Non-Primitive Data Types
Non-primitive types include Strings, Arrays, and Objects. These are reference types used for complex data structures.
Example:
String message = "Hello, Java!";
Examples of Primitive Data Types
Here’s a comprehensive example demonstrating all the primitive data types:
public class DataTypesExample {
public static void main(String[] args) {
// Integer types
byte smallNumber = 127; // Max value of byte
short mediumNumber = 32000; // Example of short
int integerNumber = 100000; // Default for integers
long largeNumber = 123456789L; // Use 'L' for long literals
// Floating-point types
float decimalNumber = 10.75f; // Use 'f' for float literals
double preciseDecimal = 3.14159265359; // More precise decimals
// Character type
char initial = 'J'; // Single character
// Boolean type
boolean isCodingFun = true; // Logical true/false
// Displaying the values
System.out.println("byte: " + smallNumber);
System.out.println("short: " + mediumNumber);
System.out.println("int: " + integerNumber);
System.out.println("long: " + largeNumber);
System.out.println("float: " + decimalNumber);
System.out.println("double: " + preciseDecimal);
System.out.println("char: " + initial);
System.out.println("boolean: " + isCodingFun);
}
}
Output:
byte: 127
short: 32000
int: 100000
long: 123456789
float: 10.75
double: 3.14159265359
char: J
boolean: true
Example: Non-Primitive Data Types
Here’s an example showing how to use a String and an Array:
public class NonPrimitiveExample {
public static void main(String[] args) {
// String example
String greeting = "Welcome to Java!";
System.out.println(greeting);
// Array example
int[] numbers = {10, 20, 30, 40, 50};
System.out.println("First number in the array: " + numbers[0]);
System.out.println("Array length: " + numbers.length);
}
}
Output:
Welcome to Java!
First number in the array: 10
Array length: 5
Practice Exercises
- Create a program to calculate the area of a circle using a
double
for radius and area. - Write a program to store your initials in a
char
variable and display them. - Use an array to store the first five even numbers and print them.
- Create a program to check if a number is positive, negative, or zero using
int
andboolean
types.
Conclusion
Java’s data types form the foundation of programming, enabling efficient and structured code. By practicing these examples, you’ll gain confidence in handling various data types.
For more detailed tutorials, head over to The Coding College and keep advancing your coding journey. Happy learning! 🚀