Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: T and <T>

  1. #1
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default T and <T>

    I am learning generics in my Data Structures class and during the reading I fully understand it but when it comes for me to write my own code and imlement it via a project or assignment, it takes me forever because I have to go back through the book and find spots where it applies to what I'm doing.

    Anyways, I have a code piece:

    public class TelephoneBinaryTree<T extends Integer> implements BinaryTreeADT<T> 
    {
    	TelephoneTreeNode<T> root;
     
    	int size;
     
    	public TelephoneBinaryTree()
    	{
    		size = 0;
    	}
     
     
    	public TelephoneBinaryTree(int a, int b, int c)
    	{
    		root = new TelephoneTreeNode<>(a, b, c);
    		size = 1;
    	}
     
    	public  T getRoot() 
    	{
    		return root; //Type mismatch: cannot convert from TelephoneTreeNode<T> to T
    	}

    Why does it give me a mismatch? I cannot find anywhere that explains this.

  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: T and <T>

    TelephoneTreeNode<T> isn't a T. You have 2 choices: change the return result to be a TelephoneTreeNode<T>.

  3. #3
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: T and <T>

    I don't get it, why isn't it a T?

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: T and <T>

    Quote Originally Posted by that_guy View Post
    I don't get it, why isn't it a T?
    Because it's not, pure and simple. When you create an ArrayList<String>, the object created isn't a String, is it? You seem to be implying that it should be.