LearnFrenzy
Sign in Take a free test

Records, Enums and Sealed Classes · Predict the Output · Question 7

Equality and toString of records Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 Back to Exercise

What is the output of the following Java program?

Main.java
record Money(String currency, long paise) { }

public class Main {
    public static void main(String[] args) {
        Money a = new Money("INR", 5000), b = new Money("INR", 5000), c = new Money("USD", 5000);
        System.out.println(a + " " + (a == b) + " " + a.equals(b) + " " + a.equals(c) + " " + a.paise() / 100);
    }
}
  • A Money[currency=INR, paise=5000] false true false 50 Correct
  • B Money@1b6d false false false 50
  • C Money[INR, 5000] false true false 50
  • D Money[currency=INR, paise=5000] true true false 50
Correct Answer: Option A

Records compare by components, not identity, and print as Name[comp=value, …].

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links