Constants and Variables in java

Introduction

Java programming में data को store और use करने के लिए variables और constants का use किया जाता है। Program में अलग-अलग values जैसे number, name, marks, salary आदि को memory में store करने के लिए variable बनाए जाते हैं।

Variable की value program execution के दौरान change हो सकती है, जबकि constant की value fixed रहती है। Constants और variables Java programming के basic building blocks हैं।Simple Definition of Variable

A variable is a named memory location used to store data values that can change during program execution.

Hindi Meaning

Variable memory का एक named location होता है जिसमें data store किया जाता है और जिसकी value program के दौरान change हो सकती है।

Simple Definition of Constant

A constant is a fixed value that cannot be changed during program execution.

Hindi Meaning

Constant एक fixed value होती है जिसे program execution के दौरान change नहीं किया जा सकता।

Variable क्या है?

Variable एक container की तरह होता है जिसमें data store किया जाता है। हर variable का एक name, data type और value होती है।

Example

int age = 20;

इस statement में:

int  → data type
age  → variable name
20   → value

यहाँ age एक variable है जिसमें 20 value store है।

Constant क्या है?

Constant ऐसी value होती है जो program में fixed रहती है। Java में constant बनाने के लिए final keyword का use किया जाता है।

Example

final int MAX_MARKS = 100;

यहाँ MAX_MARKS एक constant है। इसकी value program में change नहीं की जा सकती।

Difference Between Variable and Constant

VariableConstant
Variable की value change हो सकती है।Constant की value change नहीं हो सकती।
Variable declare करने के लिए normal data type use होता है।Constant declare करने के लिए final keyword use होता है।
Variable का नाम generally small letters में लिखा जाता है।Constant का नाम generally capital letters में लिखा जाता है।
Example: int age = 20;Example: final int PI = 3;

Syntax of Variable Declaration

data_type variable_name;

Example

int age;
String name;
double marks;

Syntax of Variable Initialization

Variable को value assign करना initialization कहलाता है।

data_type variable_name = value;

Example

int age = 20;
String name = "Rahul";
double marks = 85.5;

Syntax of Constant Declaration

Java में constant declare करने के लिए final keyword use किया जाता है।

final data_type CONSTANT_NAME = value;

Example

final int MAX_MARKS = 100;
final double PI = 3.14;

Example: Variables in Java

class VariableDemo {
    public static void main(String[] args) {
        int age = 20;
        String name = "Rahul";
        double marks = 85.5;

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Marks: " + marks);
    }
}

Output

Name: Rahul
Age: 20
Marks: 85.5

Explanation

इस program में age, name और marks variables हैं। इन variables में अलग-अलग type की values store की गई हैं और फिर output में print किया गया है।

Example: Changing Variable Value

class ChangeVariable {
    public static void main(String[] args) {
        int marks = 70;
        System.out.println("Old Marks: " + marks);

        marks = 90;
        System.out.println("New Marks: " + marks);
    }
}

Output

Old Marks: 70
New Marks: 90

Explanation

इस program में marks variable की value पहले 70 है। बाद में इसकी value change करके 90 कर दी गई। इसलिए variable की value change हो सकती है।

Example: Constant in Java

class ConstantDemo {
    public static void main(String[] args) {
        final int MAX_MARKS = 100;
        int obtainedMarks = 85;

        System.out.println("Maximum Marks: " + MAX_MARKS);
        System.out.println("Obtained Marks: " + obtainedMarks);
    }
}

Output

Maximum Marks: 100
Obtained Marks: 85

Explanation

इस program में MAX_MARKS constant है क्योंकि इसे final keyword के साथ declare किया गया है। इसकी value fixed रहेगी और change नहीं की जा सकती।

Example: Error When Changing Constant

class ConstantError {
    public static void main(String[] args) {
        final int MAX_MARKS = 100;
        MAX_MARKS = 90;

        System.out.println(MAX_MARKS);
    }
}

Error

cannot assign a value to final variable MAX_MARKS

Explanation

MAX_MARKS को final keyword के साथ declare किया गया है। इसलिए इसकी value दोबारा change नहीं की जा सकती। अगर हम change करने की कोशिश करते हैं, तो compile-time error आएगा।

Types of Variables in Java

Java में mainly तीन types के variables होते हैं:

  • Local Variable
  • Instance Variable
  • Static Variable

Local Variable

Local variable method, constructor या block के अंदर declare होता है। इसका use केवल उसी block के अंदर किया जा सकता है जहाँ इसे declare किया गया है।

Example

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

Output

Age: 20

Explanation

यहाँ age local variable है क्योंकि यह main() method के अंदर declare किया गया है।

Instance Variable

Instance variable class के अंदर लेकिन method के बाहर declare होता है। हर object के लिए instance variable की अलग copy बनती है।

Example

class Student {
    String name = "Rahul";
    int age = 20;

    public static void main(String[] args) {
        Student s = new Student();
        System.out.println("Name: " + s.name);
        System.out.println("Age: " + s.age);
    }
}

Output

Name: Rahul
Age: 20

Explanation

इस program में name और age instance variables हैं क्योंकि ये class के अंदर लेकिन method के बाहर declare किए गए हैं। इन्हें object की help से access किया जाता है।

