Header Ads

Variables & Types of Variables in Java

 Variables & Types of Variables in Java

In Java, a variable is a named storage location that holds a value of a particular type. Variables are used to store data that can be manipulated and accessed throughout the program. The type of a variable determines the kind of values it can hold and the operations that can be performed on it.

Here are some common types of variables in Java along with examples:

1. **Primitive Variables**: These variables hold simple, fundamental data types.

   - `int`: Used to store whole numbers.

     int age = 25;


   - `double`: Used to store floating-point numbers with decimal places.

     double price = 10.99;


   - `boolean`: Used to store true or false values.

     boolean isReady = true;


   - `char`: Used to store a single character.

     char grade = 'A';


2. **Reference Variables**: These variables hold references to objects.

   - `String`: Used to store a sequence of characters.

     String name = "John";


   - `Scanner`: Used for user input.

     Scanner scanner = new Scanner(System.in);


   - `ArrayList`: Used to store a dynamic list of objects.

     ArrayList<Integer> numbers = new ArrayList<>();


3. **Array Variables**: These variables hold multiple values of the same type.

   - `int[]`: Used to store an array of integers.

     int[] scores = {85, 90, 78, 92};


   - `String[]`: Used to store an array of strings.

     String[] names = {"Alice", "Bob", "Charlie"};


4. **Class Variables**: These variables belong to a class and are shared among all instances of the class.

   class Person {

       String name;

       static int count;

   }


   In the example above, `name` is an instance variable (unique for each instance of the `Person` class), while `count` is a class variable (shared among all instances).

These are just a few examples of variable types in Java. There are additional types and variations available, such as long, float, byte, short, and more. The choice of variable type depends on the kind of data you want to store and manipulate in your program.


हिंदी:

जावा में, एक चर एक नामी भंडारण स्थान होता है जो एक विशेष प्रकार के मान को रखता है। चरों का उपयोग किया जाता है जिन डेटा को संशोधित और पहुँच सका हो जबकि वह कार्यक्रम चल रहा हो। चर का प्रकार इसे निर्धारित करता है कि वह मान किस प्रकार के होंगे और उस पर किन परिवर्तनों को किया जा सकता है।

यहां जावा में कुछ सामान्य चरों के प्रकार दिए गए हैं जो कि उदाहरण के साथ हैं:

1. **प्राथमिक चर**: ये चर सरल, मूल डेटा प्रकार को रखते हैं।


   - `int`: पूर्णांकों को रखने के लिए प्रयुक्त होता है।

     int age = 25;


   - `double`: दशमलव स्थान के साथ फ्लोटिंग प्वाइंट संख्याओं को रखने के लिए प्रयुक्त होता है।

     double price = 10.99;


   - `boolean`: सच या झूठे मानों को रखने के लिए प्रयुक्त होता है।

     boolean isReady = true;


   - `char`: एकल अक्षर को रखने के लिए प्रयुक्त होता है।

     char grade = 'A';


2. **संदर्भ चर**: ये चर ऑब्जेक्ट्स के संदर्भ को रखते हैं।


   - `String`: अक्षरों का अनुक्रम रखने के लिए प्रयुक्त होता है।

     String name = "John";


   - `Scanner`: उपयोगकर्ता इनपुट के लिए प्रयुक्त होता है।

     Scanner scanner = new Scanner(System.in);


   - `ArrayList`: ऑब्जेक्ट्स की एक गतिशील सूची को रखने के लिए प्रयुक्त होता है।

     ArrayList<Integer> numbers = new ArrayList<>();


3. **ऐरे चर**: ये चर एक ही प्रकार के कई मानों को रखते हैं।


   - `int[]`: पूर्णांकों के ऐरे को रखने के लिए प्रयुक्त होता है।

     int[] scores = {85, 90, 78, 92};


   - `String[]`: स्ट्रिंग के ऐरे को रखने के लिए प्रयुक्त होता है।

     String[] names = {"Alice", "Bob", "Charlie"};


4. **कक्षा चर**: ये चर कक्षा के हिस्से होते हैं और कक्षा के सभी उदाहरणों के बीच साझा होते हैं।

   class Person {

       String name;

       static int count;

   }


   ऊपर के उदाहरण में, `name` एक उदाहरण चर है (कक्षा `Person` के प्रत्येक उदाहरण के लिए अद्वितीय), जबकि `count` एक कक्षा चर है (सभी उदाहरणों के बीच साझा)।

ये कुछ उदाहरण हैं जो जावा में चरों के प्रकार हैं। और भी अधिक चर प्रकार और विविधताएं हैं जैसे long, float, byte, short, और अधिक। चर प्रकार का चयन आपके कार्यक्रम में संग्रहीत और संशोधित करने के लिए डेटा के प्रकार पर निर्भर करता है।

No comments

Powered by Blogger.