#370 Pass-by-Value in Method Parameters

Companies:
Aston
Themes:
JavaJava BasicsCode Review

What will be printed?

class Exercise1 {
    public static void main(String[] args) {
        int int1 = 4;
        Integer integer = 9;
        User user = new User(18);

        inc(int1);
        inc(integer);
        inc(user);

        System.out.println(int1);
        System.out.println(integer);
        System.out.println(user.age);
    }

    private static void inc(int a) {
        a++;
    }

    private static void inc(Integer a) {
        a++;
    }

    private static void inc(User a) {
        a.age++;
    }
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in