Java generics - type mismatch from T to T

sreenisatish

I was trying to implement a basic Binary Search Tree (irrelevant to the question). This is what I have:

public class BSTNode<T> {
    public T data;
    public BSTNode<T> left;
    public BSTNode<T> right;
}


public class BinarySearchTree<T> {
    private BSTNode<T> root;

    public <T> BSTNode<T> insert(T item){
        BSTNode<T> newNode = new BSTNode<T>();
        newNode.data = item;

        if(root == null){
            root = newNode;
        }

        return newNode;
    }
}

The insert method is not complete. But, I am getting the following compilation error on 'root = newNode;' line in the if block:

Type mismatch: cannot convert from BSTNode<T> to BSTNode<T>

I am unable to wrap my head around this. They are the same generic type. Why would the compiler complain?

I am using JDK 8 with Eclipse Mars.

user2357112 supports Monica

Those are two type parameters with the same name. One from here:

public class BinarySearchTree<T>

and one from here:

public <T> BSTNode<T> insert
       ^^^

Get rid of the one the arrows are pointing at. You've made the method take its own T parameter distinct from the class's T.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

array of type T (Java generics)

From Dev

type mismatch: can't convert from double to int java

From Dev

type mismatch: can't convert from double to int java

From Dev

Java Stream Generics Type Mismatch

From Dev

Java generics. Type mismatch: cannot convert from object to

From Dev

Java generics : Type mismatch: cannot convert from Integer to K

From Dev

Java generics. Type mismatch: cannot convert from object to

From Dev

Scala generics; Why do I get 'Type mismatch, expected: T, actual T'

From Dev

Type mismatch with Java generics and anonymous class

From Dev

Java/Scala Bounded Generics and type inference mismatch

From Dev

Java generics bound mismatch recursive type

From Dev

Java: Generics: Getting an object to type T from a generic method and accessing its methods

From Dev

Scala type mismatch with generics

From Dev

Why isn't this a type mismatch?

From Dev

Why isn't this a type mismatch?

From Dev

Java generics: Bound mismatch: The type is not a valid substitute for the bounded parameter of the type

From Dev

Java Generics bounded type doesn't apply to method parameters

From Dev

Java Generics Clarification( Constraining T to a type, while using Comparable)

From Dev

Generics call with Type T in Swift

From Dev

Type mismatch, expected: T, actual: T

From Dev

Java generics: Bound mismatch

From Dev

Java Generics argument mismatch

From Dev

How to use Java Generics for returing T from String

From Dev

Java Comparing Generics with Comparable<? super T> from another class

From Dev

How to use Java Generics for returing T from String

From Dev

Java generics variable <T> value

From Dev

java generics T extends Simpletype?

From Dev

Scala: Type mismatch with nested generics

From Dev

Kotlin generics inheritance - Type mismatch

Related Related

  1. 1

    array of type T (Java generics)

  2. 2

    type mismatch: can't convert from double to int java

  3. 3

    type mismatch: can't convert from double to int java

  4. 4

    Java Stream Generics Type Mismatch

  5. 5

    Java generics. Type mismatch: cannot convert from object to

  6. 6

    Java generics : Type mismatch: cannot convert from Integer to K

  7. 7

    Java generics. Type mismatch: cannot convert from object to

  8. 8

    Scala generics; Why do I get 'Type mismatch, expected: T, actual T'

  9. 9

    Type mismatch with Java generics and anonymous class

  10. 10

    Java/Scala Bounded Generics and type inference mismatch

  11. 11

    Java generics bound mismatch recursive type

  12. 12

    Java: Generics: Getting an object to type T from a generic method and accessing its methods

  13. 13

    Scala type mismatch with generics

  14. 14

    Why isn't this a type mismatch?

  15. 15

    Why isn't this a type mismatch?

  16. 16

    Java generics: Bound mismatch: The type is not a valid substitute for the bounded parameter of the type

  17. 17

    Java Generics bounded type doesn't apply to method parameters

  18. 18

    Java Generics Clarification( Constraining T to a type, while using Comparable)

  19. 19

    Generics call with Type T in Swift

  20. 20

    Type mismatch, expected: T, actual: T

  21. 21

    Java generics: Bound mismatch

  22. 22

    Java Generics argument mismatch

  23. 23

    How to use Java Generics for returing T from String

  24. 24

    Java Comparing Generics with Comparable<? super T> from another class

  25. 25

    How to use Java Generics for returing T from String

  26. 26

    Java generics variable <T> value

  27. 27

    java generics T extends Simpletype?

  28. 28

    Scala: Type mismatch with nested generics

  29. 29

    Kotlin generics inheritance - Type mismatch

HotTag

Archive