Language:
Contact Us

Search

Variables, Functions and Assignment Operators in Java

  • Share this:

Declaration and Ininitilisation :

int a = 5;
int b = 7;
int c = 10;
int[] array1 = {10, 20, 30, 40};
int[] array2 = {1, 2, 3, 4};
int addition(int x, int Y)
{ return x + y; }

Statements:
a = b;
a = b+c;          
b + c = a;            
//invalid Statement
array1[1] = a;
array1[2] = array1[3];
array1[2] = array2[1];
array1[1] = array[1] + array2[3];
array1[1] = array[1] + array1[1];
array[1] = a + b +  array1[3] + array1[1] + addition(a,b);
array1[1] + array1[2] = array2[1];    
//invalid statement
a = a+b+ array1[3] + array1[1]+addition(a,b); //see rule 6
arry1[3] = a+b+ array1[3] + array1[1]+addition(a,b);
System.out.println(a)
System.out.println(a+b)
System.out.println(a+b+ array1[3] + array1[1]+addition(a,b));

Rules :

 

1) Left side of equal (=) single variable.                      a = b;
2) Right side of equal (=) one or more variables.        a = b+c;
3) Same variable can be repeated on the sides.         a = a*2;

3) Right side of equal (=) function                                a = addition(a,b);
4) Right side of equal (=) array with index.                  a = array1[1];
5) Right side of equal (=) one or more in any combination    
6) Right side is getting the value. Multiple values allowed.
7) Left side is setting the value. Single value only. (Multiple value possible through array).

Functions / Methods :
Declaration : 
int multiplication(int x, int Y)
{ return x * y; }

int addition(int x, int Y)
{ return x + y; }

Usage:
a = mulitipication(3,7);
array2[2] = multiplication(3,7);
array2[2] = multiplication(b,c);
a = array2[2] = multiplication(b, addition(b, c));

multiplication(3,5) = a;    //invalid statement
 

Raz Manva

Raz Manva