File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Associate Certification (SCJA,OCAJ 5/6) and the fly likes isOn, more Wrapper Class conversation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Associate Certification (SCJA,OCAJ 5/6)
Bookmark "isOn, more Wrapper Class conversation" Watch "isOn, more Wrapper Class conversation" New topic
Author

isOn, more Wrapper Class conversation

Leroy J Brown
Ranch Hand

Joined: Dec 02, 2007
Posts: 71
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

Joined: Aug 31, 2004
Posts: 11343

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
    Ranch Hand

    Joined: Dec 02, 2007
    Posts: 71
    Thanks, Marc.
    Would anyone like to help me out with the accessor/mutator naming conventions for boolean type variables?
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    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

    Joined: Oct 01, 2005
    Posts: 9
    set and get are the standard mutator and accessor methods, respectively.

    isXXX could be used for boolean type accessor method optionally.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: isOn, more Wrapper Class conversation
     
    Similar Threads
    Strings are immutable
    Name the mutable and immutable Classes
    Dan's exam: Mutator method
    How to easily tell if class is immutable
    primitive data type manipulation in wrapper classes