aspose file tools*
The moose likes Beginning Java and the fly likes boxing confuses Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

boxing confuses

Senthil Kumaran
Ranch Hand

Joined: Aug 24, 2008
Posts: 32
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,
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
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

Joined: Aug 24, 2008
Posts: 32
are ternary operators in scjp 6, tell me the links explain me more clearly about boxing,
Vishal Srivastav
Ranch Hand

Joined: Nov 29, 2008
Posts: 46

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...


Vishal Srivastava, Software Engineer (Android), Paradigm Creatives
SCJP 5, SCWCD 5 - http://in.linkedin.com/in/srivastavavishal
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
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
Mumtaz Khan
Ranch Hand

Joined: Feb 21, 2008
Posts: 53
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.


SCJP 5.0 -- 97%
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Don't know about the exam, but the ternary operator is a basic operator which any programmer ought to know about.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14111
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: boxing confuses