Java Data Types Example

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:

TypeSizeDefault ValueExample
byte1 byte0byte age = 25;
short2 bytes0short salary = 15000;
int4 bytes0int population = 100000;
long8 bytes0Llong distance = 123456789L;
float4 bytes0.0ffloat price = 19.99f;
double8 bytes0.0ddouble pi = 3.14159;
char2 bytes'\u0000'char grade = 'A';
boolean1 bitfalseboolean 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

  1. Create a program to calculate the area of a circle using a double for radius and area.
  2. Write a program to store your initials in a char variable and display them.
  3. Use an array to store the first five even numbers and print them.
  4. Create a program to check if a number is positive, negative, or zero using int and boolean 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! 🚀

Leave a Comment