Results 1 to 3 of 3
Thread: Generics
- 01-10-2008, 09:37 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 50
- Rep Power
- 0
Generics
Hi,
I am placing some code here...
In my code i am passing a primitive type 10.34 instead of a object.Java Code:public class GenericBox1 { public <U> void inspect(U u) { System.out.println(u.getClass().getName()); } public static void main(String[] args) { GenericBox1 ob=new GenericBox1(); System.out.println("Passing Integer object"); ob.inspect(new Integer(10)); System.out.println("Passing Double object"); ob.inspect(10.34); //..............see this line System.out.println("Passing String object"); ob.inspect("affaffaf"); Integer intobject=new Integer(24); System.out.println("Passing Integer object"); ob.inspect(intobject); } }
But i read that we can't pass primitive types as a formal type parameter in generics use.
But here i did it.
but it didn't show any error.it works well.
what is the reason for this ?
is this the example for auto boxing and unboxing ?
- 01-10-2008, 11:01 PM #2
Yes this is an example of Autoboxing and unboxing ..
can't pass primitive types as a formal type parameter in generics useJava Code:public class Box<E> { private E e; public E getE() { return e; } public void setE(E e) { this.e = e; } public static void main(String[] args) throws Exception { Box<Integer> ok = new Box<Integer>(); Box<int> no-ok = new Box<int>(); // This doesnot work } }dont worry newbie, we got you covered.
- 01-10-2008, 11:08 PM #3
Member
- Join Date
- Nov 2007
- Posts
- 50
- Rep Power
- 0
Similar Threads
-
Help w/ generics
By Hollywood in forum New To JavaReplies: 2Last Post: 02-16-2008, 03:08 AM -
Generics
By eva in forum New To JavaReplies: 2Last Post: 01-04-2008, 09:10 PM -
Java confused over Generics?
By Bibendum in forum New To JavaReplies: 3Last Post: 12-26-2007, 06:23 AM -
Java Generics (an introduction)
By Java Tutorial in forum Java TutorialReplies: 0Last Post: 11-27-2007, 06:50 PM -
ArrayList<type> - Generics
By Java Tip in forum Java TipReplies: 0Last Post: 11-14-2007, 03:21 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks