Understanding Java Variables: Types, Declaration and Examples

In Java variables key components for storing values, Which can be changeable and we can modify as much as we want. Variables can hold multiple types of data types like integer, floating numbers, strings and many more. So, understanding this concept is essentials for developers.

In this article we will discuss about types of variables, How to declare variables and provide examples guide.


Variables in Java

As we know variables are used for holding values and it can be change during execution of program. Each variables have there own types, That can defines which data they can hold. Example, Integer’s, decimals, text and objects.

Let me show syntax of declaration of variables.

dataType variableName = value;
  • dataTypes : Defines the types of value that can variable hold. (Int, Float, String, Boolean).
  • variableName : Name of Variable.
  • value : Initial value assigned to the value.

Types of Variables in Java

Java supports servals variables in java. Which can broadly support various categories. let me share with you.

  1. Local Variables
  2. Instance Variables
  3. Class Variables (Static Variables)

1. Local Variables

Local variables are declare under the method, constructor or block. they can only able to access within method, constructor or block scope and destroyed once operation is complete.

public class Main {
    public static void main(String[] args) {
        int number = 10; // Local variable
        System.out.println("The number is: " + number);
    }
}

In this example number is local variable. It is only Accessible within main method.

2. Instance Variables

Instance variable also called field or member variable. This variable declared in class but outside of any methods or constructor. Instance variables make there own copy variable and this variables define default value 0 or NULL (If not explicitly defined).

class Car {
    String brand; // Instance variable
    int speed;
}

In above example Car object have there own brand and speed.

3. Static Variables

Static variables are also called as class variables. This variables are shared with all instances of class. We can declare this variable using static keyword and it will store in class memory. Static variables can initialize only one time also we don’t have to create object for access this variable.

class Car {
    static int totalCars = 0; // Static variable

    Car() {
        totalCars++;
    }
}

Here, totalCars is a static variable that keeps track of how many Car objects have been created.


Data Types in Java

Basically java is statically typed language, That means type of variable is declared at compile time only. Java divided datatypes in two parts.

  1. Primitive Data Types
  2. Reference/Object Data Types

1. Primitive Data Types

Java offers eight built in primitive datas.

Data TypesSizeDescriptionExample Values
byte1 byteStores whole numbers from -128 to 12710, -100
short2 bytesStores whole numbers from -32,768 to 32,7672000, -5000
int4 bytesStores whole numbers from -2^31 to 2^31-150000, -100000
long8 bytesStores whole numbers from -2^63 to 2^63-1100000000L
float4 bytesStores fractional numbers (6-7 decimal digits)5.75f, -7.2f
double8 bytesStores fractional numbers (15 decimal digits)19.99, -25.45
char2 bytesStores a single character‘A’, ‘z’
boolean1 bitStores true or false valuestrue, false

Let’s take an example.

int age = 30;        // Integer type
double price = 9.99; // Double type
char grade = 'A';    // Character type
boolean isJavaFun = true; // Boolean type

2. Reference Data Types

Reference data types is an objects and arrays in Java. These variables store the memory address of the object, not the actual object itself .Examples of reference data types include class types (like String), interface types, and arrays.

String name = "John Doe";  // Reference variable pointing to a String object
Car myCar = new Car();     // Reference variable pointing to a Car object

Variable Naming Conventions

In java, variable conventions are most important for readability and maintainability.

  1. Use camelCase for variable names: The first word is lowercase, and subsequent words start with uppercase letters (e.g., myVariable, studentAge).
  2. Variable names should be descriptive: Choose meaningful names to indicate the purpose of the variable (e.g., totalPrice, isCompleted).
  3. Avoid using reserved keywords as variable names, such as class, void, static.

Conclusion

So, I hope you understand this topic. I know i haven’t talked much about variables but it is not possible to cover all the scenario in small use case articles. Feel free to google it or Search it on perplexity AI (No Promotion). We will discuss more about this topics focusing on strings in data types. Thank you for reading.

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply