Interview homework: Which is better Java ArrayList creation [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Type List vs type ArrayList in Java
Hi I have been asked the following question in an interview:
Which of the following would you choose recommended when and where?
ArrayList<SomeType> a = new ArrayList<SomeType>(); or
List<SomeType> a = new ArrayList<SomeType>();
I dont know much difference if anyone knows kindly please help. Thanks in advance.

Second is better cause you can simply change underlying implementation from ArrayList to LinkedList (or to any proper implementation) by one line code change.

Related

Run Time Polymorphism in Java Collection framework [duplicate]

This question already has answers here:
What does it mean to "program to an interface"?
(33 answers)
Java - declaring from Interface type instead of Class
(7 answers)
Closed 11 months ago.
Set set= new TreeSet<>();
vs
TreeSet set= new TreeSet<>();
I know we are using the concept of run time polymorphism here, but what is the use of writing this? Is there any advantage to writing one thing over another? both are giving me the correct results. I am learning java right now, help would be great.

Java method().method().method(); How to do this? [duplicate]

This question already has answers here:
How to achieve method chaining in Java?
(4 answers)
Closed 3 years ago.
For example how do I do this?
getServer().getManager().registerNewUser(arguments);
and
getServer().getStats().user(arguments);
Stuff like this, I've tried searching the web but I cant find any tutorials on this,so I'm asking here.
In OOP, in general, this concept is called class composition more on this you can find it here https://en.wikipedia.org/wiki/Object_composition
Also for java and the difference between Composition and inheritance https://www.baeldung.com/java-inheritance-composition

Which one is better to work with, vector or arraylist? [duplicate]

This question already has answers here:
Why is Java Vector (and Stack) class considered obsolete or deprecated?
(5 answers)
Closed 3 years ago.
I have to do a program using arrays of objects to make a person type object that receives ID, name, gender ... etc. which is better to use arraylist or vectors?
ArrayList is better (with Collections.synchronizedList if you need a thread-safe list, which I doubt you do). See this question.

when to use List, when to use LinkedList ?(java) [duplicate]

This question already has answers here:
What does it mean to "program to an interface"?
(33 answers)
Closed 5 years ago.
I was a bit confused, when we use
List<String> lst = new LinkedList<>();
when we use
LinkedList<String> lklst = new LinkedList<>();
At the beginning, I thought they are the same, but today, I realized they are not the same. For example, if I call lst.getFirst() It will tell me there is a error. However, if i do lklst.getFirst(), it works fine. My question is when do we use lklst then? why they are different? Also, does it apply same rule for Map. THanks!
On the left hand side you're declaring the type of the variable, lst. Since lst's type is List you can only access methods of a List, even if the object lst points to is really a LinkedList. There's an inherent tradeoff between declaring a variable of a concrete type like LinkedList (access to more methods / behavior) vs. a more abstract interface (safer, better compartmentalized code).
This is a big topic, and there isn't one simple answer for when to do one vs. the other (though there's lots of advice out there about it!) - you'll need to figure out which is appropriate for your use case.
Effective Java - Item 52: Refer to objects by their interfaces is a pretty canonical citation for this issue, and as the title implies suggest preferring List rather than LinkedList.

is it recommended to keep Java serializationUID unique? What will happen if I give same serializationUID =1 to all Java classes [duplicate]

This question already has answers here:
What is a serialVersionUID and why should I use it?
(25 answers)
Closed 7 years ago.
I was following the article http://javapapers.com/core-java/serialversionuid-in-java-serialization/ to understand Seriliazation.
In this its said that Java serializationUID recommended to be unique. I dont understand the reason for that.
Can someone please explain?
In this its said that Java serializationUID recommended to be unique. I dont understand the reason ...
It's wrong. There is no reason not to use 1L for every class, except in the case where you are retrofitting to a class that was initally defined without one, in which case you have to use what the serialver tool tells you.
As a matter of fact you can't use a unique value for every class, because it's a hashcode.
Don't rely on arbitrary Internet junk like this. Use the Javadoc and the specifications.

Categories