Built with Tutify.io

    AIStudyLabs

    Java Variables

    Java Variables

    Introduction

    In this lesson, you will work with Java variables: how to declare and initialize them with appropriate data types, how to distinguish between local and global (instance/static) variables, how to apply naming conventions that improve readability, and how to use type casting to convert between data types. You will also see small programs that show these ideas in action.


    What Is a Variable in Java?

    A variable is a named storage location in memory that holds a value while your program runs. Each variable has:

    In Java, every variable must have a declared type.


    Declaring and Initializing Variables

    Basic Syntax

    <data_type> variableName;          // declaration
    <data_type> variableName = value; // declaration + initialization
    

    Examples of declarations and initializations:

    int count = 10;              // integer
    double price = 19.99;        // decimal number
    char grade = 'A';            // character
    boolean isActive = true;     // boolean
    String message = "Hello";    // reference type (class)
    

    Example: Declaring and Initializing Different Types

    public class VariableDemo {
        public static void main(String[] args) {
            int age = 25;                // whole number
            double temperature = 36.6;   // decimal
            boolean isJavaFun = true;    // true/false
            char initial = 'J';          // single character
            String language = "Java";    // text
    
            System.out.println("Age: " + age);
            System.out.println("Temperature: " + temperature);
            System.out.println("Is Java fun? " + isJavaFun);
            System.out.println("Initial: " + initial);
            System.out.println("Language: " + language);
        }
    }
    
    // Output
    Age: 25
    Temperature: 36.6
    Is Java fun? true
    Initial: J
    Language: Java
    

    Explanation:


    Local vs Global Variables

    Java does not use the term "global" in the language specification, but in practice we talk about:

    Local Variables

    Characteristics:

    Instance and Static (Class) Variables

    Example: Local vs Instance vs Static

    public class Counter {
        // Static (class) variable - shared
        static int totalCounters = 0;
    
        // Instance variable - per object
        private int value;
    
        public Counter(int start) {
            value = start;           // instance variable
            totalCounters++;         // modify static variable
        }
    
        public void increment() {
            int step = 1;            // local variable
            value = value + step;    // use local + instance
        }
    
        public int getValue() {
            return value;
        }
    
        public static void main(String[] args) {
            Counter c1 = new Counter(0);
            Counter c2 = new Counter(10);
    
            c1.increment();
            c2.increment();
            c2.increment();
    
            System.out.println("c1 value: " + c1.getValue());
            System.out.println("c2 value: " + c2.getValue());
            System.out.println("Total counters created: " + Counter.totalCounters);
        }
    }
    
    // Output
    c1 value: 1
    c2 value: 12
    Total counters created: 2
    

    Explanation:


    Variable Naming Conventions

    Readable variable names make your code easier to understand and maintain. Java follows standard conventions:

    General Rules (Syntax)

    Java Conventions (Style)

    Example: Good vs Poor Naming

    public class NamingExample {
        public static void main(String[] args) {
            // Poor names
            int a = 5;
            int b = 10;
    
            // Better names
            int minAge = 5;
            int maxAge = 10;
    
            System.out.println("Poor: a = " + a + ", b = " + b);
            System.out.println("Better: minAge = " + minAge + ", maxAge = " + maxAge);
        }
    }
    
    // Output
    Poor: a = 5, b = 10
    Better: minAge = 5, maxAge = 10
    

    Explanation:

    Summary / Key Takeaways

    In this lesson on Java Variables, you have learned:

    Java Fundamentals For Beginners — Java Basics

    Introduction to Java

    Java Basics

    Java Basic Programs

    Understanding Java Classes and Methods

    Working with Strings

    Object-Oriented Programming (OOP) Concepts

    Exception & Error Handling

    File Handling (I/O Operations)

    Java Packages and Libraries

    Best Practices in Java Programming

    Building Simple Console-Based Applications

    Moving Forward: Advanced Topics

    Course Review and Final Project

    Tutorial Not Found