B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Java Variables - TutorialsApp

Java Variables

In Java, variables store values like numbers, characters, and decimals. Every variable must be declared with a data type.

Java में variables मानों को store करते हैं जैसे numbers, characters, और decimals। हर variable को एक data type के साथ declare करना जरूरी है।

Definition of Variables: A variable in Java is a named storage location in memory that holds a value of a specific data type. Variables can change their values during program execution.

Variables की परिभाषा: Java में variable एक नामित memory location होता है जो एक निश्चित data type का मान रखता है। Variables प्रोग्राम के execution के दौरान अपनी value बदल सकते हैं।

Rules to Declare Variables in Java:

  • Variable names can contain letters (A-Z, a-z), digits (0-9), underscore (_) and dollar sign ($).
  • Variable names must begin with a letter, underscore (_), or dollar sign ($). They cannot start with a digit.
  • Variable names are case-sensitive (myVar and MyVar are different).
  • Variable names cannot be Java reserved keywords (like int, class, for, etc.).
  • Use meaningful names for better code readability.
  • Variable नाम में अक्षर (A-Z, a-z), अंक (0-9), underscore (_) और dollar sign ($) हो सकते हैं।
  • Variable नाम अक्षर, underscore (_) या dollar sign ($) से शुरू होना चाहिए। अंक से शुरू नहीं हो सकता।
  • Variable नाम case-sensitive होते हैं (myVar और MyVar अलग होते हैं)।
  • Variable नाम Java के reserved keywords नहीं हो सकते जैसे int, class, for आदि।
  • सपष्ट और meaningful नाम उपयोग करें जिससे कोड पढ़ने में आसानी हो।

Examples of Valid and Invalid Variable Names:

Variable Name Valid / Invalid Explanation
age Valid Simple and meaningful name starting with a letter.
_count Valid Starts with underscore, allowed in Java.
$total Valid Starts with dollar sign, allowed but rarely used.
2ndNumber Invalid Cannot start with a digit.
my variable Invalid Spaces are not allowed in variable names.
class Invalid class is a reserved keyword in Java.
myVar Valid Valid camelCase naming.
Variable Name Valid / Invalid व्याख्या
age Valid आसान और meaningful नाम जो अक्षर से शुरू होता है।
_count Valid underscore से शुरू होता है, Java में मान्य।
$total Valid Dollar sign से शुरू होता है, संभव है लेकिन कम उपयोग होता है।
2ndNumber Invalid अंक से शुरू नहीं हो सकता।
my variable Invalid Variable नाम में spaces allowed नहीं हैं।
class Invalid class Java का reserved keyword है।
myVar Valid Valid camelCase नाम।

Syntax:

type variableName = value;

Example 1: Declare and print an integer

public class Main {
    public static void main(String[] args) {
        int age = 20;
        System.out.println("Age is: " + age);
    }
}

This program declares an integer variable `age` and prints its value.

यह प्रोग्राम एक integer variable `age` को declare करता है और उसका value print करता है।

Output: Age is: 20


Example 2: Add two integers

public class Main {
    public static void main(String[] args) {
        int a = 10, b = 5;
        int sum = a + b;
        System.out.println("Sum: " + sum);
    }
}

Two integers are declared and added, result is printed.

दो integers declare किए जाते हैं, उनका जोड़ निकाला जाता है और output में दिखाया जाता है।

Output: Sum: 15


Example 3: Floating-point variable

public class Main {
    public static void main(String[] args) {
        float pi = 3.14f;
        System.out.printf("Value of pi: %.2f", pi);
    }
}

A float variable stores decimal values with 2 digit precision.

यह program एक float variable declare करता है जो decimal value रखता है।

Output: Value of pi: 3.14


Example 4: Character variable

public class Main {
    public static void main(String[] args) {
        char grade = 'A';
        System.out.println("Grade: " + grade);
    }
}

A character is stored using the `char` data type.

`char` data type का use करके एक character को store किया गया है।

Output: Grade: A


Example 5: Changing a variable value

public class Main {
    public static void main(String[] args) {
        int x = 10;
        x = 15;
        System.out.println("x is now: " + x);
    }
}

You can change a variable’s value any time after declaration.

Variable की value को बाद में बदला जा सकता है।

Output: x is now: 15


Example 6: Declare multiple variables

public class Main {
    public static void main(String[] args) {
        int a = 1, b = 2, c = 3;
        System.out.println(a + " " + b + " " + c);
    }
}

You can declare and initialize multiple variables in one line.

एक ही line में multiple variables को declare और initialize किया गया है।

Output: 1 2 3


Example 7: Input using Scanner

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = sc.nextInt();
        System.out.println("You entered: " + num);
        sc.close();
    }
}

User input is taken using Scanner class and printed.

Scanner क्लास के माध्यम से input लिया गया है और फिर print किया गया है।

Output: You entered: 42 (if 42 was input)


Example 8: Variables in arithmetic

public class Main {
    public static void main(String[] args) {
        float radius = 2.0f;
        float area = 3.14f * radius * radius;
        System.out.printf("Area: %.2f", area);
    }
}

Variables can be used in mathematical formulas like area calculation.

Variables का उपयोग formulas में किया जा सकता है जैसे area निकालने के लिए।

Output: Area: 12.56


Example 9: Constant variable

public class Main {
    public static void main(String[] args) {
        final float PI = 3.14159f;
        float r = 1.0f;
        float area = PI * r * r;
        System.out.printf("Area: %.2f", area);
    }
}

`final` is used to declare a variable whose value doesn't change.

`final` keyword ऐसे variable के लिए use किया जाता है जिसकी value नहीं बदलती।

Output: Area: 3.14