Java Tutorial Data Types


In Java, data types are divided into two main categories: primitive data types and reference data types. Each data type serves a different purpose and has its own characteristics. Understanding these data types is fundamental for effective programming in Java.

1. Primitive Data Types

Primitive data types are the most basic data types built into the Java language. They represent simple values and are not objects. Java has eight primitive data types:

Data TypeSizeDescription
byte8 bitsRepresents integer values from -128 to 127.
short16 bitsRepresents integer values from -32,768 to 32,767.
int32 bitsRepresents integer values from -2,147,483,648 to 2,147,483,647.
long64 bitsRepresents integer values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
float32 bitsRepresents single-precision floating-point numbers.
double64 bitsRepresents double-precision floating-point numbers.
char16 bitsRepresents a single 16-bit Unicode character.
boolean1 bitRepresents one of two values: true or false.

Examples of Primitive Data Types

public class PrimitiveDataTypes { public static void main(String[] args) { byte aByte = 100; // Example of byte short aShort = 10000; // Example of short int anInt = 100000; // Example of int long aLong = 100000L; // Example of long float aFloat = 10.5f; // Example of float double aDouble = 20.99; // Example of double char aChar = 'A'; // Example of char boolean aBoolean = true; // Example of boolean // Output the values System.out.println("Byte: " + aByte); System.out.println("Short: " + aShort); System.out.println("Int: " + anInt); System.out.println("Long: " + aLong); System.out.println("Float: " + aFloat); System.out.println("Double: " + aDouble); System.out.println("Char: " + aChar); System.out.println("Boolean: " + aBoolean); } }

2. Reference Data Types

Reference data types, on the other hand, refer to objects and are created using classes. They are not predefined and can be defined by the programmer. Common reference data types include:

  • Strings: Represent a sequence of characters and are created using the String class.
  • Arrays: Used to store multiple values of the same data type.
  • Classes: Define a blueprint for creating objects that contain both data (fields) and methods.
  • Interfaces: Abstract types that allow you to define methods that can be implemented by classes.

Examples of Reference Data Types

public class ReferenceDataTypes { public static void main(String[] args) { // Example of String String greeting = "Hello, World!"; // Example of Array int[] numbers = {1, 2, 3, 4, 5}; // Example of a Custom Class Person person = new Person("Alice", 25); // Output the values System.out.println("Greeting: " + greeting); System.out.print("Numbers: "); for (int number : numbers) { System.out.print(number + " "); } System.out.println("\nPerson: " + person.getName() + ", Age: " + person.getAge()); } } // Custom class example class Person { private String name; private int age; // Constructor public Person(String name, int age) { this.name = name; this.age = age; } // Getter methods public String getName() { return name; } public int getAge() { return age; } }

3. Type Conversion and Casting

Java supports type conversion, which is the process of converting one data type to another. There are two types of conversion:

  • Widening Conversion: Automatically converting a smaller data type to a larger data type (e.g., int to long). This is done implicitly by the compiler.

    int intValue = 100; long longValue = intValue; // Widening conversion
  • Narrowing Conversion: Explicitly converting a larger data type to a smaller data type (e.g., double to int). This requires casting.

    double doubleValue = 10.5; int intValue = (int) doubleValue; // Narrowing conversion (casting)

Conclusion

In summary, understanding data types in Java is crucial for effective programming. Primitive data types represent basic values, while reference data types allow for more complex data structures. Mastering these concepts will enable you to write efficient and robust Java applications.