The site I referred to when learning Java (a super personal memorandum ...)
-[Let's understand] Explain how to draw a UML class diagram -[Introduction to Java] Summary of how to use the constructor (class / instance) -[Introduction to Java] What is this? I will explain the meaning and usage of Kihon! -[Java] What is super? Detailed explanation of meaning and usage -[Introduction to Java] Inheritance and call to constructor (super / this)
Tips
at a time like this
Main.java
class SampleClass {
private int member1=0;
public int incrementLocal1(int local){
return local++;
}
}
public class Main {
public static void main(String[] args){
SampleClass sc = new SampleClass();
System.out.println("Local variables: " + sc.incrementLocal1(0));
}
}
Execution result → I expected a local variable: 1, but ...
Local variables: 0
The incremented value is not returned, so review it.
Main.java
class SampleClass {
private int member1=0;
public int incrementLocal1(int local){
//return local++;
return ++local; //Write the increment operator before the variable.
}
}
public class Main {
public static void main(String[] args){
SampleClass sc = new SampleClass();
System.out.println("Local variables: " + sc.incrementLocal1(0));
}
}
Execution result
Local variables: 1
So, about the increment operator / decrement operator
image
i=i+1;
return i;
image
return i; //The value before being incremented is returned
i=i+1;
Recommended Posts