It is a kind of operator which has a wide use in any programming language and as it name suggest it is used to assign value to a variable and that value may be a direct value or a variable or we can say that it is used to assign value in the memory. That means there are two of assigning value to a variable .It a binary operator.

Syntax:-
<variable_name> = < value >;

1> Direct way :- It means we are directly assigning value to its typed variable.
for ex-
int    x  = 10 ;
float    x  = 10 ;
char   x  = 'A' ;

1> Indirect way :- It means we are Indirectly assigning value to its typed variable.Means we are assigning a variable to another variable. We are using this concept while swapping two numbers.
for ex-
int    y  = 10 ;
int    x  = y ;

Note:  While assigning make sure that the value should be assignment compatible to the variable type.
That means any integer value can be store to any int type variable it may be byte,short,int or long. 
And also make sure about the range of any  ' type ' because value must be within the range of variable type.

If we want to store the int value in the byte ,char or short then we must take care about the range.
for ex- Byte can't store any integer value below or above its range . Means it cannot store below ( -127 ) and above ( 128 ).
for ex-
byte   y  = 129 ; // Compilation error , 129 is not in range of byte type.

While assigning any value  to the variable also take care of memory , i means to say that if you need to store value " 128 " then it can easily accommodate in byte a variable so don't take a habit of using int only because unnecessarily it will occupy more memory.  

Let me tell you some of the useful and interesting cases while assigning any value to a variable .

1> Can't store a integer value to a byte type even if result value of within its range. 
byte   x  = 20 ;
byte   y  =  x   +  10 ;

While compiling the above code it will give compilation error. Now think why?
It is because here in the 2nd line we are adding a  byte value to the a int type and int type is a wider type as compared to byte type so the return value will be a int type, so compiler will search for a int type variable but here it is a byte so we cannot store a integer value to a byte even if the value is within its range. 

In java by by default any integer value is int type. 

2> We cannot store a two byte value in a byte type if it exceeds its range.
byte   x  = 200 ;
byte   y  = 20 ;
byte   z  =  x   +  y ;

It will also with compilation error because the result value after adding is " 200 " which exceeds its range. So even if two byte value cannot accommodate into a result byte type if it exceeds its range.  

So like above there are many cases you come across while coding. 
Interestingly, if we define any value as a constant which is with the use of " final " keyword in java then we will come across some interesting fact while storing values. I mean to say that here in the first case, 10 is by default int but if we make to it a constant then that code will not give any compilation error, we will see in details in the final keyword topic later in this tutorial. 

Related Posts:

  • Constructor in Java Constructor is the member of the class and it has name same as class name. Constructor is the one of the important concept in java which is responsible to initialize the instance member of the class. Its functionality is lik… Read More
  • Instance Inner class in Java As the name itself indicates that inner class with non-static scope that means a member of outer class which is of instance scope called as instance inner class. Instance inner class is itself a class but we cannot use any s… Read More
  • ' this ' keyword in Java this is one of the most important keyword in java which refers to the current class object. Like we are creating an object for our class similarly internally a object is there called as this which points to the current class… Read More
  • Inner Class in Java Inner class in a class defined as a class inside another class. So it is itself a class but first it will act as a member of that class. As we know that we cannot make a class as static but inner class can be made as static… Read More
  • super keyword in Java Like this in java one more most important keyword is there i.e super. super in Java has a wide use and it is mainly used to call the super class constructor explicitly by the user and implicitly used by the JVM to initialize… Read More

1 comment:

Blog Archive

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201978

Online Members

Live Traffic