JAVA Why do I get a type error when assigning these variables (T extends Comparable<T>) -


so, let's have these 2 classes.

public class main<t extends comparable<t>> {    public list<t> tolist(t t){       node n = new node();       n.value = t; //this assignment1 gives no issues.        list<t> list = new list<t>();       list.add(n.value); //this assignment2 gives type error.       return list;      } }  class node <t extends comparable<t>> {    t value = null; } 

so, why assignment1 go through without type error, while assignment2 gives me following error:

add (t) in list cannot applied (java.lang.comparable)

it disappears when do:

list.add((t)n.value); 

but understand reason first figure out if above solution correct one.

edit: now, should specify code uses these classes integer objects. so, developer can assume t types same. not sure if code has underlying routine can change values during casting.

node n = new node(); 

should be

node<t> n = new node<t>(); 

if ever refer node without <something> after it, telling compiler ignore all type checking on node. give warnings, should.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -