Flexible nature of java.lang.Object

We all love the mechanism of python, where we don’t have to bother about data types of the variables (don’t we!)

Interestingly we have one class in Java too, which is pretty similar !

Yes, you guessed it right! It’s java.lang.Object

For example,

// A Java program to demonstrate flexible nature of
// java.lang.Object
public class GFG
    public static void main(String arr[])
        Object y;

        y = 'A';

        y = 1;

        y = "Hi";

        y = 1.222;

        y = false;



Such a behaviour can be attributed to the fact that java.lang.Object is super class to all other classes. Hence, a reference variable of type Object can be practically used to refer objects of any class. So, we could also assign y = new InputStreamReader(System.in) in the above code!

This article is contributed by Ashutosh Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

My Personal Notes arrow_drop_up

Practice Tags :
Article Tags :
Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:

1.8 Average Difficulty : 1.8/5.0
Based on 25 vote(s)

User Actions