2 Replies Latest reply on May 27, 2008 3:29 PM by 800333

    Integer static valueOf(int) question

      Hi there.
      Im wondering is it at all possible for an Integer reference variable to be returned
      a null by invoking this method from the API:
      Integer myInt = Integer.valueOf(//some int);
      The reason I am asking this question is that my SCJP book asks a question
      about using a method that is similar to the following:
      class BankAccount {
      private Map<String, Integer> accountFunds = new HashMap<String, Integer>();
      public int customerBalance(String custAccount) {
         return accountFunds.get(custAccount);
      public void setCustomerBalance(String custAccount, int balance) {
         accountFunds.put(custAccount, Integer.valueOf(balance));
      Looking at the single return statement in the customerBalance method, my book states that this is inappropriate
      a null cant unbox to 0
      I disagree because if you look at the setCustomerBalance method you are putting an Integer
      instance that is initialized with an int primitive into the Map. So how could it be possible to have
      a null Integer instance in the map if you pass an int primitive to the valueOf method? I can understand
      if you were using a String to instantiate the Integer, and you passed null, that you would get a NumberFormatException or something like that, but about you are using a primitive, not an Object.
      Any clarification greatly received,
      Thanks & best regards.