Java Fundamentals

Data Types in Java

by Passion2Code

Data Types in Java are like the building blocks of your programs, helping you work with numbers, characters, and objects with ease. They’re essential for storing and managing information effectively, and understanding them will make your coding smoother and more efficient.

Data Types in Java are like the building blocks of your programs, helping you work with numbers, characters, and objects with ease.

Java Data Types: An In-Depth Guide


Every program relies on data, so knowing how to classify, store, and work with it is key to writing good code.

Different Types of Data

Before we explore Java data types, it’s important to grasp the different types of data that a program might need to handle.

Integer

  • These are numbers without a decimal point.
  • These can be either positive or negative.
    • Example: 99, 120

Decimal

  • These are the numbers with decimal points.
  • These can be either positive or negative.
    • Example: 0.5, 9.5

Character

  • It can be an Alphabet, Digit, or Symbol but only one and that should be enclosed between single quotes.
    • Alphabet: A-Z, a-z
    • Digit: 0-9
    • Symbol: @, $
    • Example: ‘7’, ‘a’, ‘@‘

String

  • It is a sequence of characters that should be enclosed between double quotes.
    • Alphabet: A-Z, a-z
    • Digit: 0-9
    • Symbol: @, $
    • Example: “Passion2Code”

Data Types in Java

Data types in Java are essential for effectively managing and storing data in your programs. They define what kind of data can be held in a variable, the operations that can be executed on that data, and the amount of memory allocated for it. Java offers both primitive and non-primitive data types, and understanding these basics, along with real-world examples, will help you handle data effectively in your programs.

Primitive Data Types

Primitive data types serve as the fundamental components for data manipulation in Java. They represent basic, individual pieces of data.

Java offers eight of these types:

  1. byte 
  2. short
  3. int
  4. long
  5. long
  6. float
  7. double
  8. char
  9. boolean
Byte
  • Description: An 8-bit signed integer.
  • Range: -128 to 127.
  • Default Value: 0.
  • Use Case: Useful for saving memory in large arrays when values fall within the range.
Example:

byte b = 10;
System.out.println("Byte value: " + b);

Short
  • Description: A 16-bit signed integer.
  • Range: -32,768 to 32,767.
  • Default Value:  0.
  • Use Case: Used in applications where memory optimization is needed, but a larger range than byte is required.
Example:

short s = 30000;
System.out.println("Short value: " + s);

Int
  • Description: A 32-bit signed integer.
  • Range: -2,147,483,648 to 2,147,483,647.
  • Default Value: 0.
  • Use Case: The most commonly used data type for whole numbers.
Example:

int i = 100000;
System.out.println("Int value: " + i);

Long
  • Description: A 64-bit signed integer.
  • Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  • Default Value: 0L.
  • Use Case: Used when larger integer values are required.
Example:

long l = 1000000000000L;
System.out.println("Long value: " + l);

Float
  • Description: A 32-bit IEEE 754 single-precision floating-point number.
  • Range: Approximately ±3.40282347E+38 (7 decimal digits).
  • Default Value: 0.0f.
  • Use Case: Useful for saving memory in larger arrays of decimal numbers.
Example:

float f = 5.75f;
System.out.println("Float value: " + f);

Double
  • Description: A 64-bit IEEE 754 double-precision floating-point number.
  • Range: Approximately ±1.7976931348623157E+308 (15 decimal digits).
  • Default Value: 0.0d.
  • Use Case: Used for decimal numbers requiring greater precision.
Example:

double d = 19.99;
System.out.println("Double value: " + d);

Variables in Java are like containers that store data values, making them essential for performing calculations, holding temporary results, and enabling dynamic application behavior.
Variables in Java
Boolean
  • Description: Stores logical values: true or false.
  • Default Value: false.
  • Use Case: Commonly used in control statements to decide program flow.
Example:

boolean isJavaFun = true;
System.out.println("Is Java Fun? " + isJavaFun);

Char
  • Description: A 16-bit Unicode character.
  • Use Case: Used to store a single character.
Example:

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

Differences
Data TypeSizeRangeUsage
byte
8-bit

-128 to 127

Ideal for saving memory in large arrays
short16-bit-32,768 to 32,767Used when memory is a concern
int32-bit-2,147,483,648 to 2,147,483,647Commonly used for numerical operations
long64-bit-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807Used for large numerical values
float32-bitApprox. ±3.40282347E+38 (7 decimal digits)Used for fractional numbers with less precision
double64-bitApprox. ±1.7976931348623157E+308 (15 decimal digits)Used for high-precision calculations
char16-bitSingle Unicode characterRepresents a single character
boolean1-bittrue or falseRepresents true/false values

