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

Few very general questions

 
Ranch Hand
Posts: 88
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79959
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Greg Charles
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 88
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ 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...
 
Water! People swim in water! Even tiny ads swim in water:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic