This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 6 years ago.
I'm learning Java, and from what I learned, is that you need to specify the value that a function return.. If it doesn't return a value than it is void..
However in the below program, I'm able to change the values of an array from a void function. Can anybody explain this to me please?
public class ArraysInMethods {
public static void main(String args[]){
int rd[] = {2,3,4,5,6};
change(rd);
for(int y: rd){
System.out.println(y);
}
}
public static void change(int x[]){
for(int counter = 0; counter<x.length;counter++){
x[counter]+=5;
}
}
}
I'm learning Java, and from what I learned, is that you need to specify the value that a function return.
That is correct only for methods returning values, i.e. methods other than void. These methods define expressions, while void methods define statements.
Calling your change method is a statement, in the sense that it lacks return values. It does not mean, however, that it cannot change the state of your running program - for example, give different values to variables.
However in the below program, I'm able to change the values of an array from a void function.
You are not returning a value from your void function; all you do is modifying the array in place. This is allowed, because arrays are passed by reference.
Related
This question already has answers here:
Java variable may not have been initialized
(7 answers)
Why does the Java compiler not understand this variable is always initialized?
(3 answers)
Closed 2 years ago.
I wrote this code in an online java test recently. It was inside a method that was set to return an integer. I got an error message something like "variable a has no value assigned to it". I find this odd because the forloop must have access to the methods variable and the assignments inside of the loop must be registered right?
int a;
for(int i=1;i<5;i++){
a = i;}
return a;
I did assume that the method would return the integer 5.
This is only a question regarding scope of the variable a. I know that the code makes no sense.
You can try below code , that will help you. Initialisation is mandatory for any variable to return or hold any value.
class a{
public static void main(String[] args) {
System.out.println(test());
}
public static int test(){
int a = 0;
for(int i=1;i<5;i++){
a = i;}
return a;
}
}
This question already has answers here:
How do I pass a primitive data type by reference?
(8 answers)
Closed 4 years ago.
I have the following code
private static void modifyX(int x) {
if (x!=0) {
modifyX(--x);
}
}
I want the value of my variable to be updated after the recursive call, so I wrote the following code:
public static void main(String... args) {
int x = 5;
modifyX(x);
System.out.println("Modified value:\t" + x);
}
However, the value remains the same (5).
Why is my variable not updating?
You are not passing the same instance of the value 5. Instead, the JVM is creating a new int with a value of 5, and passing that to your method.
See this thread for more information.
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 6 years ago.
Given this code:
public class Test {
public void add(int x){
x++;
}
public static void main(String args[]){
Test t = new Test();
int a = 1;
t.add(a);
System.out.println(a);
}
}
I just want to print out 2 instead of 1. I think I am calling this method wrong.
Could you help me understand why?
java passes by value , that means your variable got serialized and its value was sent to the method.
To print 2 you need to make your method returns the value after increment .
Your method is declared void so it doesn't return anything. Variable a will always remain unchanged when using it for calling the method add.
It should be, for your purpose:
public int add(int x){
return x++;
}
There are a lot of errors in your code, and you should study Java properly. It shows you haven't understand neither the unary operators, that's not strictly related to Java.
However one of the possible solutions (there are many), is to change your code as follow:
public class Test {
public int add(int x){
return ++x;
}
public static void main(String args[]){
Test t = new Test();
int a = 1;
a = t.add(a);
System.out.println(a);
}
}
Again, study properly Java before trying any exercise or posting on SO.
Next time, before posting a new question please review how to ask.
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 7 years ago.
Hey there I was wondering why the "return ask" here does not change the value of 'ask' when I print it out on my main method (it prints out 0 in the main but works in the Log method) and how I can fix it. Thanks in advance!
public static int Log(int ask){
int b=0;
int c =0;
c = scannerobj.nextInt();
b = scannerobj.nextInt();
ask = b*c;
System.out.println(ask);
return ask;
}
public static void main(String [] args){
int ask=0;
Log(ask);
System.out.println(ask);
}
Because you never reset the ask variable, but rather you ignore the int that is returned by the Log method:
Log(ask); // the int returned is not assigned to anything
Instead do:
ask = Log(ask); // assign the int returned from the method back into ask.
Also understand that the ask parameter inside of the Log method is totally disconnected from the ask variable in your main method. Changing one will have no effect on the other, especially since ask is a primitive, and Java is pass by value.
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 8 years ago.
while learning C language I have learned that when you pass a variable to a function. you are not passing the variable itself you are passing a copy of it so the actual varible's value will not change unless the function returns a value and you assign that value to the variable.
but I just executed this program and this happened when I passed "Newobj" object to a "changer" function and then change the values of the variables and then print the new variables values it is working. It should not happen right? because I am sending the copy of "Newobj" to "copyobj". explain please I am confused.
Note: Explain in detail please. My brain is slow. I know the concepts of c and few concepts of c++;
here is my code:
public class PassObjects {
int regno;
String name;
double per;
PassObjects(int a,String s,double p){
regno=a;
name=s;
per=p;
}
PassObjects changer (PassObjects copyobj){
copyobj.regno=797;
copyobj.name="Srimanth";
copyobj.per=70.9;
return copyobj;
}
public static void main(String[] args){
PassObjects Newobj= new PassObjects(86,"srimanth",85.4);
System.out.println("The original values of Instance variables are "+Newobj.regno+" "+Newobj.name+" "+Newobj.per);
Newobj.changer(Newobj);
System.out.println("The changeed values of Newobj are "+Newobj.regno+" "+Newobj.name+" "+Newobj.per);
}
}
output is here:
The original values of Instance variables are 86 srimanth 85.4
The changeed values of Newobj are 797 Srimanth 70.9
What you are doing is passing a pointer to an object. You are effectively modifying the PassObjects object.
There is a difference between a reference type and a value type. For example, a function that takes an integer and modifies it will indeed receive a copy of that integer.
public void add(int x)
{
x = x + 1;
}
However, an array of ints is also a reference type.
When you call this function using the following code:
int x = 5;
add(x);
// X will still have a value of 5.
This is because x is a value-type. You call the function and pass in a copy of the integer.
However, when you create an object, you do not pass a copy of the entire object. See code below.
public void ChangeName(SomeObject x, String newname)
{
x.name = newname;
}
SomeObject x = new SomeObject("thename");
ChangeNameTo(x, "newname"); // As the comment below pointed out, you don't even have to reassign.
//x.name will have the value "newname" now.
You can read more here and here.
Java does manipulate objects by reference, and all object variables are references.
That means u only created a new reference to the same object and when u changed the values then it gets changed in actual object.