From: Josh Cheek Date: 2010-02-14T13:25:38+09:00 Subject: Re: Why no ++ and --? --001636b2b0fa52240f047f87e040 Content-Type: text/plain; charset=ISO-8859-1 I thought it might be interesting to see how Java handles this, since it has both primitives and wrappers. Outputs primative1 = 6 , primative2 = 5 immutable1 = 6, immutable2 = 5 mutable1 = 4, mutable2 = 4 public class IntWrapper { public static void main(String[] args) { // Java does not support references to primitive types, so primative2 = primative1 creates a new int // with the same value as primative1. Then when we incrememnt primative1, primative2's value does not change int primative1 = 5; int primative2 = primative1; primative1++; System.out.println( "primative1 = " + primative1 + " , primative2 = " + primative2 ); // because Integer is immutable, immutable1 and immutable2 end up pointing to different objects // and they have different values. This contradicts the C / C++ model, as well as the general object model Integer immutable1 = new Integer(5); Integer immutable2 = immutable1; immutable1++; System.out.println( "immutable1 = " + immutable1 + ", immutable2 = " + immutable2 ); // an example of how mutable objects behave, that shows the difference, in this case, // the object is mutable, so mutable1 and mutable2 both point to the same object // and when it is updated, they both reflect this change // using a method .increment() because java does not support operator overloading. IntWrapper mutable1 = new IntWrapper(3); IntWrapper mutable2 = mutable1; mutable1.increment(); System.out.println( "mutable1 = " + mutable1 + ", mutable2 = " + mutable2 ); } public int num; public IntWrapper( int num ) { this.num = num; } public void increment() { num++; } public String toString() { return Integer.toString(num); } } Ruby avoids the criticism of contradicting the C/C++ model, and general object model because it's immutable classes don't implement self-modifying methods like ++, every method returns a new object. --001636b2b0fa52240f047f87e040--