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. 

1 comment:

Blog Archive

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic