Reference & Reference types
what are reference types in java?
Reference types in Java are object types that refer to objects created from classes. They are used to store and manipulate the objects created by the class. Examples of reference types include objects, arrays, interfaces, and classes.
- Object type: Any class instance or array is an object type.
- Interface type: An interface type is any interface or class that implements an interface.
- Class type: A class type is a concrete or abstract class type.
- Array type: An array type is an array of a specific type.
- Enum type: An enum type is an enumeration type.
- Annotation type: An annotation type is an annotation interface or class.
- Primitive type: Primitive types are the basic data types in Java.
- Type variable: A type variable is a type parameter that can represent any type.
- Union type: A union type is a type that can be one of several types.
- Wildcard type: A wildcard type is a type parameter that can represent any type.
Get Mahima Chandel’s stories in your inbox
Join Medium for free to get updates from this writer.
how does the reference variable work in Java?
In Java, a reference variable is a variable that stores the reference or memory address of an object. The reference variable itself does not contain the object’s data but instead contains the memory address of the object which is used to access the object’s data.
Reference variables are declared using the type of the object they refer to, preceded by an ampersand (&). For example, if you want to declare a reference variable that will refer to a String object, you would use the following
syntax: String &name;
A reference variable must be initialized with the address of an object before it can be used.
This is done by assigning the address of an object to the reference variable, as in the following example:
String name = “John”; &name = &name;
Once a reference variable has been initialized, it can be used to access and modify the object it is referring to.
For example, if you wanted to change the value of the String object referenced by the &name reference variable, you would use the following syntax:
name = “Jane”;
By using reference variables, you can avoid making copies of objects and save memory. This is because only a single copy of the object exists, and any changes







