Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TypeSafety differences

 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, What is the difference between Line 5 and Line 6 besides Compilation warnings at Line 5.
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saibabaa Pragada wrote:Hi, What is the difference between Line 5 and Line 6 besides Compilation warnings at Line 5.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ankit. I got your point.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic