--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 , primative2
immutable1 , immutable2
mutable1 , mutable2
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 ;
int primative2 rimative1;
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 ew Integer(5);
Integer immutable2 mmutable1;
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 ew IntWrapper(3);
IntWrapper mutable2 utable1;
mutable1.increment();
System.out.println( "mutable1 + mutable1 + ", mutable2 +
mutable2 );
}
public int num;
public IntWrapper( int num ) { this.num um; }
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--