- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between object and reference in java?
A class in a blue print/user defined datatype in java that describes the behavior/state that the object of its type support.
Example
public class Student { String name "Krishna"; int age = 20; void greet() { System.out.println("Hello how are you"); } }
An object is an instance of a class created from it using the new keyword. Once you create an object of a class, using it you can access he members of the class. In the below given code an object of the class Student is created.
public class Example { public static void main(String args[]) { Student obj = new Student(); } }
Classes, interfaces, arrays, enumerations and, annotations are the in Java are reference types in Java. Reference variables hold the objects/values of reference types in Java
difference between object and reference
When you create an object of a class as −
Student obj = new Student();
The objects are created in the heap area and, the reference obj just points out to the object of the Student class in the heap, i.e. it just holds the memory address of the object (in the heap).
And since the String is also an object, under name, a reference points out to the actual String value (“Krishna”).
In short, object is an instance of a class and reference (variable) points out to the object created in the heap area.
- Related Articles
- What is the difference between a weak reference and an unowned reference?
- What is the difference between a String object and a StringBuffer object in java?
- What is difference between a pointer and reference parameter in C++?
- What is the difference between pass by value and reference parameters in C#?
- Difference Between Pointer and Reference
- What is the difference between a String object and a String literal in Java?
- Differences between Method Reference and Constructor Reference in Java?
- Difference between Object and Class in Java
- What is the difference between `new Object()` and object literal notation in JavaScript?
- What is the difference between Object oriented programming and Object based programming?
- What is the difference between Java and Core Java?
- What is the difference between Java and Java EE
- What is the difference between /* */ and /** */ comments in Java?
- What is the difference between >> and >>> operators in Java?
- What is the difference between Java and JavaScript?
