• Post Reply Bookmark Topic Watch Topic
  • New Topic

confused with Java Generics  RSS feed

 
Indranil Sinha
Greenhorn
Posts: 20
IntelliJ IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi experts I am new to Java and was a php developer. I am really messed with Java Generics.
Can anybody help me with this line of code? what it means how to understand these?



excuse me if the question is too novice.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indranil Sinha wrote:Hi experts I am new to Java and was a php developer. I am really messed with Java Generics.
Can anybody help me with this line of code? what it means how to understand these?



excuse me if the question is too novice.


There is no such thing as too novice; we've all been there. And generics are confusing even for experienced developers.

You probably know that this line is a method signature that is going to return an object of type PagedData. We don't know at this point what that object is and it doesn't matter. The generic in brackets means that the object of type PagedData will contain and can only contain an object of type BankAccountListModel. It can't contain a String or an Integer or an ArrayList or any other object. Only a BankAccountListModel.

Why do this? Good question. It prevents you from inadvertently storing the wrong kind of object in PagedData somewhere in your code because the generic declaration allows the compiler to catch the error at compile time instead of waiting until your program crashes and burns at runtime when you accidentally try to store a String (or whatever) in the PagedData object. In short, it turns possible runtime errors into compile time errors where they can be dealt with sooner and makes the code more robust.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Incidentally, this document is considered the best reference on Java generics, but just bookmark it for later. Don't try to absorb it now. It's 297 pages which by itself tells you how complex generics are. Personally, I think there is something wrong with a "feature" that requires almost 300 pages to explain, and I know that I don't always use generics the way I should. I just use the very basics like the method signature you asked about.
 
Indranil Sinha
Greenhorn
Posts: 20
IntelliJ IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:
Indranil Sinha wrote:Hi experts I am new to Java and was a php developer. I am really messed with Java Generics.
Can anybody help me with this line of code? what it means how to understand these?



excuse me if the question is too novice.


There is no such thing as too novice; we've all been there. And generics are confusing even for experienced developers.

You probably know that this line is a method signature that is going to return an object of type PagedData. We don't know at this point what that object is and it doesn't matter. The generic in brackets means that the object of type PagedData will contain and can only contain an object of type BankAccountListModel. It can't contain a String or an Integer or an ArrayList or any other object. Only a BankAccountListModel.

Why do this? Good question. It prevents you from inadvertently storing the wrong kind of object in PagedData somewhere in your code because the generic declaration allows the compiler to catch the error at compile time instead of waiting until your program crashes and burns at runtime when you accidentally try to store a String (or whatever) in the PagedData object. In short, it turns possible runtime errors into compile time errors where they can be dealt with sooner and makes the code more robust.


Thanks a lot.... for the descriptive and to the point answer.
 
Indranil Sinha
Greenhorn
Posts: 20
IntelliJ IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:Incidentally, this document is considered the best reference on Java generics, but just bookmark it for later. Don't try to absorb it now. It's 297 pages which by itself tells you how complex generics are. Personally, I think there is something wrong with a "feature" that requires almost 300 pages to explain, and I know that I don't always use generics the way I should. I just use the very basics like the method signature you asked about.


Thanks Kevin... it's an wonderful document.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!