Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Few very general questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Few very general questions" Watch "Few very general questions" New topic
Author

Few very general questions

Faisal Fuad
Ranch Hand

Joined: Jul 05, 2011
Posts: 79

Hi...can any one please help me to understand what the following 2 lines are doing?


Few additional stuff are....In above number (2), Why List and ArrayList are going together? I saw most of the time Object types are the same in both sides of the equal (=) sign. Like as follows:

>> Then why here List and ArrayList are together. Just worming up myself for Java so need to learn more...Please help me to make myself clear.

Moreover, Java Wrappers and Boxing mechanism are very confusing to me. I saw some sample examples but not clear much. Can any one please present those two very important concepts based on their absolute precedence? That will be a great help.

Thanks everyone in advance.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2835
    
  11

The first line is instantiating a TreeMap that uses Integers as a key to its stored collection of Persons. The second line is doing something similar with ArrayList.

For your second question, ArrayList is an implementation of List, so by the rules of object-oriented programing, you can assign an object of type ArrayList to a List variable. It's often a good idea to do just that. List defines the interface ... that is, what you can do with the object, like add an element, retrieve an element, remove an element, etc., but doesn't contain the implementation code that actually performs these tasks. ArrayList implements the interface in one way, and LinkedLinked in a completely different way. Once you've created the object though, you don't care how it implements the interface; you just care what it does for you.

You often hear advice to program to the interface. If everything using the ArrayList object just specified that it is using a List, then you could easily change the creation line to create a LinkedList instead, and none of your other code would have to change. That's very useful if you're passing the object as a parameter to other methods, or returning it to a caller.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37976
    
  22
Please always tell us that the thread is about.

Both those lines are similar. What you are doing is declaring the two variables as instances of the interface (List<E> and SortedSet<E>). That means you can assign anything which implements those interfaces to those variables. If you look up those two interfaces, you find a list of classes they know about which implement them, and you can see whether the classes you have are subtypes of those interfaces.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Faisal Fuad wrote:Hi...can any one please help me to understand what the following 2 lines are doing?


(a) Creates a TreeMap object which uses Integers as Keys, and Persons as Values.
(b) Creates a variable of the interface type SortedMap (which also expects Integers as Keys, and Persons as Values) named people.
(c) Assigns the reference to the TreeMap object as the value of the people variable.
Faisal Fuad wrote:


(a) Creates a ArrayList object which is intended to hold Person objects.
(b) Creates a variable of the interface type List (which also expects to hold Person objects) and named it personList
(c) Assigns the refrence to the ArrayList object as the value of the personList variable

Faisal Fuad wrote:Few additional stuff are....In above number (2), Why List and ArrayList are going together? I saw most of the time Object types are the same in both sides of the equal (=) sign. Like as follows:

>> Then why here List and ArrayList are together. Just worming up myself for Java so need to learn more...Please help me to make myself clear.


java.util.List is an interface type which defines the methods (and behavior) or all different types of Lists. The java.util.ArrayList is a specific implementation of the interface. Because ArrayList implements List and therfor is-a, you can use an ArrayList as a List: store an ArrayList in a List reference, pass an ArrayList into methods that expect Lists, etc... In many cases it is a good idea to refer to an Object as the interface type (List) rather than the concrete implementation (ArrayList) because it is more flexible - you could change the actual implementation from ArrayList to LinkedList, for example, and not have to change anything except the initialization line (new ArrayList<Person>() becomes new LinkedList<Person>() and nothing else changes).


Faisal Fuad wrote:Moreover, Java Wrappers and Boxing mechanism are very confusing to me. I saw some sample examples but not clear much. Can any one please present those two very important concepts based on their absolute precedence? That will be a great help.

Thanks everyone in advance.

I am not clear what you mean by 'absolute precedence'. Can you be specific as to what you are confused about?


Steve
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2835
    
  11

Faisal Fuad wrote:Hi...can any one please help me to understand what the following 2 lines are doing?
[code=java]

Moreover, Java Wrappers and Boxing mechanism are very confusing to me. I saw some sample examples but not clear much. Can any one please present those two very important concepts based on their absolute precedence? That will be a great help.


Do you understand the idea of primitives, like int, boolean, double, and so on? Wrappers are just classes that "wrap up" one of those primitives into an Object type. Sometimes you'll be in a situation where you need to work with an object, and not a primitive, like storing things in many types of collections. In that case you wrap up your primitive, say the int 4 into an Integer object, and go ahead an use it. Other times you need the primitive and not the object, like when you are performing arithmetic. It used to be we had to explicitly write the code to wrap a primitive when we needed an object, and unwrap the object when we needed a primitive again. That was tedious, and was solved when auto-boxing was added to the language. Essentially, the compiler just inserts the conversion code for you.
Faisal Fuad
Ranch Hand

Joined: Jul 05, 2011
Posts: 79

@ Campbell Ritchie: Sorry Ritchie. Yes you are right. Next time for sure I will take care of my posting.

@ Luke: Thank you very much for such wonderful reply. Everything was to the point and very clear. And about the very last point, Charles got my problem and replied perfectly.

@ Charles: I found your answer very nice about the Wrappers. Thank you as well. I don't know why I become bit shaky when seeing "Integer" rather than "int" beside a variable. Funny...!!! Yes...for sure I need to make my hand very dirty so that these are the issues will be comfortable to me quickly.

Thanks all of you. Have a great weekends...
 
Consider Paul's rocket mass heater.
 
subject: Few very general questions
 
Similar Threads
FileWriter wait to finish
Generics..
about abstract class
Generics - need help
Compile fails (generics/collections)