Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

isOn, more Wrapper Class conversation

 
Leroy J Brown
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks in advance
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
     
    Leroy J Brown
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks, Marc.
    Would anyone like to help me out with the accessor/mutator naming conventions for boolean type variables?
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.

    (See Sun - JavaBeans Specifications.)
    [ March 12, 2008: Message edited by: marc weber ]
     
    Maniganda Prakashh
    Greenhorn
    Posts: 9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    set and get are the standard mutator and accessor methods, respectively.

    isXXX could be used for boolean type accessor method optionally.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic