Java Data Types

Welcome to The Coding College, your trusted resource for mastering programming concepts! In this tutorial, we’ll explore Java Data Types, one of the core aspects of Java programming. By understanding data types, you can store, manipulate, and operate on data efficiently in your programs.

What Are Data Types in Java?

In Java, data types specify the type of data that a variable can hold. They are categorized into two main groups:

  1. Primitive Data Types: Predefined by the language (e.g., int, char).
  2. Non-Primitive Data Types: User-defined or derived types (e.g., String, Array).

1. Primitive Data Types

Java has 8 primitive data types:

Data TypeDescriptionSizeExample
byteInteger (-128 to 127)1 bytebyte b = 100;
shortInteger (-32,768 to 32,767)2 bytesshort s = 5000;
intInteger (-2^31 to 2^31-1)4 bytesint num = 100000;
longLarge integer (-2^63 to 2^63-1)8 byteslong l = 100000L;
floatDecimal (up to 7 digits)4 bytesfloat f = 10.5f;
doubleDecimal (up to 15 digits)8 bytesdouble d = 99.99;
charSingle character2 byteschar c = 'A';
booleanTrue or false1 bitboolean b = true;

Example: Using Primitive Data Types

public class PrimitiveDataTypes {
    public static void main(String[] args) {
        byte age = 25;
        int salary = 50000;
        double height = 5.9;
        char grade = 'A';
        boolean isPassed = true;

        System.out.println("Age: " + age);
        System.out.println("Salary: " + salary);
        System.out.println("Height: " + height);
        System.out.println("Grade: " + grade);
        System.out.println("Passed: " + isPassed);
    }
}

Output:

Age: 25  
Salary: 50000  
Height: 5.9  
Grade: A  
Passed: true

2. Non-Primitive Data Types

Non-primitive data types are created by the programmer. They include:

  • String: Represents a sequence of characters.
  • Array: Stores multiple values of the same type.
  • Classes: User-defined blueprints for objects.
  • Interfaces: Define methods that a class must implement.

Example: Using Non-Primitive Data Types

public class NonPrimitiveDataTypes {
    public static void main(String[] args) {
        String name = "The Coding College";
        int[] numbers = {1, 2, 3, 4, 5}; // Array
        System.out.println("Name: " + name);

        System.out.print("Numbers: ");
        for (int num : numbers) {
            System.out.print(num + " ");
        }
    }
}

Output:

Name: The Coding College  
Numbers: 1 2 3 4 5

Key Differences: Primitive vs Non-Primitive

FeaturePrimitiveNon-Primitive
Predefined/CustomPredefined by JavaCreated by programmers
Stored in MemoryStored directly in memoryStores reference to the memory
Exampleint, char, booleanString, Array, Class
Null AllowedCannot be nullCan be null

Type Conversion in Java

1. Implicit Type Casting (Widening Conversion)

Java automatically converts smaller types to larger types.

Example:

int num = 10;
double decimal = num; // int to double
System.out.println("Decimal: " + decimal);

2. Explicit Type Casting (Narrowing Conversion)

Larger types can be converted to smaller types manually.

Example:

double decimal = 99.99;
int num = (int) decimal; // double to int
System.out.println("Number: " + num);

Practice Exercise

  1. Declare variables for all primitive data types and print their default values.
  2. Write a program to store and print the names of 5 programming languages in an array.
  3. Perform type conversions between int, double, and float.

Conclusion

Understanding Java data types helps you manage memory and perform operations effectively. Both primitive and non-primitive types have their uses, and mastering them is essential for becoming a proficient Java programmer.

For more tutorials, visit TheCodingCollege.com, where learning is tailored for your success. Keep coding and growing! 🚀

Leave a Comment