Scope of Variables | Default Values of Variables

મિત્રો, આ Tutorial માં આપણે શીખીશું Scope of variables and Default Values of Variables in java, Scope of variables, Default Values of Variables.

આપણે અગાઉ ના Tutorial માં જોયું કે Type Conversion અને Casting અને હવે આપડે શીખીશું કે Scope of Variables and Default Values of Variables. તો ચાલો મિત્રો આજે આ Tutorial માં Scope of Variables અને Default Variables Values ને ખૂબજ સરળ રીતે સમજીએ.

Scope of Variables | Default Values of Variables

Scope of Variables

  • Java variables ને કોઈ પણ block માં declare કરવા allow કરે છે.
  • Block એ curly braces એટલે કે છગડીયા કૌસથી શરૂ અને બંધ થાય છે.
  • બ્લોક એ scope ને define કરે છે.
  • આમ, જ્યારે પણ તમે નવો block શરૂ કરો છો ત્યારે તમે એક નવો scope બનાવો છો.  
  • Variable નો scope એ program નો region [વિસ્તાર] છે કે જેમાં variables access કરી શકાય છે.
  • Scope નક્કી કરે છે કે system ક્યારે variable માટે memory બનાવે છે અને તેને destroy કરે છે.
  • Scope એ પણ નક્કી કરે છે કે તમારા program ના other parts [બીજા ભાગો] માં કયા objects visible છે.
  • Scope અંદર declare કરેલ variables તે scope ની બહાર define કરેલા code ને visible થતાં નથી.
  • Scope ની અંદર declare કરેલ variable ને scope ની અંદર જ access કરી શકાય અને unauthorized access થી protect કરી શકાય.
  • બહાર declare કરેલ variable ને તમે અંદર ના scope માં access કરી શકો.
  • પરંતુ, અંદર ના scope માં declare કરેલ variable ને તમે બહાર access કરી શકો નહીં.
  • Program માં Variable declaration નું location નીચે મુજબ છે:
  1. Member variable
  2. Local variable
  3. Method parameter
  4. Exception-handler parameter

Member Variable

  • આ variables class ની અંદર declare થાય છે.
  • આ variables ને આપણે class ની અંદર ગમે ત્યાં access કરી શકીએ છીએ.
  • Class ની બહાર તેને access કરી શકાય નહીં.

Local Variable

  • આ variables એ method, constructor અથવા method level હોય તેવા block અથવા block level scope ની અંદર declare થાય છે.
  • તે જેમાં પણ declare થાય તેની બહાર તેને access કરી શકાય નહીં.
  • ઉદાહરણ તરીકે જો કોઈ method માં int a કરીને variable declare કરેલ છે તો તે method ની બહાર int a ને access કરી શકાય નહીં.

Method Parameter

  • કોઈ પણ method માં આપણે parameter add કરીએ છીએ.
  • આ parameters નો scope તે method પૂરતો જ હોય છે.
  • તે method ની બહાર તેને access કરી શકાય નહીં.
public void add(int a)
{
}
  • ઉપરોક્ત ઉદાહરણ માં a variable ને આપણે add method ની બહાર access કરી શકીએ નહિ.

Exception Handling Parameter

  • જ્યારે આપણાં પ્રોગ્રામ માં કોઈ error આવે છે તો તે error ને solve કરવા માટે આપણે તેને handle કરીએ છીએ જેને exception handling કહે છે.
  • આ process try……catch block દ્વારા થાય છે.
  • આપણે catch block માં જે પણ exception throw કરાવીએ તે exception પણ catch નો parameter જ છે.
  • તે parameter ને આપણે catch block ની બહાર access કરી શકીએ નહીં.

Example:

public class maricollege
{
     int a; //member variable
     public void add(int b)
     {
          //int b is a method parameter
             int c; //local variable
     }
    try
    {
    }
    catch(ArithmeticException e)
    {
          //ArithmeticException e is exception-handler parameter
    }
}

ઉપરોક્ત ઉદાહરણ માત્ર scope ને સમજવા માટે આપેલ છે.

Default Values of Variables

  • જો તમે variable ને value assign નથી કરતાં તો java runtime variable ને default value assign કરે છે.
  • જ્યારે તમે તે variable ને access કરવાનો try કરો ત્યારે તમને variable ની default value મળશે.
  • નીચેના table માં variable ની type અને default value દર્શાવેલ છે
Data typeDefault value
booleanFalse
char\u0000
int,short,byte/long0/0L
float/double0.0f/0.0d
Any reference typeNull
  • અહી, char ની default value નો મતલબ blank અથવા space character છે.
  • જ્યારે તમે કોઈ local અથવા block variable ને declare કરો છો, ત્યારે તેમને default value મળતી નથી.

આ પણ વાંચો – Type Conversion and Casting in Java

આ પણ વાંચો – Variables and Constants in Java

આ પણ વાંચો – Java Identifiers and Literals (With Examples)

આ પોસ્ટ જેમાં મિત્રો આપણે જોયું કે Scope of variables and Default Values of Variables in java, Scope of variables, Default Values of Variables.

જો મિત્રો તમને આ પોસ્ટ મદદરૂપ લાગી હોય, તો તમે તમારા મિત્રો સાથે ચોક્કસ શેર કરો અને જો તમને જાવા અથવા બીજા કોઈ વિષય સંબંધિત કોઈ પ્રશ્ન હોય તો તમે નીચે કોમેન્ટ કરીને અમને જણાવી શકો છો. આભાર.

Leave a Comment