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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

boxing confuses

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
hi, somebody explain me this program, especially line 9, this question from Inquisition scjp6 trail set mock exam,


public class testboxing
1. {
2. public static void main(String []as)
3. {
4. String []words = new String[]{"aaa","bbb","ccc","aaa"};
5. Map<String,Integer>m = new Treemap<String,Integer>();
6. for(String word:words)
7. {
8. Integer freq= m.get(word);
9. m.put(word,freq==null? 1:freq+1);
10. }
11. system.out.println(m);
12. }
13. }

the answer is {aaa=2,bbb=1,ccc=1}

is that question mark in line 9 is printing mistake, if it is, i cant understand that boxing & unboxing clearly,
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
No, the ? is not a mistake. There isn't a lot about it in the Java Tutorials, but it is half of the conditional operator (also called the ternary operator). It is a bit like having a tiny if-else inside a statement. I found some other links SAMS and Wikipedia.

Go through that program with a pencil and paper and write down what it does. You should be able to work it out easily. Try changing words to args, then you can pass several words in via the command-line arguments.

What boxing/unboxing means is that you can swap Integers and ints around, like this:Not a very useful piece of code, but it shows boxing in action.
 
Senthil Kumaran
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
are ternary operators in scjp 6, tell me the links explain me more clearly about boxing,
 
Ranch Hand
Posts: 48
1
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
The program is simple...

Line 4: Creates ...
Line 5: Creates ...
Line 6: Starts ...
Line 8: Since ...
Line 9: String .


This keeps on . . . and the following steps take place...

1: value from freq
2: now ...
3: since it is treemap ...

Line 11: Produces the output
Hope you are satisfied with my solution if it is not too complex...

Do give feedback...
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please, Vishal Srivastav, don't simply give out answers and explanations like that. It does not help the poster at all. Have a look at the contents page for the Beginners' forum.

I have felt obliged to delete the solution.

CR
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Its not only boxing and unboxing, its about put(key, value) method of map too. If the map previously contained a mapping for any key, the old value is replaced. This is happening while putting "aaa" string again to the map.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Don't know about the exam, but the ternary operator is a basic operator which any programmer ought to know about.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Do not post the same question in more than one forum; carefully choose one forum. I'm closing this one. Continue in the copy in the SCJP forum.
 
    Bookmark Topic Watch Topic
  • New Topic