File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Niko Question

 
Simran Dass
Ranch Hand
Posts: 183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

class Test2<String> {
String my = "Hello!";
}

Cannot understand why above code gives Compiler Error -

niko1.java:17: incompatible types
found : java.lang.String
required: String
String my = "Hello!";
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that declaration, <String> doesn't mean java.lang.String, you are creating a new type, just like <T>, so in the class, when you use String, it will not represent java.lang.String. The code actually can be written like this too
 
Sumukh Deshpande
Ranch Hand
Posts: 87
Firefox Browser IntelliJ IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit then what should be the correct syntax to achieve what Simran Dass is trying to do?
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sumukh Deshpande wrote:Ankit then what should be the correct syntax to achieve what Simran Dass is trying to do?

 
Rohan Kayan
Ranch Hand
Posts: 123
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If I am not wrong. the generic type "String" is creating confusion , we generally use <T> or some other character for generic types , in above code we are using "String" to represent generic type.
 
Vivek K Singh
Ranch Hand
Posts: 85
Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the modified code sample by Ankit:



The generic type String will never be used as the type parameter String is hiding the type String.

The code sample compiles as the we gave the full reference to java.lang.String but in your code you will never be able to use the generic type "String".
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic