How to Record the Value of the Method Before We Call It Again in Java
Although Java is strictly passed by value, the precise result differs between whether a archaic type or a reference type is passed. When we pass a primitive type to a method, it is passed by value. Only when we pass an object to a method, the situation changes dramatically, because objects are passed by what is effectively call-by-reference. Java does this interesting thing that's sort of a hybrid between pass-by-value and pass-by-reference.
Basically, a parameter cannot exist changed by the function, only the office can ask the parameter to alter itself via calling some method within it.
- While creating a variable of a grade blazon, nosotros simply create a reference to an object. Thus, when nosotros laissez passer this reference to a method, the parameter that receives information technology will refer to the same object as that referred to by the argument.
- This effectively ways that objects act as if they are passed to methods by use of phone call-by-reference.
- Changes to the object inside the method do reflect the object used equally an statement.
Illustration: Permit usa suppose three objects 'ob1' , 'ob2' and 'ob3' are created:
ObjectPassDemo ob1 = new ObjectPassDemo(100, 22); ObjectPassDemo ob2 = new ObjectPassDemo(100, 22); ObjectPassDemo ob3 = new ObjectPassDemo(-ane, -1);
From the method side, a reference of type Foo with a name a is declared and information technology'due south initially assigned to null.
boolean equalTo(ObjectPassDemo o);
As we call the method equalTo, the reference 'o' volition exist assigned to the object which is passed every bit an statement, i.due east. 'o' will refer to 'ob2' every bit the post-obit statement execute.
Organization.out.println("ob1 == ob2: " + ob1.equalTo(ob2));
Now as we tin can run across, equalTo method is called on 'ob1' , and 'o' is referring to 'ob2'. Since values of 'a' and 'b' are same for both the references, so if(condition) is true, then boolean true will be return.
if(o.a == a && o.b == b)
Once again 'o' will reassign to 'ob3' as the following statement execute.
System.out.println("ob1 == ob3: " + ob1.equalTo(ob3));
- Now equally we tin can run into, the equalTo method is chosen on 'ob1' , and 'o' is referring to 'ob3'. Since values of 'a' and 'b' are non the aforementioned for both the references, and so if(condition) is simulated, then else block will execute, and false volition be returned.
In Java we can laissez passer objects to methods equally one can perceive from the below program as follows:
Case:
Java
class ObjectPassDemo {
int a, b;
ObjectPassDemo( int i, int j)
{
a = i;
b = j;
}
boolean equalTo(ObjectPassDemo o)
{
render (o.a == a && o.b == b);
}
}
public grade GFG {
public static void main(String args[])
{
ObjectPassDemo ob1 = new ObjectPassDemo( 100 , 22 );
ObjectPassDemo ob2 = new ObjectPassDemo( 100 , 22 );
ObjectPassDemo ob3 = new ObjectPassDemo(- 1 , - 1 );
Organisation.out.println( "ob1 == ob2: "
+ ob1.equalTo(ob2));
System.out.println( "ob1 == ob3: "
+ ob1.equalTo(ob3));
}
}
Output
ob1 == ob2: true ob1 == ob3: false
Defining a constructor that takes an object of its grade every bit a parameter
One of the nigh mutual uses of object parameters involves constructors. Frequently, in practice, there is a need to construct a new object and so that information technology is initially the same as some existing object. To do this, either nosotros can utilize Object.clone() method or define a constructor that takes an object of its form as a parameter.
Example
Java
class Box {
double width, pinnacle, depth;
Box(Box ob)
{
width = ob.width;
height = ob.height;
depth = ob.depth;
}
Box( double w, double h, double d)
{
width = due west;
elevation = h;
depth = d;
}
double volume() { return width * height * depth; }
}
public class GFG {
public static void main(Cord args[])
{
Box mybox = new Box( 10 , twenty , xv );
Box myclone = new Box(mybox);
double vol;
vol = mybox.book();
Arrangement.out.println( "Volume of mybox is " + vol);
vol = myclone.volume();
System.out.println( "Book of myclone is " + vol);
}
}
Output
Volume of mybox is 3000.0 Volume of myclone is 3000.0
Returning Objects
In java, a method can return any type of data, including objects. For example, in the following program, the incrByTen( ) method returns an object in which the value of an (an integer variable) is ten greater than it is in the invoking object.
Example
Java
class ObjectReturnDemo {
int a;
ObjectReturnDemo( int i) { a = i; }
ObjectReturnDemo incrByTen()
{
ObjectReturnDemo temp
= new ObjectReturnDemo(a + x );
return temp;
}
}
public form GFG {
public static void master(String args[])
{
ObjectReturnDemo ob1 = new ObjectReturnDemo( 2 );
ObjectReturnDemo ob2;
ob2 = ob1.incrByTen();
Arrangement.out.println( "ob1.a: " + ob1.a);
System.out.println( "ob2.a: " + ob2.a);
}
}
Note: When an object reference is passed to a method, the reference itself is passed by use of call-by-value. However, since the value being passed refers to an object, the copy of that value will still refer to the aforementioned object that its corresponding argument does. That's why nosotros said that java is strictly pass-by-value.
This article is contributed past Gaurav Miglani. If you similar GeeksforGeeks and would like to contribute, you tin also write an commodity using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article actualization on the GeeksforGeeks main folio and help other Geeks. Please write comments if you discover anything wrong, or you want to share more information about the topic discussed above.
Source: https://www.geeksforgeeks.org/passing-and-returning-objects-in-java/
0 Response to "How to Record the Value of the Method Before We Call It Again in Java"
Post a Comment