• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generics Raw type  RSS feed

 
Puspender Tanwar
Ranch Hand
Posts: 499
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi ranchers,
I have the code above, and the query is also mentioned in the code in front of respective line. Please answer me .
In case 2, since the type parameter is Object, so the T should be Object and the return type or getkey() must be Object and that line should print Object. why it is printing String ?
Thanks
 
Daniel Cox
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender Tanwar wrote://case 1           //why not giving unchecked warning for this, since type parameter is not given(its much like a raw type) ?

I get this warning
Box is a raw type. References to generic type Box<T> should be parameterized

Puspender Tanwar wrote://case 2   //why its printing "String" , not "Object " ?

Your code is equivalent to
 
Puspender Tanwar
Ranch Hand
Posts: 499
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Cox wrote:I get this warning
Box is a raw type. References to generic type Box<T> should be parameterized

why I am not getting any warning for this 
Daniel Cox wrote:Your code is equivalent to

why String? Is it because the getClass() return the type of the instance, not the variable type. Like here returned type is Object but the actual object type is String
 
Daniel Cox
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender Tanwar wrote:
Daniel Cox wrote:I get this warning
Box is a raw type. References to generic type Box<T> should be parameterized

why I am not getting any warning for this 

When compiling, you can use the -Xlint option to enable all recommended warnings.

When I compile this code

I get this warning

Generics_Test.jpg
[Thumbnail for Generics_Test.jpg]
 
Puspender Tanwar
Ranch Hand
Posts: 499
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got the warning, but we have to explicitly gave the -Xlint. Which is not in case of Box b = new Box("hell"); If both are the Raw types, then why behaving different for warning ?
and what about the second doubt i am having ?
 
Daniel Cox
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender Tanwar wrote:
why String? Is it because the getClass() return the type of the instance, not the variable type. Like here returned type is Object but the actual object type is String

Yes you're correct. The getClass() method returns the runtime type (not the compile-time type).

When you do this

Object a = new String();

the compiler sees the type of this String object as java.lang.Object

For this reason, you'll get a compiler error if you do this

String b = a;

However, when you do this

System.out.println(a.getClass());  // class java.lang.String

The JVM sees the type of this String object as java.lang.String
 
Puspender Tanwar
Ranch Hand
Posts: 499
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Daniel.
what about this 
Got the warning, but we have to explicitly gave the -Xlint. Which is not in case of Box b = new Box("hell"); If both are the Raw types, then why behaving different for warning ?
 
Daniel Cox
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender Tanwar wrote:Got the warning, but we have to explicitly gave the -Xlint. Which is not in case of Box b = new Box("hell"); If both are the Raw types, then why behaving different for warning ?

By default, the Eclipse compiler enables both rawtypes and unchecked warnings; however, javac disables them by default. You have to explicitly use
-Xlint:rawtypes or -Xlint:unchecked or -Xlint

Even though the "unchecked" warning is disabled, the compiler will give you a hint.

You can read more here The Java™ Tutorials: Raw Types
The "unchecked" warning is disabled, by default, though the compiler gives a hint.

 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because using raw types is bad, but not unsafe. Performing unchecked operations is unsafe, so it's important that the compiler gives you a stronger warning.
 
Puspender Tanwar
Ranch Hand
Posts: 499
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephen and Daniel
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!