Getting Started with Java Variables: A Practical Product Example

Last Updated: October 12, 2024


When learning Java, one of the foundational concepts is understanding variables and their various data types. Variables are used to store information that can be referenced and manipulated in your program. Java offers a variety of data types, including both primitive types and reference types.

In this blog, we’ll break down how to use Java variables by creating a simple Products class that simulates a real-world scenario — managing a product in an inventory. This example will demonstrate various Java data types and how they are used in a program.

What Are Java Variables?

In Java, a variable is a container that holds data, such as a number, text, or a true/false value. Each variable must be declared with a data type to define the kind of data it will store. For example, int stores integers, boolean stores true/false values, and String stores text.

Data Types in Java

Java data types fall into two main categories:

  • Primitive Data Types: These are the most basic data types that store simple values:
    • byte, short, int, long (for integer values)
    • float, double (for decimal values)
    • boolean (for true/false values)
    • char (for single characters)
  • Reference Data Types: These refer to objects or arrays. In our example, we’ll use String, which is a reference type for storing sequences of characters.

Real-Time Example: Creating a Products Class

Let’s create a simple class called ProductsJava that holds information about a product, such as its name, price, stock, and category.

Code Breakdown

Here’s the complete Java code for the ProductsJava class, which utilizes multiple data types:

class ProductsJava {
    private byte productId;        // Product ID (byte)
    private String name;           // Product name (String)
    private double price;          // Price (double)
    private int stock;             // Stock available (int)
    private long barcode;          // Barcode number (long)
    private short warranty;        // Warranty period (short)
    private float discount;        // Discount percentage (float)
    private char category;         // Category code (char)
    private boolean isAvailable;   // Is the product available? (boolean)

    // Constructor to initialize all variables
    public ProductsJava(byte productId, String name, double price, int stock, long barcode, short warranty, float discount, char category, boolean isAvailable) {
        this.productId = productId;
        this.name = name;
        this.price = price;
        this.stock = stock;
        this.barcode = barcode;
        this.warranty = warranty;
        this.discount = discount;
        this.category = category;
        this.isAvailable = isAvailable;
    }

    // Method to display product information
    public void displayProductInfo() {
        System.out.println("Product ID: " + this.productId);
        System.out.println("Name: " + this.name);
        System.out.println("Price: " + this.price);
        System.out.println("Stock: " + this.stock);
        System.out.println("Barcode: " + this.barcode);
        System.out.println("Warranty (months): " + this.warranty);
        System.out.println("Discount: " + this.discount + "%");
        System.out.println("Category: " + this.category);
        System.out.println("Available: " + this.isAvailable);
    }

    // Main method to run the program
    public static void main(String[] args) {
        // Creating a product instance with various data types
        ProductsJava product = new ProductsJava(
            (byte) 1,              // Product ID
            "Product One",         // Name
            10.10,                 // Price
            100,                   // Stock
            122233333,             // Barcode
            (short) 24,            // Warranty period
            10.3f,                 // Discount percentage
            'P',                   // Category code
            true                   // Availability status
        );

        // Display product information
        product.displayProductInfo();
    }
}

Explanation of the Java Variables

  • productId (byte): Holds the product's unique identifier. A byte is used because it’s enough to store small numbers (range: -128 to 127).
  • name (String): The product name is a sequence of characters, so we use the String type, which is a reference type in Java.
  • price (double): The price is stored as a decimal value. double is preferred for more precision in monetary values.
  • stock (int): The number of products in stock is an integer, so we use the int type (range: -2^31 to 2^31-1).
  • barcode (long): The product's barcode is a long number, which can exceed the range of int, making long the better choice (range: -2^63 to 2^63-1).
  • warranty (short): The warranty period is a small number, so we use short (range: -32,768 to 32,767).
  • discount (float): We use float to store the discount percentage, as it’s a decimal value but doesn’t require the same level of precision as double.
  • category (char): The product category is represented by a single character (e.g., 'P' for "Perishable"), which makes char the ideal choice.
  • isAvailable (boolean): A boolean is used to indicate whether the product is available (true) or not (false).

Output of the Program

When you run the program, it creates an instance of the ProductsJava class and outputs the following information:

Product ID: 1
Name: Product One
Price: 10.1
Stock: 100
Barcode: 122233333
Warranty (months): 24
Discount: 10.3%
Category: P
Available: true