• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

JAVA BlueJ question

jellyrole

Senior member
Can someone please explain how this code outputs 2,9,12,3?

Code:
import java.util.*;
public class Exam4 {
    public static void main(String[] args) {
    int a = 3;
    int b = Exam4.method2(a);
    System.out.println(b);
    Exam4.method1(a, b);
    System.out.println(a);
}
    public static void method1(int x, int y) {
    System.out.println(x+y);
}
    public static int method2(int x) {
    System.out.println(x-1);
    return x*x;
}
}
 
It seems a little obvious considering it's pretty sequential. What are you lost on?

int b = Exam4.method2(a); //prints out 2 because of method2 while setting b to 9
System.out.println(b); //prints out 9 because b is 9
Exam4.method1(a, b); // prints out 12 because method2
System.out.println(a); // prints out 3 because a is 3
 
Back
Top