• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Hi

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basket b1 = new Basket<Fruit>(); is it valid declaration
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends on how you declare your class.
If your class have a generic type, the declaration is valid.
Like this:

 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Kota,

welcome to the Ranch!




Maybe this thread can clear some doubts:
https://coderanch.com/t/265010/java-programmer-SCJP/certification/generic


Yours,
Bu.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Burkhard,

I have gone through the link provided by you on Generics and would like to check if I had understood the concept well. So now I believe that making the Object reference typesafe would safe guard the collection. In the other terms

If we say List<Integer> l=new ArrayList() // This will save us, from adding bad data to the ArrayList.

but , if we say List l=new ArrayList<Integer>() // This will warn us for the bad data, but would not really stop us from adding it.

So when the first case is true enough, out of curiosity what is the advantage of having typesafe declaration on both sides. What is the double check we are having by declaring the type safe and both sides.

I read generics couple of times, but sometimes its getting too harder for me to memorize these simple concepts. Any tips N tricks
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy!

Chandra asked:


So when the first case is true enough, out of curiosity what is the advantage of having typesafe declaration on both sides. What is the double check we are having by declaring the type safe and both sides.



Or : what is the advantage of
List<Integer> list = new ArrayList<Integer>();
compared to
List<Integer> list = new ArrayList();


The only advantage is that the first version does not produce a warning.

Therefore it is better to use the upper version. You would never want to write code that gives you warnings. But there is a case when you are forced to do something like the lower line.
You would never use the lower line as it is there (because the warning could be easily avoided) but imagine you have an old legacy class that had been compiled with java1.4:


When you want to use that class, you have to be type unsafe:

In the first line of the main method you have no other choice than to be type unsafe because the Old class in not generic.
The code also shows a problem, because it compiles. It compiles, because the Old class is allowed to add a String to the ArrayList. But at runtime you would get a class cast exception.


If you made up a new version of class Old, the method signatures for the two methods would take different type parameters (Integer for the getFirstFive and String for addCowboy).
With the new Old ( ) class, the addCowboy line would not longer compile.
So there would be no chance of a runtime exception, you have to repair the code.


And last point:
List<Integer> list = new ArrayList<Integer>();
compared to
List<Integer> list = new ArrayList();

Behind the scenes, and after type erasure, both objects are exactly the same.




Yours,
Bu.
 
expectation is the root of all heartache - shakespeare. tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic