Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Wrapper classes

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Wrapper classes

    Why does the following code compile?
    if Double is a class, why am I able to assign the return value of test() to a double?
    if Integer is a class, why am I able to pass just 12, and not "new Integer(12)"?

    public static void main(String[] args) {
    double x = test(12);
    }

    public static Double test(Integer a) {
    a = 13;
    return new Double(34.12);
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,266
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Wrapper classes


  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,149
    Thanks
    48
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Wrapper classes

    The compiler has a feature: autoboxing that will do many conversions so that you don't have to understand what needs to be done or what was done to change code into legal Java code.
    It allows sloppy/lazy programmers to write code that normally wouldn't compile as coded. There are many cases where autoboxing fools programmers and makes for bugs in the code until the programmer understands what the compiler has done to him.
    One example:
    anArrayListRef.add(123);     //  add an object to an arraylist
    anArrayListRef.remove(123);  // remove that element from the arraylist
    The arg for the first arg is autoboxed to Integer(123). The second arg is the index of the element to be removed. It is not autoboxed.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Nov 2012
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Wrapper classes

    so this is for collection section ?

Similar Threads

  1. wrapper class
    By mysterious.ek in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 17th, 2012, 10:27 AM
  2. Java Wrapper Classes
    By tcstcs in forum Java Theory & Questions
    Replies: 4
    Last Post: June 15th, 2012, 09:39 AM
  3. JAVA .NET WRAPPER
    By kafka82 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 18th, 2011, 07:46 AM
  4. Need a jar wrapper for Mac
    By namhm in forum Java SE APIs
    Replies: 2
    Last Post: October 10th, 2011, 11:31 AM
  5. the definition of a wrapper class
    By nickypass in forum Java Theory & Questions
    Replies: 1
    Last Post: October 17th, 2010, 01:54 AM