Question:
What does in java?
Matt D
2010-04-09 10:43:24 UTC
I keep seeing it in this example Binary Tree implementation.

private TreeNode leftChild;
class BinaryTree extends BinaryTreeBasis
Three answers:
Blackcompe
2010-04-09 11:09:17 UTC
indicates the class or method is generic. I think it's called a type operator. For instance, I may have a custom class named "Animal" that can be different types, such as: Dog, Cat, Snake, whatever....



class Dog { String name = "Dog"; String getName(){ return name;}}

class Snake {String name = "Snake"; String getName(){ return name;}}



class Animal {

T type;

Animal(T type){ this.type = type; }

String getName(){ return this.type.getName(); }

}



class AnimalExample { public static void main(String... args) {

Animal a = new Animal();

System.out.println(a.getName());

a = new Animal();

System.out.println(a.getName());

}}



Hopefully, you can see the power of this little concept. In your example, generics allows the the BST is contain any Java object (assuming the comparison method doesn't fail).............BinaryTree, BinaryTree, and so on.
Nigel
2010-04-09 11:02:45 UTC
It's called Generics. When you have a class which ( in this case ) is a tree, the T acts as a place holder in the code. When you define your own class which uses the BinaryTree, you'd create it something like:

BinaryTree employees = new BinaryTree();

When your application gets compiled, the the BinaryTree code has the Employee data type replaced for each instance of T. This means that the employees item will only work with Employee objects and enforce type checking. So when you call something like an add method of the employees - you know it's expecting an Employee object.
iGrannyWrinkles
2010-04-09 10:47:33 UTC
I believe it is title :/


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...