what is the difference between? (polymorphism) [duplicate] - java

This question already has answers here:
Type List vs type ArrayList in Java [duplicate]
(15 answers)
Closed 2 years ago.
What is the difference between
List<String> lst = new LinkedList<>();
and
LinkedList<String> lst = new LinkedList<>();

There's no difference other than convenience - in the first case you can change LinkedList to other List implementation (such as ArrayList), if you find that it works better in your application, for example, without the need to change any other code.

Related

ArrayList and List declaration [duplicate]

This question already has answers here:
What is the difference between List and ArrayList? [duplicate]
(1 answer)
What does it mean to "program to an interface"?
(33 answers)
Closed 2 years ago.
So I started learning Java about a month ago. And there is this one thing that I can't quite put my finger on.
What is difference between
List<String> list1 = new ArrayList<>();
and
ArrayList<String> list2 = new ArrayList<>();
I get that in list1 declaration is done by using name of Array class and initialization by using name of ArrayList class, and in list2 declaration and intialization both are done by using name of ArrayList class. But what is the difference if there are any.
And even when I make object for inherited class and do the same thing, declaration using name of one class and initializing using name of other. Is there any difference apart form the fact that the code looks different??
Ps : Kinda new to all the terminology and learned most stuff from books, oracle website and stuff but not completely from one source so if i made any mistake please correct me :)
ArrayList is contained under the List interface. When you use ArrayList, you are adding more specification and using ArrayList methods.
See Polymorphism

ArrayList list=new ArrayList(); List list=new ArrayList(); What's the difference between the two? [duplicate]

This question already has answers here:
What does it mean to "program to an interface"?
(33 answers)
Type List vs type ArrayList in Java [duplicate]
(15 answers)
Closed 3 years ago.
I'm a beginner.i can't understand the difference between "ArrayList list=new ArrayList();" and " List list=new ArrayList();".please help me.Thank you very much!
List and ArrayList are the members of the Collection framework. List is a collection of elements in a sequence where each element is an object and elements are accessed by there position (index). The primary difference between List and ArrayList is that List is an interface and ArrayList is a class

How should I make a new instance of an ArrayList? [duplicate]

This question already has answers here:
What is the point of the diamond operator (<>) in Java?
(7 answers)
Closed 3 years ago.
I would like to make an ArrayList which contains Objects.
ArrayList<Object> objects = new ArrayList<Object>();
The code is underlined and NetBeans says: "Redundant type arguments in new expression (use diamond operator instead)."
What does it mean?
It means you can rewrite the line of code like this:
ArrayList<Object> objects = new ArrayList<>();
and it will work the same.

What is the Advantage of creating ArrayList in java by the below mentioned way [duplicate]

This question already has answers here:
What is the difference between declaring List<Integer> vs ArrayList<Integer>?
(7 answers)
Closed 8 years ago.
I am new to Java. What is the advantage or difference of creating ArrayList by:
List<String> a = new ArrayList<String>;
over
ArrayList<String> b = new ArrayList<String>();
You're decoupling your code from a specific implementation of an interface, see this answer for more detail:
Polymorphism: Why use "List list = new ArrayList" instead of "ArrayList list = new ArrayList"?

What's the main reason to prefer List(interface) over Linkedist etc(implementations) [duplicate]

This question already has answers here:
What does it mean to "program to an interface"?
(33 answers)
Closed 8 years ago.
Suppose we wanna define and use a LinkedList in our code.
I would define it this way:
LinkedList<String> list= new LinkedList<>();
Why some people use the interface List
List<String> list = new LinkedList<>();
to define a LinkedList? What are the advantages?
If you use following :
List<String> list = new LinkedList<>();
Then everywhere, in your code, you just using List<String> list. It means, that if you want use ArrayList, the only thing you have to do is to change one line : List<String> list = new ArrayList<>(); and it will work perfectly in all other code, no matter how complex it is.
If you are using LinkedList, you can have huge problems with switching to something else.

Categories