Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Java Variables and Data Types – A Comprehensive Guide with Examples

What Is a Variable in Java?

A variable is a named memory location that holds data during a Java program’s execution. Every variable is assigned a data type that specifies the kind of value it can store and the amount of memory required.

Java variables are grouped into three main categories:

Using Variables: Declaration & Initialization

To work with a variable, you must:

  1. Declare it by specifying its type and name.
  2. Initialize it by assigning a value.

Variable Declaration

When declaring a variable, you provide its type and a unique name.

Java Variables and Data Types – A Comprehensive Guide with Examples

Valid declaration examples:

int a, b, c;
float pi;
double d;
char a;

Variable Initialization

Initialization assigns an initial value to a variable.

Java Variables and Data Types – A Comprehensive Guide with Examples

Valid initialization examples:

pi = 3.14f;
do = 20.22d;
a = 'v';

You can combine declaration and initialization in a single statement:

Java Variables and Data Types – A Comprehensive Guide with Examples

Example:

int a = 2, b = 4, c = 6;
float pi = 3.14f;
double do = 20.22d;
char a = 'v';

Types of Variables

Java distinguishes three kinds of variables:

  1. Local Variables
  2. Instance Variables
  3. Static Variables

1) Local Variables

Declared inside a method or block, they exist only during that method’s execution.

2) Instance Variables

Defined without the static keyword, they are tied to individual objects of a class.

3) Static Variables

Initialized once at program start, before any instance variables. They belong to the class rather than any instance.

Example: Types of Variables in Java

class Guru99 {
    static int a = 1; // static variable
    int data = 99; // instance variable
    void method() {
        int b = 90; // local variable
    }
}

Data Types in Java

Java’s data types are divided into two categories:

Java Variables and Data Types – A Comprehensive Guide with Examples

Primitive Data Types

Java provides eight primitive types, each with a fixed size and default value:

Java Primitive Types
Data TypeDefault ValueSize
byte01 byte
short02 bytes
int04 bytes
long0L8 bytes
float0.0f4 bytes
double0.0d8 bytes
booleanfalse1 bit
char'\u0000'2 bytes

Key Takeaways

Type Conversion and Casting

Java allows values of one type to be assigned to variables of another type through two mechanisms:

  1. Implicit Conversion (Widening) – assigning a smaller type to a larger type (e.g., int to long). This happens automatically.
  2. Explicit Casting (Narrowing) – assigning a larger type to a smaller type (e.g., double to int). The programmer must cast explicitly; otherwise, the compiler reports an error to prevent accidental data loss.

Example: Demonstrating Type Casting

Step 1: Copy the following code into an editor.

class Demo {
    public static void main(String[] args) {
        byte x;
        int a = 270;
        double b = 128.128;
        System.out.println("int converted to byte");
        x = (byte) a;
        System.out.println("a and x " + a + " " + x);
        System.out.println("double converted to int");
        a = (int) b;
        System.out.println("b and a " + b + " " + a);
        System.out.println("\ndouble converted to byte");
        x = (byte) b;
        System.out.println("b and x " + b + " " + x);
    }
}

Step 2: Save, compile, and run the code.

Output:

int converted to byte
a and x 270 14
double converted to int
b and a 128.128 128
double converted to byte
b and x 128.128 -128

Java

  1. Master C# Variables & Primitive Data Types: A Complete Guide
  2. Java Variables and Literals: A Comprehensive Guide
  3. Java Primitive Data Types: A Complete Guide with Examples
  4. Mastering C# Variables & Operators: Practical Examples & Explanations
  5. Encapsulation in Java: A Comprehensive Guide with Practical Example
  6. Understanding Java Classes and Objects: Clear Concepts, Practical Examples
  7. Master Java String Manipulation: Essential Functions, Methods, and Practical Examples
  8. Mastering the 'this' Keyword in Java: Purpose, Usage, and Practical Examples
  9. Java Inheritance Explained: Types, Syntax, and Practical Examples
  10. Understanding Java Variable Types: A Comprehensive Guide