Topic 1.2 Understanding Variables and Data Types in Java

Covers following:

1.      Identify the most appropriate data type category for a particular specification.

2.      Primitive and Reference Data types

3.      Three primitive data types: int, double and Boolean

4.      Memory associated with a value

5.      When a variable is declared final, its value cannot be changed once it is initialized.

Introduction

In Java, variables are used to store data that can be used and manipulated throughout a program. Each variable has a data type that defines what kind of data it can hold. In this lesson, we will explore the three main primitive data types used in Java: intdouble, and boolean. We will also learn about the final keyword and how it makes variables constant. Additionally, we will look at the difference between primitive and reference data types.

1. What are Variables?

A variable is a named space in memory that stores data. Think of it as a box with a label on it where you can put things. Each box (variable) can hold only one type of item (data type).

Example:

Imagine you have a box labeled "age" where you can store your age, and another box labeled "temperature" to store the temperature outside.

2. Data Types in Java

Java has different data types to specify what kind of data a variable can hold. In this course, we focus on three primitive data types:

int

The int data type is used to store whole numbers (integers).

int age = 16;

double

The double data type is used to store decimal numbers (floating-point numbers).

double temperature = 98.6;

boolean

The boolean data type is used to store true or false values.

boolean isJavaFun = true;

3. Using final Keyword

When a variable is declared with the final keyword, its value cannot be changed once it has been initialized. This makes the variable a constant.

final int daysInWeek = 7;

4. Primitive vs. Reference Data Types

Primitive Data Types

Primitive data types store the actual values. They are predefined by the Java language and are named by reserved keywords.

Reference Data Types

Reference data types store references (addresses) to the actual objects in memory. They are created using defined classes and can be used to call methods to perform operations.

Example:

// Primitive data type example

int number = 10;

 

// Reference data type example

String greeting = "Hello, World!";

In the above examples, number holds the actual integer value 10, whereas greeting holds a reference to the string object "Hello, World!".

Real-World Example

Imagine you are creating a program to track the progress of a video game player. You need to store their age, score, and whether they have completed the game or not.

int playerAge = 18; // Player's age (primitive)

double playerScore = 1500.75; // Player's score (primitive)

boolean gameCompleted = false; // Has the player completed the game? (primitive)

String playerName = "Alex"; // Player's name (reference)

Full Program Example

Here is a simple Java program that demonstrates how to use variables and data types:

public class VariablesDemo {

public static void main(String[] args) {

// Declare an integer variable and initialize it

        int age = 16;

        // Declare a double variable and initialize it

        double temperature = 72.5;

        // Declare a boolean variable and initialize it

        boolean isJavaFun = true;

        // Declare a final variable (constant)

        final int daysInWeek = 7;

        // Declare a reference variable (String) and initialize it

        String greeting = "Hello, Java!";

 

        // Print the values of the variables

        System.out.println("Age: " + age);

        System.out.println("Temperature: " + temperature);

        System.out.println("Is Java Fun: " + isJavaFun);

        System.out.println("Days in a Week: " + daysInWeek);

        System.out.println("Greeting: " + greeting);

 

        // Uncommenting the next line will cause an error because daysInWeek is final

        // daysInWeek = 8;

    }

}

Programming Problem

Create a Java program that declares variables for the following:

Print out the values of all these variables.

Sample Output

Name: John Doe

Favorite Number: 7

Average Grade: 85.5

Completed Homework: true

Days in a Month: 30

Conclusion

Variables and data types are fundamental concepts in programming. By understanding how to declare and use them, you can start creating more complex and functional programs. Practice using intdouble, and boolean data types, and remember that the final keyword can be used to create constants. Also, be aware of the difference between primitive and reference data types. Happy coding!