Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes TypeSafety differences Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "TypeSafety differences" Watch "TypeSafety differences" New topic
Author

TypeSafety differences

Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi, What is the difference between Line 5 and Line 6 besides Compilation warnings at Line 5.
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

Saibabaa Pragada wrote:Hi, What is the difference between Line 5 and Line 6 besides Compilation warnings at Line 5.


SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Usually For the Collections, if the type information is not specified- The compiler gives a warning( after Generics were introduced). So always preferred is to use Type information along with the Collections.


Mohamed Sanaulla | My Blog
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi Prasad, you mentioned "the typing information does not exist at runtime and can take ANY object". I agree with "the typing information does not exist at runtime".

Prasad Kharkar wrote:

But here we can add only integers in either case and wanted to figure out the difference between these 2 options besides compile warning in first case..
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

At runtime, there is no generics whatsoever. Generics provides type safety only at compile time. So if I write this code, compiler will warn me at compile time but it will run fine
Now the compiler warns you when assigning a raw collection to a typed one because the compiler knows that it can break type safety. Like this code will break type safety


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi Ankit, Your explanation is good. My question is different.
In your example on Left Hand side, there is no typesafe code. But, in my example there is type safety in both cases on left side. Maybe I need to change the question. What is the difference between
1. typesafety defined on Left hand side and type safety NOT DEFINED on right hand side
2. typesafety defined on Left hand side and type safety DEFINED on right hand side

I feel both are absolutely same during compile time and runtime in functionality wise(I mean code will not break in any case). Even though we get compile WARNING for Option 1, It should not be matter. Please correct me if I am wrong.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Saibabaa, my 2nd code uses a type safe collection on the left hand side and a raw collection on the right hand side (line 3). If you are creating a new collection, then there is no difference in both the scenarios that you mentioned
Both will work the same in this case (except for warning on line 1). The reason the compiler issues a warning at the 1st line is explained in my previous post. The compiler is only concerned that you are assigning a raw collection to a type safe collection at line 1. In this case we just created the raw collection and it doesn't have any other reference so type safety can't be broken but it can be broken as I showed in the example...
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Thank you Ankit. I got your point.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TypeSafety differences