Java Reference
Java references are the ways that variables refer to objects in Java. The term “reference” is often used to describe the way that variables of non-primitive types (i.e., object types) relate to the actual objects that they represent. A Java reference can be thought of as a way to access a particular object without necessarily knowing where that object resides in memory.
Here are a few key points to keep in mind:
- Object Reference: In Java, all objects are dynamically allocated, which means they’re stored in a part of memory known as the heap. When you create an object, Java automatically assigns memory space for it in the heap. A reference to that object is returned, which you can assign to a variable. This variable doesn’t contain the object itself; rather, it contains a pointer or a reference to the object.
// object creation
String str = new String("Hello, World!");
In this case, str
is a reference to a new String
object.
- Null Reference: A null reference is one that doesn’t point to any object. In Java,
null
is a special value that indicates that a reference doesn’t point anywhere. Attempting to call methods on a null reference will result in aNullPointerException
.
// null reference
String str = null;
- Reference Assignment: When you assign one reference variable to another, both will point to the same object. Changes made through one reference will be visible through the other.
String str1 = new String("Hello, World!");
String str2 = str1; // str2 now points to the same object as str1
- Reference Types and Primitive Types: In Java, there are eight primitive types:
boolean
,byte
,char
,short
,int
,long
,float
,double
. Every other type is a reference type, which includes classes, interfaces, arrays, and enums.
Demo Day 1 Video:
Conclusion:
Unogeeks is the No.1 Training Institute for Java Training. Anyone Disagree? Please drop in a comment
You can check out our other latest blogs on Java Training here – Java Blogs
You can check out our Best in Class Java Training details here – Java Training
Follow & Connect with us:
———————————-
For Training inquiries:
Call/Whatsapp: +91 73960 33555
Mail us at: info@unogeeks.com
Our Website ➜ https://unogeeks.com
Follow us:
Instagram: https://www.instagram.com/unogeeks
Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute
Twitter: https://twitter.com/unogeeks