Beginner Return Assistance [duplicate] - java

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.

Related

Java variable scope in and outside loop [duplicate]

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;
}
}

Return value of a void function [duplicate]

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.

Why is this printing out 1 instead of 2? How can I improve? [duplicate]

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.

How to accept a public static variable as parameter in Java [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 6 years ago.
I have the following instantiated variable
public static int r1 = 10;
I have several of these variables r1 - r4, I want to write a method that will be able to take the variable r1 as a parameter of the method and increment it.
Pseudo code would look like :
r1 = r1 + 1;
My question is how can I take the variable as a parameter of one method instead of writing 4 different methods to accomplish this?
You can't, because Java does not let you pass variables of primitive types by reference.
You can put your variables into an array, and pass an array and an index:
public static int[] r = new int[4];
...
public static void modify(int[] array, int pos) {
array[pos]++;
}
...
modify(MyClass.r, 1); // instead of modify(MyClass.r1);
Alternative approach is to return the modified value to callers, and let them assign it back:
public static int modify(int orig) {
return orig+1;
}
...
MyClass.r1 = modify(MyClass.r1);

Why should my method be static? [duplicate]

This question already has answers here:
Non-static variable cannot be referenced from a static context
(15 answers)
Closed 7 years ago.
Hi I have this piece of code and i am really confused to why i have to make the lel method static.The error is this "non static method cant be referred from static content". Usually when I create methods either to construct new objects or to manipulate objects in the main method I do not get this error message.Plus, i never declared e to be static!!. can someone please explain to me why this occurring?? Thank you :)
class x {
public static void main(String[]args){
int e= 2232;
e= lel(e);
}
int lel(int k){
return k+1;
}
}
There are two solutions you could implement. The first option is to make your int lel(int k) a static method which would look like static int lel(int k)
Your other option is to declare a new object of your class x and use that for your lel method within main as MickMnemonic suggested in the comments. That code would look like:
e = new x().lel(e);
I believe the simplest thing would be to make the lel method static but it is up to you.
A deeper explanation of static methods can be found here.

Categories