Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Declaration And Assignment Of Java Variables

In Java, a variable is a named memory location used to store data. Variables have a name, a data type, and a value. In this tutorial, we will learn about different types of variables in Java and how to use them.

  • Primitive Data Types:

Java has eight primitive data types, which are the building blocks for all other data types:

  • byte: 8-bit integer
  • short: 16-bit integer
  • int: 32-bit integer
  • long: 64-bit integer
  • float: 32-bit floating-point number
  • double: 64-bit floating-point number
  • char: 16-bit Unicode character
  • boolean: true or false
  • Variable Declaration:

To declare a variable, you need to specify its data type followed by the variable name:

int age;
double salary;
char letter;
boolean isActive;
  • Variable Initialization:

You can assign a value to a variable during declaration or later in the code:

int age = 25;
double salary = 50000.0;
char letter = 'A';
boolean isActive = true;

// or

int age;
age = 25;
  • Local Variables:

Local variables are declared and used within a method, constructor, or block. They are created when the method or block is entered and destroyed when it is exited. Local variables don't have a default value, so you must initialize them before using them.

public void myMethod() {
    int localVar = 10;
    System.out.println("Local variable value: " + localVar);
}
  • Instance Variables:

Instance variables are declared inside a class but outside any method, constructor, or block. They are created when an object is created using the new keyword and destroyed when the object is destroyed. Instance variables have default values depending on their data type (e.g., 0 for numeric types, false for boolean, and null for reference types).

public class MyClass {
    int instanceVar; // Default value: 0

    public void displayInstanceVar() {
        System.out.println("Instance variable value: " + instanceVar);
    }
}
  • Class (Static) Variables:

Class variables, also known as static variables, are declared inside a class but outside any method, constructor, or block, with the static keyword. There is only one copy of a class variable shared among all instances of the class. Class variables have default values like instance variables.

public class MyClass {
    static int classVar; // Default value: 0

    public void displayClassVar() {
        System.out.println("Class variable value: " + classVar);
    }
}
  • Constants:

Constants are variables whose values cannot be changed after initialization. To declare a constant, use the final keyword:

final double PI = 3.14159;

In conclusion, Java has various types of variables, including local variables, instance variables, class variables, and constants. Each type of variable has a specific scope and lifetime. Understanding these variables and their usage will help you write efficient and maintainable Java code.

  1. Variable types in Java and their declaration: Java supports various types of variables: local variables, instance variables, and class variables. They are declared with appropriate keywords.

    // Local variable declaration
    int localVar;
    
    // Instance variable declaration
    class MyClass {
        int instanceVar;
    }
    
    // Class variable declaration
    class AnotherClass {
        static int classVar;
    }
    
  2. Initializing variables during declaration in Java: Variables can be initialized during declaration, providing an initial value.

    int initializedVar = 10;
    
  3. Primitive data types and variable declaration in Java: Java has primitive data types (int, char, boolean, etc.) for basic value types.

    int intValue;
    char charValue;
    boolean boolValue;
    
  4. Declaring and assigning arrays in Java: Arrays in Java are declared using square brackets.

    // Array declaration
    int[] intArray;
    
    // Array creation and assignment
    intArray = new int[]{1, 2, 3};
    
  5. Using final keyword for constant variables in Java: The final keyword is used to declare constants.

    final int CONSTANT_VALUE = 100;
    
  6. Variable shadowing in Java: Variable shadowing occurs when a variable declared within a certain scope has the same name as a variable in an outer scope.

    int x = 5;
    
    void myMethod() {
        int x = 10; // This shadows the outer x
        System.out.println(x); // Prints 10
    }
    
  7. Java multiple variable declaration and assignment: Multiple variables can be declared and assigned on the same line.

    int a = 1, b = 2, c = 3;
    
  8. Declaring and assigning objects in Java: Objects are instances of classes and are declared and assigned similarly.

    // Object declaration
    MyClass myObject;
    
    // Object instantiation and assignment
    myObject = new MyClass();
    
  9. Default values for variables in Java: Variables have default values if not explicitly initialized.

    int defaultInt; // Defaults to 0
    boolean defaultBool; // Defaults to false