Yes. The type specified for E
is frequently one of your own design.
Here is a program that tests GenericNode
.
Three different class types are used for the generic type.
Notice that autoboxing happens automatically, so that
the GenericNode<Float>
object contains
a reference
a Wrapper class object of type Float
,
not a primitive of type 5.5f
.
public class GenericNodeTester { public static void main (String args[]) { GenericNode<Float> nodeA = new GenericNode<Float>( 5.5f ); System.out.println("nodeA: " + nodeA ); nodeA.setValue( 5.0f ); System.out.println("nodeA: " + nodeA ); GenericNode<String> nodeB = new GenericNode<String>( "Good Dog!" ); System.out.println("nodeB: " + nodeB ); nodeB.setValue( "The Cat in the Hat" ); System.out.println("nodeB: " + nodeB ); GenericNode<Integer> nodeC = new GenericNode<Integer>( 45); System.out.println("nodeC: " + nodeC ); nodeC.setValue( 55 ); System.out.println("nodeC: " + nodeC ); } }
To compile and run it, ensure that GenericNodeTester.java
and GenericNode.java
are in the same subdirectory:
PS C:\Code> javac GenericNodeTester.java PS C:\Code> java GenericNodeTester nodeA: 5.5 nodeA: 5.0 nodeB: Good Dog! nodeB: The Cat in the Hat nodeC: 45 nodeC: 55 PS C:\Code>
What do you expect this will print?
public class GenericNodeTester { public static void main (String args[]) { GenericNode<Integer> nodeA = new GenericNode<Integer>( 5.5 ); System.out.println("nodeA: " + nodeA ); } }