What Java developer Should Know about Object and java.lang.Object

Java is an object oriented programming language and core of Java API is java.lang.Object class. In order to work properly in Java platform its important to learn fundamentals of Object in Java e.g. What is an Object in Java and How to use Object in Java. There are two meanings of Object in Java one which is used to refer object of Object oriented programming language or OOPS and other is java.lang.Object class. Every class in Java which explicitly doesn’t extend any class, implicitly extends java.lang.Object class. Crucial methods like finalize and wait and notify are declared in Object class, which is source of one of the java questions Why wait and notify are declared in Object class and not on java.lang.Thread class.

In this Java tutorial we will some important properties of Object in Java and OOPS which is worth learning for all Java programmers. By the way, if you want to learn about OOPS and SOLID design principles, you can also check 10 Object-oriented design principles for Java programmer.


10 points on Object in Java and OOPS

Here are some of the fundamental points to know about Object in Java and OOPS :

1) Objects are instance of Class. they represent Class at particular instance. for example "abc" is an instance of String
and "code" is another instance of String class in Java.



2) There is also class called java.lang.Object in Java which is super class of every single class in Java, don't confuse
Object term with Object Class in Java. Similarly there is a real class called java.lang.Class , don't confuse that with term class of Object oriented programming concepts or OOPS. See What is use of java.lang.Class in Java for more details.

3) Object also known as instance of Class is created using new() operator in Java. new() will call no argument constructor of  class in Java. There are some more ways to create Object in Java e.g. using reflection like Class.newInstance()

4) An object in Java are created in heap memory and they will be reclaimed by Garbage collector once there is no more live
reachable reference of them. when GC collects objects it reclaims memory allocated to it.

5) Since Java is multi-threaded language it has inbuilt synchronization mechanism. Synchronization in Java depends upon lock and every Object has one lock in Java. locks are obtained when thread enter synchronized block and returned back when thread
left synchronized block.

6) You can compare two objects in Java by using either "==" equality operator or by using equals() method in Java. "==" compares memory location of two object and returns true only if two object are same and just referenced by different reference variable, while equals() method compares two object for logical equality e.g. two String object are considered equal if both of them contains same content or characters in same order.

7) Don't confuse reference variable to Object or instance. A reference variable is used to point instance or Object located in the heap. they are not objects and they just contain handle of Object for performing operations on object e.g. calling a method or accessing any field of the class.

8) Every object in Java has finalize() method which comes from parent java.lang.Object class and gets called by Garbage collector before Object is garbage collected but this is not guaranteed by JLS.

9) Two objects can be compared using Comparable or Comparator in Java. Comparable is used to compare object in there natural order e.g. the lexicographical order for String and Comparator is used to create custom ordering for Objects.

10) Except primitives like int, short, double or float most of the types e.g. String is Object in Java including Error and Exception and that's the reason Java is some time known as close to full Object Oriented language.

Object in Java and OOPS and important points example

These are 10 points about Objects in Java and Object-oriented programming language. Sometimes it gets confusing between object term and java.lang.Object class, they are closely related because every object in Java is an instance of java.lang.Object and inherit common property like lock or monitor from Object.


Other Java programming tutorials from Java67 Blog

Thanks for reading this article so far. If you like an object-oriented programming tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

P. S. - If you are serious about learning object-oriented programming and looking for a free online course to start with then you can also check this FREE Object Oriented Programming (OOPs) for JAVA Interviews course on Udemy. It's completely free and you just need a free Udemy account to join this course. 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.