Open In App
Related Articles

Flexible nature of java.lang.Object

Improve Article
Save Article
Like Article

We all love the mechanism of python, where we don’t have to bother about the data types of the variables. Interestingly we have one class in Java too, which is pretty similar! It’s java.lang.Object.



// Java program to Demonstrate Flexible Nature of
// java.lang.Object
// Importing required classes
import java.util.*;
// Main class
public class GFG {
    // Main driver method
    public static void main(String arr[])
        // Declaring a variable of Object class type
        Object y;
        y = 'A';
        // Getting the class name
        // using getClass() and getname() method
        y = 1;
        // Getting the class name
        y = "Hi";
        // Getting the class name
        y = 1.222;
        // Getting the class name
        y = false;
        // Getting the class name



Such behavior can be attributed to the fact that the Object class is a superclass to all other classes. Hence, a reference variable of type Object can be practically used to reference objects of any class. So, we could also assign y = new InputStreamReader( in the above code.

If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to 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.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 02 May, 2022
Like Article
Save Article
Similar Reads
Related Tutorials