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'; System.out.println(y.getClass().getName()); y = 1; System.out.println(y.getClass().getName()); y = "Hi"; System.out.println(y.getClass().getName()); y = 1.222; System.out.println(y.getClass().getName()); y = false; System.out.println(y.getClass().getName()); } } |
Output:
java.lang.Character java.lang.Integer java.lang.String java.lang.Double java.lang.Boolean
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
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Theming Material Design Snackbars in Android with Example
- Custom ArrayAdapter with ListView in Android
- How to Execute Multiple SQL Commands on a Database Simultaneously in JDBC?
- Java Program to Get Components of a URL
- Reference Variable in Java
- Popup Menu in Android With Example
- FileOutputStream in Java
- Java Program to Determine the Unicode Code Point at Given Index in String
- Java Program to Search an Element in a Circular Linked List
- Java Program to Display Floyd’s Triangle
- Java Program to Implement the Karatsuba Multiplication Algorithm
- Java Program to Compute the Area of a Triangle Using Determinants
- Java Program to Create a Singly Linked List and Count the Number of Nodes
- Java Program to Find the Volume and Surface Area of Sphere

