I was very confused about the reflection and wrapper, I know that reflection can reflect the object into another object type, wrapper can convert the primitive type into object. Is this correct?
Your concept of reflection is wrong. Reflection lets a program investigate its own classes at runtime. For example, you get an obect of an (at compile time) unknown class and find out which fields and methods it has.
A wrapper is simply a class that takes an object and wraps it, i.e. it adds (almost) no new functionality, but exposes a different interface than the original class. A special case are the wrappers for primitive types; since primitive types are not objects in some languages, e.g. Java, wrapper classes for those primitive classes allow for treating those primitve types like objects.
wrapper can convert the primitive type into object
Ehm, you seem to be confused. A "wrapper" is usually some code that hides an API ("wraps" it), to simplify calls etc. . You are probably thinking about autoboxing, which allows you to use primitive types like their corresponding objects and vice versa.
reflection can reflect the object into another object type
No, reflection allows you to retrieve information about available classes and their members at runtime, and to invoke their functionality, even if they're not available at compile time.
See http://java.sun.com/docs/books/tutorial/reflect/ for details. Please read this, then come back if you still are confused, and ask a new question.
A wrapper will wrap
around another object which may hide some of the complexities of using the original object / provide diffrent naming conventions etc.
forgive the C# syntax and the fairly contrived example
class SimplePerson{
ComplexPerson _person;
public void WalkForward (int steps){
for (int i = 0; i < steps; i ++){
_person.LeftFoot ();
_person.MoveFoot ();
_person.PlaceFoot ();
}
}
// More methods
}
Reflection on the other hand can be used to retrieve methods / fields / properties and metadata in general from an object.
Again forgive the C#
SimplePerson _person;
Console.WriteLine ("Class {0} has the following methods:", _person.GetType().Name);
foreach (var method in _person.GetType().GetMethods()){
Console.WriteLine ("\t {0}", method.Name);
}
which should give output something like (depending on the class obviously)
Class SimplePerson has the following methods:
Eat WalkForward RunForward
I know that reflection can reflect the object into another object type
Not really. Reflection is used to dynamically access/modify objects at runtime.
Suppose you have the class Foo
you can create an instance programatically with:
Object o = new Foo();
But you can also create an instance at runtime only with the name:
public Object createInstanceOf( String className )
throws InstantiationException,
IllegalAccessException {
return Class.forName( className ).newInstance();
}
Object o = createInstanceOf( "Foo" ) ;
That's basically what reflection is all about.
wrapper can convert the primitive type into object
In general terms a wrappers simply ... well wraps another object. In particular for Java primitives you're right, Java wrappers do wrap the primitives so they can be used as any other regular object, for instance to pass an int to an object that receive an object you'll have to use a wrapper:
public void somethingWith( Object o ) {}
....
int i = 1234;
somethingWith( new Integer( i ) );
Prior to java 1.5 you could not invoke the method somethingWith
with out having to explicitly create the wrapper instance.
Today with autoboxing the compiler does that for you and you can invoke directly:
somethingWith( 1234 ) ;
I hope this helps and doesn't confuse you more.