Variable In Java

Variable In Java

Variable is an identifier which holds data or another one variable is an identifier whose value can be changed at the execution time of program. Variable is an identifier which can be used to identify input data in a program.    

Variable In Java

 Variable names are case-sensitive. A variable’s name can be any legal identifier — an unlimited length sequence of Unicode letters and digits, beginning with a letter, the dollar sign “$”, or the underscore character “_”. The convention, however, is to always begin your variable names witha letter, not “$” or “_”. Additionally, the dollar sign character, by convention, is never used at all.You may find some situations where auto-generated names will contain the dollar sign, but your variable names should always avoid using it. 

Rules to declare a Variable

  • Every variable name should start with either alphabets or underscore ( _ ) or dollar ( $ ) symbol.
  • No space are allowed in the variable declarations.
  • Except underscore ( _ ) no special symbol are allowed in the middle of variable declaration
  • Variable name always should exist in the left hand side of assignment operators.
  • Maximum length of variable is 64 characters.
  • No keywords should access variable name.
  • The identifier should not duplicated. 

class Test
{
void add()
{
int a=10;
int a=20; //    the identifier should not be duplicated.

} };

  In the java applications it is possible to declare all the predefined class names and predefined interfaces names as an identifier. But it is not recommended to use.

class TestVariableName

{

public static void main(String[] args)

{

int String=10; //predefind String class

int Serializable=20; //predified Seriaiable class

float Exception=10.2f; //predefined Exception class

System.out.println(String);

System.out.println(Serializable);

System.out.println(Exception);

}}; 

Syntax

Datatype variable_ name;

Example:-

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

Variable initialization
·         It is the process of storing user defined values into variable.·         storing user defined values at the time of allocation of memory space. 

·         Storing user defined values after declaration the variable.

//declaring & initializing at a time 
int a=10;//declaring a variable
int b;b=20;      // direct assigned
variableb=a;     //assigned value in term of variable
b=a+10;   //assigned value as term of expression

Note-

public class Main{
public static void main(String[] args) {
        int a,b,c,d;
        a=b=c=d=20;
        System.out.println(“a=”+a+”b=”+b+”c=”+c+”d=”+d);
    }
}
Output-a=20 b=20 c=20 d=20
Note- chained assignment is allowed in java

public class 
ArithAssignment{
public static void main(String[] args) {
int a=b=c=d=20;
System.out.println(“a=”+a+”b=”+b+”c=”+c+”d=”+d);
}

}
Output-ArithAssignment.java:5: error: cannot find symbol
        int  a=b=c=d=20;
               ^
  symbol:   variable b
  location: class ArithAssignment
ArithAssignment.java:5: error: cannot find symbol
        int  a=b=c=d=20;
                 ^
  symbol:   variable c
  location: class ArithAssignment
ArithAssignment.java:5: error: cannot find symbol
        int  a=b=c=d=20;
                   ^
  symbol:   variable d
  location: class ArithAssignment
 
Note- we can not perform chained assignments directly at the time of declaration.

public class 
ArithAssignment{
public static void main(String[] args) {
int b,c,d;
int a=b=c=d=20;// It is a valid
System.out.println(“a=”+a+”b=”+b+”c=”+c+”d=”+d);

}
Output-a=20 b=20 c=20 d=20

Leave a Comment

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