Java

  1. Home
  2. Computing & Technology
  3. Java
photo of Paul Leahy

Paul's Java Blog

By Paul Leahy, About.com Guide to Java

Answer to Monday's Wrapper Question

Sunday December 14, 2008

Monday's programming question was concerned with creating wrapper classes that correspond to primitive data types. The question was to see if you can figure out the values of the wrapper objects that are created from a series of statements.

The simplest way to confirm the values of the wrapper objects is to display them:

public class Wrappers {

  public static void main(String[] args) {

    Boolean eminem = new Boolean("yes");
    System.out.println("eminem: " + eminem);

    Boolean lLCoolJ = new Boolean("true");
    System.out.println("lLCoolJ: " + lLCoolJ);

    Integer mosDef = new Integer(Integer.MAX_VALUE+1);
    System.out.println("mosDef: " +mosDef);

    Long queenLatifah = new Long(Integer.MAX_VALUE+1);
    System.out.println("queenLatifah: " + queenLatifah);

    Long iceCube = new Long((long)Integer.MAX_VALUE + 1);
    System.out.println("iceCube: " + iceCube);

    Long jayZ = new Long(iceCube++);
    System.out.println("jayZ: " + jayZ);

    Long kanyeWest = new Long(++iceCube);
    System.out.println("kanyeWest: " + kanyeWest);

    Character snoopDogg = new Character("c".replace("c", "d").toCharArray()[0]);
    System.out.println("snoopDogg: " + snoopDogg);

    Double theGame = new Double(Double.POSITIVE_INFINITY);
    System.out.println("theGame: " + theGame);

    Double common = new Double (Math.round(Double.POSITIVE_INFINITY));
    System.out.println("common: " + common);

    Integer nas = 10444;
    Integer trickTrick = 10444;
    Boolean lilWayne = new Boolean(nas == trickTrick);
    System.out.println("lilWayne: " + lilWayne);

    Boolean bowWow = new Boolean(nas.equals(trickTrick));
    System.out.println("bowWow: " + bowWow);
  }
}

The output is:

eminem: false
lLCoolJ: true
mosDef: -2147483648
queenLatifah: -2147483648
iceCube: 2147483648
jayZ: 2147483648
kanyeWest: 2147483650
snoopDogg: d
theGame: Infinity
common: 9.223372036854776E18
lilWayne: false
bowWow: true

Comments:

  • enimen is false because a Boolean object created using a String will always be false unless the String represents true.
  • mosdef becomes -2147483648 because once the maximum value of an int is reached the value of an addition makes the Integer cycle around to its minimum value (e.g., maximum value is 2147483647, add one and you find yourself at the minimum value of -2147483648).
  • even though queenLatifa is a Long object and can handle values greater than an int, the calculation of Integer.MAX_VALUE + 1 happens before the value of the calculation is assigned.
  • iceCube gets the right value because of the explicit cast of Integer.MAX_VALUE to a long. The addition of one is now performed on to a long and can be stored in iceCube.
  • jayZ is the subject of a trick question. Using the postfix version of increment operator (i.e., iceCube++) is perfectly valid but the increment is performed last. Therefore jayZ gets the value of iceCube before iceCube's value is incremented by one.
  • kanyeWest is subjected to even more trickery. This time using the prefix version of the increment operator (i.e., ++iceCube) does happen before iceCube's value is assigned. However, remember that iceCube was also incremented after the creation of jayZ.
  • snoopDogg just shows that String literals can also be treated as String objects. Using the replace method simply changes the "c" to a "d".
  • theGame gets created with the representation of Infinity.
  • lilWayne is assigned the value of false because the "==" operator looks to see if nas and Tricktrick are the same object, not that they have the same value.
  • bowWow does get the value of true because the equals method is used to compare the values of the objects nas and Tricktrick.

Comments

No comments yet. Leave a Comment

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Discuss

Community Forum

Explore Java

About.com Special Features

Java

  1. Home
  2. Computing & Technology
  3. Java

©2009 About.com, a part of The New York Times Company.

All rights reserved.