Pin It

How assignment operator works with reference variables in Java ?

Program to demonstrate how assignment operator works with reference variables in Java

class Triangle {
 
  public int width;
 
}

public class AssignmentReferenceTest {
 
  public static void main(String[] args) {
  
     Triangle triangleOne = new Triangle();
     Triangle triangleTwo = new Triangle();
  
     triangleOne.width = 10;
     triangleTwo.width = 20;
  
     System.out.println("1: triangleOne's width = " + triangleOne.width);
     System.out.println("1: triangleTwo's width = " + triangleTwo.width);
  
     triangleOne= triangleTwo;
  
     System.out.println("2: triangleOne's width = " + triangleOne.width);
     System.out.println("2: triangleTwo's width = " + triangleTwo.width);
  
     triangleOne.width = 30;
      
     System.out.println("3: triangleOne's width = " + triangleOne.width);
     System.out.println("3: triangleTwo's width = " + triangleTwo.width);
 
   }

}


Output of the program :




Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot