Two other areas of confusion for me on the SCJA exam. Noted in a sample question I went through recently was the JavaBean naming convention of using "is" for the public accessor or mutator method for a boolean (which one wasn't made clear). I haven't seen any deviation from set/get in my studies elsewhere but I'm assuming this is accurate. If so, is this intended to be used for the accessor or the mutator?
My second knowledge gap is about the Wrapper Classes. Wikipedia states that all of the Wrapper Classes are immutable. I've not worked with the wrapper classes before but from my (probably poor) understanding of immutability in OOP shouldn't that mean that if you say declared:
Integer i = 10; i++; // this doesn't work?
or are there only certain other properties of the Wrapper Classes that are immutable?
Originally posted by Tristan Rouse: ...shouldn't that mean that if you say declared:
Integer i = 10; i++; // this doesn't work? ...
As you know, a wrapper instance is immutable, so once the object is created, it cannot be modified. However, a variable that points to a wrapper instance can be reassigned to point to a different wrapper instance.
In this example, autoboxing is moving a lot of steps behind the scenes. When you call i++, what's really happening is:
The Integer instance is automatically unboxed to a primitive int.
The primitive int is incremented by the ++ operator.
The primitive int is automatically boxed to a different Integer instance.
The new Integer instance is assigned back to the variable.
Or to put this in code without boxing/unboxing, it would look something like this...
[ March 12, 2008: Message edited by: marc weber ]
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Leroy J Brown
Joined: Dec 02, 2007
Thanks, Marc. Would anyone like to help me out with the accessor/mutator naming conventions for boolean type variables?
I'm not very good with JavaBeans, but my understanding is the "is<Property>" naming convention can be used for accessing (getting) a boolean property. This can be used instead of, or in addition to, a regular "get<Property>" method.
To set a boolean property, "set<Property>" is still used.