Vinitha Palani wrote:
1)When a copy of 'li' is passed to the parameter 'list' in addstuff() ,wont 'list' also become type-safe.?
Apparently, it isnt - so what is the reason -type erasure?? ...please could you explain ...
2)What is actually happening in the 'foreach'...?
where is the casting from Integer to String taking place....wont the elements in 'li' be assigned to Object o ,iteratively?
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Vinitha Palani wrote:
but i am still geting the exception ...did you change anything?
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Be nice !
Dwijen Bhattacharjee wrote:
When you are passing a generic list to a non generic method all the security will vanish there. You can add anything in that list.
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Vinitha Palani wrote:
But the "int" will get Autoboxed wont it?
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Sagar Rohankar wrote: when you say, 'List list', its something like defining, 'List <Object> list',
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg wrote:
Sagar Rohankar wrote: when you say, 'List list', its something like defining, 'List <Object> list',
Well this is a minute detail but this is not true. You can do this
but you can't do this
Ankit Garg wrote:
.. but I couldn't stop myself from pointing this out as I don't want anyone to have confusions or misconceptions. Actually List list is equivalent to List<? extends Object>...
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Sagar Rohankar wrote:what is the difference between List<Object> and List<? extends Object>
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg wrote:Chaitanya did you try your code?? The exception doesn't come when you add the element. The exception comes while iterating the List...
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg wrote:
Here you won't get any warnings. So it is the responsibility of the compiler to make sure, that no ClassCastException occurs at runtime. That's why it disallows you to add elements to it.
Ankit Garg wrote:
Taking this same example, here, The actual type of the ArrayList is String, but we are using it with a reference of type ? extends Object,
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Sagar Rohankar wrote:
Ankit Garg wrote:
Taking this same example, here, The actual type of the ArrayList is String, but we are using it with a reference of type ? extends Object,
If that's so, then why this code works, the List#add() methods works here.
Sagar Rohankar wrote:
Ankit Garg wrote:
Here you won't get any warnings. So it is the responsibility of the compiler to make sure, that no ClassCastException occurs at runtime. That's why it disallows you to add elements to it.
So whats the use of such declaration, Whats the use any facility which we can't use. Just to avoid compile time warnings ?, which, an average programmer with knowledge of Java generics, can possibly avoid them.
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg wrote:Actually there is no direct way of representing an untyped List using generics.
Ankit Garg wrote:
Well the use is to retrieve elements. Suppose you want to create a method that will accept a List of Serializables and serialize the objects, then you can do this
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Ankit Garg wrote:I found the thread. Here you can find a good discussion of the problem that you are facing...
All code in my posts, unless a source is explicitly mentioned, is my own.
The important thing is not to stop questioning.
Albert Einstein
Vinitha Palani wrote:
Any answers for the questions in my last post..?
Vinitha Palani wrote:
So does this means that type safety is decided by the reference variable ..
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
kalpita lawande wrote:I am kalpita lawande.I am planning to give scjp 1.5 in june 2009.
can you please tell me which chapters are important and how many mock exams should i give?
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Sagar Rohankar wrote:
List list =new ArrayList<String>();
From Java 5, consider this statement as,
List<Object> list =new ArrayList<String>();
and you know this is valid, Now one important point to remember, this is for sake of understand, I didn't found any doc/guide which adhere or support this my assumption.
SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Vijitha Kumara wrote:Again this might be miss-leading someone as List<Object> list =new ArrayList<String>() is not allowed since that will not compile.
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Dwijen Bhattacharjee wrote:Now I have a doubt --
whatever the code Vinitha posted at the beginning I simply copy pasted the same code in eclipse and it's generating the output-
Me
myself
and
43
the same code If i compile and run from command line its generating the output--
Me
myself
and
Exception in thread main java.lang.ClassCastException:java.lang.Integer
Both using jdk1.5.0_09.If The problem is with the for..each loop . Then why its generating two different output?
First I thought may be problem with my configuration . But then i thought if both the version is 1.5 then it should present the same output in all the places. Please clarify the doubt.
All code in my posts, unless a source is explicitly mentioned, is my own.
Peter Murach wrote:Hi all,
I would like to add my penny to the discussion. This is my first post so be gentle.
is simply syntacticly equal to
Because 'li' is of List<String> reference type when passed to the addStuff method it is erased of its own type safety so we can add then anything to it. We also need to note that we pass by value, so when the method addStuff finishes, 'li' contains all the four elements and in main method still refers to the same List<String> type which is passed to the expression part of the foreach statement. 'li' returns Iterator<String> in our foreach loop and casts every element to String as shown above. Of course the cast of Integer type will throw runtime exception.
All code in my posts, unless a source is explicitly mentioned, is my own.
Rototillers convert rich soil into dirt. Please note that this tiny ad is not a rototiller:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|