• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Generics  RSS feed

 
Stribor Kab
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have trouble understanding Java Generics. Idea that I can pass any time and it will work is very confusing. so if I have this generic class...



And my main is here...






What would happen if I wanted to pass Integers to this list and add them up? Where is method for that? Since this class can accept anything I want this class do be able to handle what it already does in above but I also want it to be able to be passed Integers and add them up? Sorry if question is stupid I am just trying to wrap my head around this?
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stribor Kab wrote:What would happen if I wanted to pass Integers to this list and add them up? Where is method for that?


Of course, there isn't a method for "pass Integers to this list and add them up". There's a method for passing integers to a Queue<Integer> object, and you should already know what that is. As for "adding them up", since a Queue<T> can be created for any type T it doesn't make sense for Queue<T> to have an "add them up" method. That's because for most classes, adding two objects isn't a meaningful concept.

There's nothing to stop you from writing some code which adds up the integers from a Queue<Integer> object, though. You just wouldn't put that code into the Queue<T> class.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, welcome to the Ranch!
 
Stribor Kab
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:And, welcome to the Ranch!

Thank you :)

Why can't I put this code into Queue class?
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the code which you would put in the Queue<T> class:



I don't know what you mean by "add them up" (since T can be any class whatsoever) so it's up to you to write the code. What would you write there?
 
Stribor Kab
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Here's the code which you would put in the Queue<T> class:



I don't know what you mean by "add them up" (since T can be any class whatsoever) so it's up to you to write the code. What would you write there?


Well maybe ....

 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
   For each object in the list
      Get its int value


Okay. But remember that T is any class which extends Object. So try this: close your eyes and choose a class from the standard API at random. Does it have a method for getting an int value from that class?
 
Stribor Kab
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
   For each object in the list
      Get its int value


Okay. But remember that T is any class which extends Object. So try this: close your eyes and choose a class from the standard API at random. Does it have a method for getting an int value from that class?


Maybe I am overthinking this.  Doesn't any class extend Object.  For example let's say I have class myClass that have int field and method to return that field
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stribor Kab wrote:For example let's say I have class myClass that have int field and method to return that field


But you don't get to choose T. The person using your Queue<T> class gets to choose T. So I choose... say... InputStream. I'm going to create a Queue<InputStream> object. You need to implement your method so it can deal with a queue of InputStream objects. How do you add them up?
 
Stribor Kab
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Stribor Kab wrote:For example let's say I have class myClass that have int field and method to return that field


But you don't get to choose T. The person using your Queue<T> class gets to choose T. So I choose... say... InputStream. I'm going to create a Queue<InputStream> object. You need to implement your method so it can deal with a queue of InputStream objects. How do you add them up?

Well I guess you can't add that up. 
 
Mark Spencers
Ranch Hand
Posts: 51
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you specify generic type on class:

it means that you can instantiate this class wih any type(String,Integer,Object,etc) you like but you cannot modify it after instance was created.
And if your method accepts argument of type T you must use the type when instance is created.
 
Stribor Kab
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What i think i am mainly confusedis that we use generics to prevent run time type errors and eliminate duplicate code.
So if you have class that adds up 2 Integers you can probably use that class also to add up 2 doubles. 
But how is this class helping you wanted to pass objects to this class of type say bankAccount??
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stribor Kab wrote:What i think i am mainly confusedis that we use generics to prevent run time type errors and eliminate duplicate code.

That's right. Using generics makes your code more type-safe; it allows you to, for example, tell the compiler that your List should only contain String objects and not something else. When you use generics, the compiler will check that you don't put anything else than String objects in your list, and you don't need to use casts (which have the risk of causing a ClassCastException when the object is not of the type you cast it to at runtime).

Stribor Kab wrote:So if you have class that adds up 2 Integers you can probably use that class also to add up 2 doubles.

But if there are no constraints at all on the type parameter T, then you can use any type for the type parameter. You could make a Queue<Integer> or a Queue<Double> but you could just as well make a Queue<String> or a Queue<BankAccount>. As already explained, it's not meaningful to define a generic add() method if any type can be used for the type parameter T.

It's possible to define constraints on a type parameter T, for example by expressing that the type T must be a subclass of a specific type:

That way it will be allowed to make a Queue<Integer> or a Queue<Double>, because classes Integer and Double both extend class Number, but it is not allowed to make a Queue<String> or a Queue<BankAccount> because those types do not extend class Number.

It will still be not that easy to make an add() method though, because adding two integers is really a different operation than adding two doubles. (You couldn't use the + operator because that works only on the primitive types int and double and not the wrapper types Integer and Double).
 
Stribor Kab
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
Stribor Kab wrote:What i think i am mainly confusedis that we use generics to prevent run time type errors and eliminate duplicate code.

That's right. Using generics makes your code more type-safe; it allows you to, for example, tell the compiler that your List should only contain String objects and not something else. When you use generics, the compiler will check that you don't put anything else than String objects in your list, and you don't need to use casts (which have the risk of causing a ClassCastException when the object is not of the type you cast it to at runtime).

Stribor Kab wrote:So if you have class that adds up 2 Integers you can probably use that class also to add up 2 doubles.

But if there are no constraints at all on the type parameter T, then you can use any type for the type parameter. You could make a Queue<Integer> or a Queue<Double> but you could just as well make a Queue<String> or a Queue<BankAccount>. As already explained, it's not meaningful to define a generic add() method if any type can be used for the type parameter T.

It's possible to define constraints on a type parameter T, for example by expressing that the type T must be a subclass of a specific type:

That way it will be allowed to make a Queue<Integer> or a Queue<Double>, because classes Integer and Double both extend class Number, but it is not allowed to make a Queue<String> or a Queue<BankAccount> because those types do not extend class Number.

It will still be not that easy to make an add() method though, because adding two integers is really a different operation than adding two doubles. (You couldn't use the + operator because that works only on the primitive types int and double and not the wrapper types Integer and Double).


So I am assuming generic classes are somewhat simple?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!