• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Sybex OCA - chapter 3 #26 - I do not agree key

 
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote this code:
import java.util.*;
public class TestChapter3_26 {
 public static void main(String[] args) {
   List<Integer> ages = new ArrayList<>();
   ages.add(Integer.parseInt("5"));
   ages.add(Integer.valueOf("6"));
   ages.add(7);
   //ages.add(null);
   for (int age : ages) System.out.print(age);
 }
}

respons is:
567

why key is 'D'? I think correct key is 'E'.
 
author & internet detective
Posts: 40211
816
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Confused. There is no "key" in  your code.
 
hiroki inoue
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I do not know very well English.
In the book OCA Oracle Certified Associate Java SE 8 Programmer I STUDY GUIDE, in page 342 the answer of 26 is A, B, D.
I think the answer is A, B, E.
 
Jeanne Boyarsky
author & internet detective
Posts: 40211
816
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, this is from the book. I didn't pick up on that. (I've edited your subject to make it clearer).

Option D says exactly two of the add statements use autoboxing. Option E says exactly three of the add statements use autoboxing.

valueOf() method returns an Integer. Therefore it does not use autoboxing.  The other two do. Hence D is correct.
 
hiroki inoue
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, 'Autoboxing'!
5 <- autoboxing
6 <- boxing
7 <- autoboxing

I understand! Thank you!
 
Jeanne Boyarsky
author & internet detective
Posts: 40211
816
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hiroki inoue wrote:Ah, 'Autoboxing'!
5 <- autoboxing
6 <- boxing
7 <- autoboxing


Not quite. 6 doesn't have autoboxing or boxing. It's already an object so no conversion is needed
 
knowledge is the difference between drudgery and strategic action -- tiny ad
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic