Can you give examples?

QuestionsQuestions4 SkillsProYour First Java LabSep, 30 2025
0146

Sure! Here are examples of each Java primitive data type:

public class DataTypesExample {
    public static void main(String[] args) {
        // byte example
        byte byteValue = 100;
        System.out.println("Byte value: " + byteValue);

        // short example
        short shortValue = 10000;
        System.out.println("Short value: " + shortValue);

        // int example
        int intValue = 100000;
        System.out.println("Int value: " + intValue);

        // long example
        long longValue = 100000L; // 'L' suffix indicates a long literal
        System.out.println("Long value: " + longValue);

        // float example
        float floatValue = 10.5f; // 'f' suffix indicates a float literal
        System.out.println("Float value: " + floatValue);

        // double example
        double doubleValue = 20.99;
        System.out.println("Double value: " + doubleValue);

        // char example
        char charValue = 'A';
        System.out.println("Char value: " + charValue);

        // boolean example
        boolean booleanValue = true;
        System.out.println("Boolean value: " + booleanValue);
    }
}

Explanation:

  • Each variable is declared with its respective data type.
  • The System.out.println statements print the values of these variables to the console.

How to Run:

  1. Save the code in a file named DataTypesExample.java.
  2. Compile it using: javac DataTypesExample.java
  3. Run it using: java DataTypesExample

This will display the values of each data type in the console. If you have any further questions, feel free to ask!

0 Comments

no data
Be the first to share your comment!