Non-Primitive Data Types

Non-primitive data types, also known as reference types, represent objects and complex data structures.

Objects
  • Description: Instances of classes that can hold multiple fields and methods.

Example:

class Person {
    String name;
    int age;
    public String getDetails(String name, int age){
      return name + " - " + age
    }
}
Person person = new Person();

Strings
  • Description: Represents a sequence of characters.

Example:

Example:

String greeting = "Hello, Java!";

Arrays
  • Description: Used to store multiple values of the same type in a single variable.

Example:

int[] numbers = {1, 2, 3, 4};
System.out.println("First element: " + numbers[0]);

Differences
Data TypeDescriptionExample Code
StringRepresents a sequence of charactersString greeting = “Hello, Java!”;
ArrayStores multiple values of the same typeint[] numbers = {1, 2, 3, 4, 5};
ObjectInstance of a classCar myCar = new Car();

Practical Examples

Example: Primitive Data Types

public class DataTypesDemo {
    public static void main(String[] args) {
        int age = 30;
        double salary = 75000.50;
        char initial = 'J';
        boolean isEmployed = true;

        System.out.println("Age: " + age);
        System.out.println("Salary: " + salary);
        System.out.println("Initial: " + initial);
        System.out.println("Employed: " + isEmployed);
    }
}

Example: Non-Primitive Data Types

class Student {
    String name;
    int age;
    double grade;

    // Constructor
    Student(String name, int age, double grade) {
        this.name = name;
        this.age = age;
        this.grade = grade;
    }

    // Method to display student details
    void displayDetails() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Grade: " + grade);
    }
}

public class NonPrimitiveExample {
    public static void main(String[] args) {
        // Creating an object of the Student class
        Student student1 = new Student("Alice", 20, 8.5);
        Student student2 = new Student("Bob", 22, 9.0);

        // Displaying details of each student
        System.out.println("Student 1 Details:");
        student1.displayDetails();

        System.out.println("Student 2 Details:");
        student2.displayDetails();
    }
}

Summary

  • Understanding Java data types is essential for anyone looking to become a skilled Java developer.
  • By learning the distinctions between primitive and non-primitive types, you can create efficient and reliable code that meets the specific requirements of your applications.
  • Playing around with these data types will not only improve your coding abilities but also get you ready for more complex programming tasks.
  • With a solid understanding of Java data types under your belt, consider putting your knowledge to the test by developing real-world applications.
  • The opportunities are limitless, and each project will take you one step closer to mastering coding!

Must Read

Expand your understanding of Java with these related articles:

Structure of Java Program – Java Syntax
Learn about the structure of a Java program and how to define variables using different data types effectively.

JVM (Java Virtual Machine) – The Heart of Java Execution
Explore how the JVM handles data types during program execution for efficient performance.

Keywords in Java are reserved words with special meanings and functions. They can’t be used as variable names, method names, or identifiers.
Keywords in Java

JVM vs JRE vs JDK – What is the Difference Between JVM, JRE, and JDK?
Understand how these components interact with Java data types to execute your programs.

Execution of Java Program – What is the Process of Execution of a Java Program?
Follow the step-by-step process of how Java programs utilize data types during execution.


FAQ’S

What are non-primitive data types in Java?
Non-primitive data types represent objects and complex structures like Strings, Arrays, and Classes. Instead of storing actual values, they store references to memory locations.

What are the types of data in Java with examples?
Java has two main types of data:

  • Primitive: For simple values (e.g., int age = 25;).
  • Non-primitive: For complex structures (e.g., String name = “Alice”;).

How are primitive and non-primitive data types different in Java?

  • Primitive types store basic values like numbers (int, double, etc.).
  • Non-primitive types store references to objects like Strings (String name = “Hello”;) or arrays (int[] numbers = {1, 2, 3};).

Where can I find a PDF on Java data types?
You can create or download a PDF on Java data types from reliable sources, like official Java documentation or trusted programming websites.

How do JavaScript data types differ from Java?
JavaScript is dynamically typed, meaning variables can hold any type of value, while Java is statically typed, requiring you to define the type (like int, String, etc.) upfront.

What is the int data type in Java?
The int type is used for whole numbers and has a range of -2,147,483,648 to 2,147,483,647.

  • For example: int age = 30;

What is the float data type in Java?
The float type handles decimal numbers and is a 32-bit single-precision type.

  • For example: float price = 19.99f

What are reference data types in Java?
Reference types store memory addresses of objects like Strings, Arrays, or custom Classes, enabling you to handle more complex data structures.

Leave a Comment

Index