Java operators



Java operators 


 Operators produce new values from one or more operands. 


 The result is a boolean or numeric value. 


 Simple assignment operator = 


 The equal (=) sign is used for assigning a value to a variable. 


 The equal sign can be used with both primitive values and objects. 


 The assignment operator = writes over the previous value of the destination variable. 


 <variable> = <expression> 


 <variable> must have been declared. 


 <expression> evaluates to either a primitive data value or an object reference. 

<variable> and <expression> must be type compatible. 


 Assigning primitive values 


 The assignment operator has the lowest precedence, allowing the expression on the right-hand 
 side to be evaluated before assignment. 


 Example, 


 int a = 10; 
 System.out.println ("a = " + a); 
 int b = a; 
 b = 30; 
 System.out.println ("a = " + a + "after change to b"); 


 Output 


 a = 10 
 a = 10 after change to b 


 Assigning references 


 Variables are merely pointers to the actual object itself. 


 If we assign an existing instance of an object to a new reference variable, then two reference 
 variables will point to the same object. 


 Assigning references does not copy the state of the object on the right-hand side, only the 
 reference value. 


 Example, 


 Dimension a = new Dimension (5,10); 
 System.out.println ("a.height = " + a.height); 
 Dimension b = a; 
 b.height = 30; 


 Output 


 a.height = 10 




Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email