-5

I giving a mock test on SCJP. I encounter two different question having the statements as

ArrayList<Integer> arr = new ArrayList<Integer>();

and

ArrayList arr = new ArrayList();

1) What is the differnce between these two?

My Analysis=> first can store Integerand its subclasses. and the second can store Object and its subclasses.

2) Can we make object without <> of any generic class?

dead programmer
  • 4,223
  • 9
  • 46
  • 77
  • 7
    The difference is in [searching](http://docs.oracle.com/javase/tutorial/java/generics/why.html) and [not searching](http://stackoverflow.com/questions/28041224/what-is-the-difference-between-arraylist-with-and-without-operator). – TheLostMind Jan 20 '15 at 09:02
  • I suggest you learn by trying it. Learning from a book is one thing but you will remember it better if you try it. – Peter Lawrey Jan 20 '15 at 09:04
  • Yes you can make object without generic classes. The main difference is you are limiting the adding and searching of the data to the specified type rather than all kind of data. The usage of generics is the proper way of implementing Collections as they will only create objects which are specific to the requirements. – Rohith K Jan 20 '15 at 09:05
  • possible duplicate of [What's the difference between raw types, unbounded wild cards and using Object in generics](http://stackoverflow.com/questions/7360594/whats-the-difference-between-raw-types-unbounded-wild-cards-and-using-object-i) – RealSkeptic Jan 20 '15 at 09:10

3 Answers3

2

1) What is the differnce between these two?

You already find the answer yourself. I suggest you to go deeper in the Java documentation to look for details...

2) Can we make object without <> of any generic class?

IMO, you should always use the first approach:

ArrayList<Integer> arr = new ArrayList<Integer>();

or better

List<Integer> arr = new ArrayList<Integer>();

because you make clear to the reader what you intent to put in the ArrayList.

Stephan
  • 41,764
  • 65
  • 238
  • 329
0

1) What is the differnce between these two?
ArrayList<Integer> arr = new ArrayList<Integer>(); This list store Integer class objects.

ArrayList arr = new ArrayList(); - This list is generic list it will store all type of objects.

2) Can we make object without <> of any generic class?

<> is a generics which introduced by Java 5
Generics add stability to your code by making more of your bugs detectable at compile time.

when you are adding object in collection its become error prone when you don't know which type of objects your collection takes. and which type of object you have to retrieve from that collection.

So, It's better to use generics, Read more about Generics

atish shimpi
  • 4,873
  • 2
  • 32
  • 50
0

Short answer: ArrayList without <> is basically equivalent to ArrayList<?> which means that the type of the List's content in not known at compile time.

Due to type erasure at runtime there's no difference between the generic and non-generic version: they are just list containing objects.

What you gain using the generic (with <>) version is that the compiler can do some static type checking assuring that your code is sound at least at the type level.

As per your second question, yes. You can create non-generic version of any generic class, although this is generally a bad practice both for the clarity of your code as well as its safeness.

nivox
  • 2,060
  • 17
  • 18