misc - Wednesday, October 12, 2011 Parameter passing in...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Wednesday, October 12, 2011 Parameter passing in Java In Java objects are always accessed via references These references are different from, and simpler than, C++ references Unlike a C++ reference, a Java reference can be reassigned Thus, unlike a C++ reference, the type of the object referred to by the reference can change with assignement Unlike C++, references cannot be to a primitive type Wednesday, October 12, 2011 Examples public class B { public int age; public B(int a) { age = a; } public void print(String s) { System.out.println(s+", "+"B object "+age); } } public class D extends B { public int weight; public D(int a, int w) { super(a); weight = w; } public void print(String s) { System.out.println(s+" "+"D object "+age+", "+weight); } } Wednesday, October 12, 2011 Example continued public class T { /* basic reference operations */ public static void main(String args) { B b1 = new B(50); b1.print("b1{50} "); D d = new D(51,100); d.print("d{51, 100}"); B b2 = (B) d; b2.print("b2{51,100} which is d"); b2 = b1; b2.print("b2{50} which again b1 again"); b1.print("b1{50} before xchange"); d.print("d{51,100} before xchange"); xchangeWrong(b1, (B) d); b1.print("b1{50} after xchange"); d.print("d{51,100} after xchange"); } public static void xchangeWrong(B bee1, B bee2) { B b = bee1; bee1.print("bee1 before xchange"); bee2.print("bee2 before xchange"); bee1 = bee2; bee2 = b; bee1.print("bee1 after xchange"); bee2.print("bee2 after xchange"); } } Wednesday, October 12, 2011 public class T { /* basic reference operations */ public static void main(String args) { B b1 = new B(50); b1.print("b1{50} "); D d = new D(51,100); d.print("d{51, 100}"); B b2 = (B) d; b2.print("b2{51,100} which is d"); b2 = b1; b2.print("b2{50} which again b1 again"); b1.print("b1{50} before xchange"); d.print("d{51,100} before xchange"); xchangeWrong(b1, (B) d); b1.print("b1{50} after xchange"); d.print("d{51,100} after xchange"); } public static void xchangeWrong(B bee1, B bee2) { B b = bee1; bee1.print("bee1 before xchange"); bee2.print("bee2 before xchange"); bee1 = bee2; bee2 = b; bee1.print("bee1 after xchange"); bee2.print("bee2 after xchange"); } } B 50 b1 Wednesday, October 12, 2011 public class T { /* basic reference operations */ public static void main(String args) { B b1 = new B(50); b1.print("b1{50} "); D d = new D(51,100); d.print("d{51, 100}"); B b2 = (B) d; b2.print("b2{51,100} which is d"); b2 = b1; b2.print("b2{50} which again b1 again"); b1.print("b1{50} before xchange"); d.print("d{51,100} before xchange"); xchangeWrong(b1, (B) d); b1.print("b1{51} after xchange"); d.print("d{51,100} after xchange"); } public static void xchangeWrong(B bee1, B bee2) { B b = bee1; bee1.print("bee1 before xchange"); bee2.print("bee2 before xchange"); bee1 = bee2; bee2 = b; bee1.print("bee1 after xchange"); bee2.print("bee2 after xchange"); } } B 50 b1 D 51, 100 d Wednesday, October 12, 2011 public class T { /* basic reference operations */ public static void main(String args) {...
View Full Document

Page1 / 101

misc - Wednesday, October 12, 2011 Parameter passing in...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online