Static Variable

Static variable class के अंदर और method के बाहर static keyword के साथ declare होता है। Static variable class से related होता है, object से नहीं। सभी objects static variable की same copy share करते हैं।

Example

class College {
    static String collegeName = "ABC College";

    public static void main(String[] args) {
        System.out.println("College Name: " + College.collegeName);
    }
}

Output

College Name: ABC College

Explanation

यहाँ collegeName static variable है। इसे class name College की help से access किया गया है।

Naming Rules for Variables

Java में variable name लिखते समय कुछ rules follow करने होते हैं।

  • Variable name letter, digit, underscore _ या dollar sign $ से बन सकता है।
  • Variable name digit से start नहीं हो सकता।
  • Variable name में space नहीं हो सकता।
  • Java keyword को variable name नहीं बना सकते।
  • Variable name case-sensitive होता है।
  • Meaningful variable name use करना चाहिए।

Valid Variable Names

age
studentName
marks1
_total
$salary

Invalid Variable Names

1age
student name
class
roll-no
int

Good Programming Style for Variables

Variable names meaningful और readable होने चाहिए। Java में variable names generally camelCase में लिखे जाते हैं।

Good Examples

studentName
totalMarks
averageSalary
rollNumber

Bad Examples

a
b
x
n1

अगर variable का purpose clear हो, तो program समझना आसान हो जाता है।

Constants Naming Style

Java में constants के names generally capital letters में लिखे जाते हैं। अगर name में multiple words हों, तो underscore _ use किया जाता है।

Examples

final int MAX_MARKS = 100;
final double PI_VALUE = 3.14;
final int MIN_AGE = 18;

Example: Area of Circle Using Constant

class CircleArea {
    public static void main(String[] args) {
        final double PI = 3.14;
        double radius = 5;
        double area = PI * radius * radius;

        System.out.println("Area of Circle = " + area);
    }
}

Output

Area of Circle = 78.5

Explanation

इस program में PI constant है क्योंकि इसकी value fixed रहती है। radius और area variables हैं क्योंकि इनकी values program में change हो सकती हैं।

Example: Student Result Using Variables

class StudentResult {
    public static void main(String[] args) {
        String name = "Amit";
        int rollNo = 101;
        int hindi = 80;
        int english = 75;
        int computer = 90;
        int total = hindi + english + computer;
        double percentage = total / 3.0;

        System.out.println("Name: " + name);
        System.out.println("Roll No: " + rollNo);
        System.out.println("Total Marks: " + total);
        System.out.println("Percentage: " + percentage);
    }
}

Output

Name: Amit
Roll No: 101
Total Marks: 245
Percentage: 81.66666666666667

Common Mistakes

Variable बिना Declare किए Use करना

Wrong:

age = 20;
System.out.println(age);

Correct:

int age = 20;
System.out.println(age);

Keyword को Variable Name बनाना

Wrong:

int class = 10;

Correct:

int marks = 10;

Constant की Value Change करना

Wrong:

final int MAX = 100;
MAX = 90;

Correct:

final int MAX = 100;

Semicolon भूल जाना

Wrong:

int age = 20

Correct:

int age = 20;

Exam-Oriented Short Answer

Q. What are constants and variables in Java?

Variable Java में एक named memory location होता है जिसका use data values को store करने के लिए किया जाता है। Variable की value program execution के दौरान change हो सकती है। Constant एक fixed value होती है जिसकी value program execution के दौरान change नहीं की जा सकती। Java में constant declare करने के लिए final keyword का use किया जाता है। Example: int age = 20; में age variable है और final int MAX_MARKS = 100; में MAX_MARKS constant है।

Important Points to Remember

  • Variable data store करने के लिए use होता है।
  • Variable की value change हो सकती है।
  • Constant की value fixed रहती है।
  • Java में constant बनाने के लिए final keyword use होता है।
  • Variable का name meaningful होना चाहिए।
  • Variable digit से start नहीं हो सकता।
  • Java keywords को variable name नहीं बना सकते।
  • Local variable method के अंदर declare होता है।
  • Instance variable class के अंदर और method के बाहर declare होता है।
  • Static variable static keyword के साथ declare होता है।

Practice Questions

Short Answer Questions

  • Variable क्या होता है?
  • Constant क्या होता है?
  • Variable और constant में अंतर लिखिए।
  • Java में constant declare करने के लिए कौन सा keyword use होता है?
  • Local variable क्या होता है?
  • Instance variable क्या होता है?
  • Static variable क्या होता है?
  • Variable naming rules लिखिए।

Long Answer Question

Java में constants और variables को example के साथ explain कीजिए।

Homework Program

एक Java program लिखिए जो student का name, roll number, marks और maximum marks print करे। Maximum marks को constant के रूप में declare कीजिए।

Hint

class StudentMarks {
    public static void main(String[] args) {
        final int MAX_MARKS = 100;
        String name = "Rahul";
        int rollNo = 101;
        int marks = 85;

        System.out.println("Name: " + name);
        System.out.println("Roll No: " + rollNo);
        System.out.println("Marks: " + marks);
        System.out.println("Maximum Marks: " + MAX_MARKS);
    }
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top