Introduction to Java alias
In Java, Alias is used when reference, which is of more than one, is linked to the same object. The issue with aliasing is when a user writes to a particular object, and the owner for the several other references do not expect that object to change. Here, the code that includes aliasing can get a bit confusing fast, and it is very tedious to debug as well. Overall, aliasing is a process that should be avoided or used with at most care. Let us see how Java Alias works.
How does alias work in Java?
As already discussed, aliasing is used when reference, which is of more than one, is linked to the same object. It also means that there are several aliases to a location that can be modified, and these particular aliases have several types.
Let us take an example. x and y are two variable names that have two types X and Y. Y extends X.
Y[] y = new Y[10];
X[] x = y;
x[0] = new X();
Y[0].func1();
In memory as well, they both point to the same location.
The memory location which is pointed is pointed by x as well as y. However, the actual object saved chooses which method to call during runtime.

4.8 (14,296 ratings)
View Course
Let us see another example.
Rectangle b1 = new Rectangle (0, 0, 50, 150);
Rectangle b2 = b1;
Both b1 and b2 refer to the same object, or we can say that the given object has two names, such as b1 and b2. Similar to a person that has two names, objects can also have two names.
When two aliased variables are present, changes that cause one variable also reflect on the other, as shown below.
System.out.println (b2.width);
b1.grow (40, 40);
System.out.println (b2.width);
On executing the code, you will see that the changes that have caused on one rectangle have occurred in the second rectangle as well. This is one of the main things that has to be noted for Alias in Java.
Examples of Java Alias
The following are some of the sample programs on Java Alias.
Example #1
Code:
//class X
class X {
//function 1
public void func1()
{
System.out.println("called sample function 1");
}
}
//Class Y that extends the class X
class Y extends X
{
//function 1
public void func1()
{
System.out.println("called sample function 1");
}
//function 2
public void func2()
{
System.out.println("called sample function 2");
}
}
//main class
public class AliasExample {
//main method
public static void main(String[] args) {
Y[] y = new Y[10];
X[] x = y;
x[0] = new X();
y[0].func1();
}
}
Output:
On executing the code, it can be seen that an exception known as ArrayStoreException has occurred. How this occurs? What has to be changed? Is it possible to solve this?
Yes!! The only reason for this exception is that Java manages aliases during runtime. Only during the run time, it will be able to know that the first one should be an object of Y, instead of X. To solve this, the above code has to be changed.
Therefore, the sample program runs correctly only if it is altered into the following code.
//class X
class X {
//function 1
public void func1()
{
System.out.println("called sample function 1");
}
}
//Class Y that extends the class X
class Y extends X
{
//function 1
public void func1()
{
System.out.println("called sample function 1");
}
//function 2
public void func2()
{
System.out.println("called sample function 2");
}
}
//main class
public class AliasExample {
//main method
public static void main(String[] args) {
Y[] y = new Y[10];
X[] x = y;
x[0] = new Y();
y[0].func1();
}
}
Output:
The exception is thrown above in the first program that has not occurred here as the object of X changed into to object of Y.
Example #2
Code:
//main class
public class AliasExample {
//main method
public static void main(String[] args) {
//create two different arrays with same value
int a= 87;
int b=87;
//checks whether a and b are equal
System.out.println(a == b);
//assign b equal to a
b=a;
//checks whether a and b are equal
System.out.println(a == b);
}
}
Output:
In this program, two integer variables a and b are created firstly. Then the code checks whether a and b are equal. In the next step, a is assigned to b. Again a and b are checked whether they are similar. On executing the code, both the results will be printed as true.
What will happen if two arrays a and b are used instead of integer variable?
That can be explained using the program given below.
//main class
public class AliasExample {
//main method
public static void main(String[] args) {
//create two different arrays with same value
int []a = {81, 54, 83};
int []b = {81, 54, 83};
//checks whether a and b are equal
System.out.println(a == b);
//assign b equal to a
b=a;
//checks whether a and b are equal
System.out.println(a == b);
}
}
Output:
In this program, two arrays a and b are created in the first step. Then, similar to the above program, a and b are checked whether they are equal. After that, a is assigned to b, and once again, both are checked whether they are similar. On executing the code, it can be seen that the output for the first check is false, and output for the second check is true. It is because Java Alias works.
Conclusion
Alias is used in Java when the reference of more than one is linked to the same object. The drawback of aliasing is when a user writes to a specific object, and the owner for some other references does not guess that object to change. In this article, more details on Java Alias is explained in detail.
Recommended Articles
This is a guide to Java Alias. Here we discuss how does alias work in java along with programming examples for understanding better. You may also have a look at the following articles to learn more –