Reputation: 38899
Just trying to understand the behavior of java.lang.Object
class.
public class TypeCheck{
static void printMethod(Object obj)
{
System.out.println(obj);
}
public static void main(String[] args)
{
Object obj = new Object();
Integer intObj = new Integer(12);
String stringObj = new String("Hello");
printMethod(obj); //---> java.lang.Object@78b5f53a
printMethod(intObj); //---> 12
printMethod(stringObj); // ---> Hello
}
}
My questions are:
printMethod(intObj)
and in printMethod(Object obj)
I do an addition: System.out.println(obj+1)
it does not work. If obj
recognizes that it's an Integer, why can't I do operations on it?TypeCheck.java:5: operator + cannot be applied to java.lang.Object,int System.out.println(obj+1);
Now, if I do this:
public class TypeCheck
{
static void printMethod(Object obj)
{
System.out.println(obj.getClass().getName());
}
public static void main(String[] args)
{
int i = 666;
printMethod(i);
}
}
It returns java.lang.Integer
, but it's defined as int
(primitive type). Why does Java convert a primitive type to it's wrapper class when passed to an Object
.
Upvotes: 2
Views: 1589
Reputation: 21306
You rarely need to use Object itself. Object is a placeholder that contains default implementations of things like toString(), hashCode() and so on. You don't need to explicitly extend it to get these, though - every class inherits them automatically.
Java is a statically typed language, which means that type checking takes place at compile time to ensure all parameters are of the correct type, methods exist on the (declared) type of the objects they're being called on, and so on. The compiler doesn't know that you might pass an Integer into printMethod, and obj+1
makes no sense when obj is a string or a bare Object. You could cast it to an Integer, but of course this will break (ClassCastException) when you pass in a non-Integer.
Primitive types are not objects in Java. When the Java compiler sees code that uses a primitive type where an object is required:
int i = 5;
printMethod(i);
it compiles as if it were:
int i = 5;
printMethod(new Integer(i));
This is known as autoboxing and was introduced in Java 5 so that primitive types could be stored inside collections.
An addition (like in your second question) becomes something like:
// ... assume obj has been cast to an Integer ...
System.out.println(obj.intValue() + 1);
which of course makes no sense if obj
were actually an Object, since it wouldn't have an integer value to begin with!
Upvotes: 1
Reputation: 1679
I believe you will rarely use Object directly. I mean you will mostly never instantiate it. The purpose of Object is to be a common parent to all other objects (all classes inherit from the Object class). So Object will define methods that all objects in Java are going to have, like toString. You will sometimes use an Object pointer to hold a class of unknown identity but, most of the time, you can find a closer parent to the candidate classes which can be held by this pointer.
When you pass intObj (which is an Integer, but can pass as an Object since Integer inherits from Object), it is not recognized by the method as an Integer. If it prints a number, it's because the toString method in the class Integer was overrided to print the value it represents, instead of the name of the class and its address. See it this way too... You can't do operations on the Integer in the printMethod, because printMethod doesn't know if it is going to be a raw Object, an Integer, or anything else. If you cast the Object as an Integer, it should get unboxed and the operation will succeed.
It's called autoboxing. Boxing is putting a primitive in an object which basicly only holds the primitive. So when you pass the int to a method which is waiting for an object, the int it boxed in an Integer and then passed to the method.
Upvotes: 0
Reputation: 3094
Question 1 When you have a list/array of different objects of different classes and you need to access each object. (I know this is bad and we should use Generics, but that is beside the point) Object is the superclass of all class and you can use it to reference any other objects.
Question 2 Cast it to Integer before you can perform addition as the addition operation belongs to the Integer class and not the Object class.
When you print a object, the toString() method is called. The implementation of toString() in Object class returns the pointer and the implementation of toString() in Integer returns the value of the integer. Depending on the type of the object, the corresponding toString() method will be called although the instance is the same.
Question 3 The other concept you have to understand is AutoBoxing. Java automatically converts your primitive int to Integer.
Upvotes: 0
Reputation: 106530
int
, are not objects. Therefore it needs to be made an object (boxed) to be passed as one.EDIT in response to question edit:
(Number 2)
Inside of printMethod
, the type of obj
is not int
, it is Object
. You can't add objects! You would need to cast it back to Integer
or int
before you could perform such operations on it. What would the +
do if you passed an instance of TypeCheck
into TypeCheck.printMethod
?
Upvotes: 4