You have made two incorrect assumptions:
1. boolean and double are primitive types, not objects. They are not immutable. Their corresponding object wrappers Boolean and Double are immutable, however.
2. A variable that holds a reference to an object is not the same as the object itself. For example:
String s = "Hello, world";
String t = "Goodbye, world";
String u = s;
System.out.println(u); // Prints "Hello, world"
u = t;
System.out.println(u); // Prints "Goodbye, world"
The String variable u first refers to the String whose value is "Hello, world", but you can easily change it to point to a different string. You have not changed the String object "Hello World" at all - it is indeed immutable.
Similarly:
String s = "";
s = s + "Hello";
s = s + " ";
s = s + "World";
has a number of different String objects, all of them immutable. Each time you assign a new value to s, it is simply pointing to a different object.
------------------
Phil Hanna
Author of :
JSP: The Complete Reference
Instant Java Servlets [This message has been edited by Phil Hanna (edited April 15, 2001).]