Why does Java print 77 for the expression 3 + 4 + \"7\"?

Understand why Java prints 77 for the expression 3 + 4 + \"7\" by stepping through numeric addition first, then string concatenation. When a string shows up, Java converts the left number to text and appends it, yielding 77. This tiny rule clarifies mixed-type operations.

Multiple Choice

What is the output of the following Java code: `System.out.println(3 + 4 + "7");`?

Explanation:
In the given Java code `System.out.println(3 + 4 + "7");`, the operation involves both numeric addition and string concatenation. First, the numeric values `3` and `4` are added together. In Java, when you use the `+` operator with two integers, it performs arithmetic addition. Therefore, `3 + 4` equals `7`. Next, the result of this addition, which is `7`, is subsequently concatenated with the string `"7"`. In Java, when you use the `+` operator and one of the operands is a string, it triggers string concatenation. Thus, `7 + "7"` results in the string `"77"`. This concatenation is performed as it converts the number `7` to its string equivalent and appends it to `"7"`. As a result, the final output of the code is `"77"`, which is printed to the console. This is why the correct answer to the code's output is option C, which is `77`.

Java’s plus operator is a chameleon. It can do arithmetic, but it can also stitch together text. That double life is what makes a tiny line like System.out.println(3 + 4 + "7"); so interesting to unpack. If you’ve ever wrestled with sneaky outputs, you’re not alone. This little snippet is a friendly reminder that types and evaluation order matter in real-world coding—and yes, it shows up in the kinds of challenges you’ll encounter in Revature’s coursework and interviews.

What exactly happens in this line

Let me explain what the code does, step by step. The snippet has three pieces: two numbers (3 and 4) and a string ("7"). The + operator is used with all of them. In Java, when you mix numbers with strings using +, the rules switch from arithmetic to concatenation as soon as a string enters the mix. That’s the moment the behavior shifts.

Step 1: 3 + 4

The first operation is between two integers. Java performs ordinary numeric addition here, so 3 + 4 equals 7. No strings involved yet, so no surprises on this step.

Step 2: 7 + "7"

Now we’ve got an int (7) and a String ("7") side by side. When one operand is a string, Java converts the other operand to a string as well and concatenates them. So 7 becomes "7", and the two strings join to form "77".

Step 3: System.out.println(...)

That final result is a string, "77", which System.out.println prints followed by a newline. So the console shows 77, exactly as the correct answer notes.

Why isn’t the result 34 or 7?

  • If you saw something like 3 + 4 and then a string, you might wonder whether it would print 34. That would happen only if the first two operations were treated as strings from the start (for example, "3" + "4" + "7" would yield "347"). But here the numbers come first, so we get 7, then the string concatenation makes it "77".

  • The option 7 would imply that the code stops after the numeric addition, which it doesn’t—the moment a string shows up, concatenation takes over.

  • 34 would require the code to concatenate the digits as strings before the addition, which isn’t what Java does in this order.

A closer look at the rules of the game

  • Left-to-right evaluation: In Java, the + operator is left-associative for both numeric addition and string concatenation. That means the expression is effectively evaluated from left to right unless parentheses tell Java to do something else first.

  • The moment a string appears: If any operand is a string, the operation switches to string concatenation for that pair and then continues in a concatenation-friendly way for the rest of the expression. This is why 3 + 4 + "7" becomes 7 + "7" -> "77".

  • Type promotion: Java isn’t “adding” a number to a string in the arithmetic sense; it’s turning the non-string into a string and joining them. That subtle distinction trips a lot of folks when they’re just starting out.

A few quick experiments you can try (in your IDE or a quick sandbox)

  • 3 + 4 + "7" → "77" (as we explained)

  • "3" + 4 + "7" → "347" (string first, then 4 becomes "4" and joins)

  • 3 + "4" + 7 → "347" (the 3 turns into a string when it meets "4")

  • (3 + 4) + "7" → "77" (parentheses don’t change the result here, but they do clarify intent)

  • 3 + 4 + 5 → 12 (all numbers, no strings involved)

How this matters in real-world Java code

These kinds of type combos aren’t rare. In real projects, you’ll run into situations where you’re building strings from pieces that come from numbers, booleans, or other objects. If you’re not careful, you can end up with outputs you didn’t expect, or you might misinterpret logs that mix data kinds.

Tips to keep your code predictable

  • Use parentheses to control order: If you want to ensure numeric addition before any concatenation, write (3 + 4) + "7" or 3 + 4 + ("7"). Either way, you’ll get a predictable path to a string result.

  • Be explicit with string building: For complex strings, consider using String.format or a StringBuilder. They’re more readable and less error-prone when you’re juggling multiple data types.

  • Print intermediate steps during debugging: If you’re unsure where the switch to concatenation happens, print out intermediate values. It’s a quick way to confirm what’s going on before you ship the code.

  • Remember the environment: Some languages treat plus differently. Java’s behavior here is particular: the presence of a string triggers concatenation. Other languages may have different rules, so assume the language-specific behavior unless you’ve verified it.

Relatable digressions that still connect back

If you’ve ever cooked with a recipe that includes both dry and wet ingredients, you know the order matters. Add flour before liquid, and you’ll get a different batter than if you toss everything into the bowl at once. Java’s addition behaves like that kitchen rule. Numbers are dry ingredients, strings are the wet ones—plus has to know what you’re making before it behaves correctly. In that sense, coding is a bit of culinary math: a tiny change in order can flavor the final outcome.

What these ideas look like in a broader tech journey

For students exploring Java and related technologies, these foundational ideas are stepping stones. They show up again and again, in APIs that return numbers and strings, in data formatting routines, and in logging statements where you need readable output. You’ll also encounter version differences, IDE capabilities, and build tools that help you catch such issues earlier. For many people entering tech roles, the early “aha” moment about type interactions becomes the spark that makes more complex topics click—things like generics, streams, or how frameworks optimize string handling behind the scenes.

Revature’s ecosystem and the bigger picture

If you’re aligning with Revature’s program track, you’re likely to see these patterns echoed across coursework and team projects. Java basics aren’t just a quiz; they’re the language you’ll use to describe systems, debug issues, and collaborate with teammates. Understanding how the plus operator behaves—especially when numbers meet text—helps you read logs, reason about data flows, and write code that’s easier to maintain.

A practical takeaway for your day-to-day coding

  • When you’re assembling strings from mixed data, assume the worst-case scenario: a string can appear in the middle of your expression. Then decide whether you want numeric results first or a fully formed string from the outset.

  • If you’re ever unsure, simplify. Break the expression into smaller parts, print each piece, and verify the flow step by step.

  • When you’re documenting code, a quick comment about the intent can save hours of debugging later. Something like: “Note: this expression relies on left-to-right evaluation; the first two numbers are added before concatenation with the string.”

Closing thoughts

That tiny line of Java code is more than a trivia answer. It’s a window into how the language handles types, how the compiler and runtime cooperate, and how easy it is to make assumptions that don’t hold up under closer inspection. It’s exactly the kind of insight that makes you more confident when you’re working on real projects—where decisions aren’t just about getting a result, but about making that result clear to teammates, future you, and anyone who reads the code later.

If you’re exploring Java concepts in a structured setting, you’ll keep running into these moments. The good news is that once you’ve seen one of these type-and-operator quirks, you’re better prepared for the next one. And when you connect the dots, you’re not just solving a line of code—you’re building a mindset for clean, reliable software. The more you see, the more natural it becomes to anticipate edge cases, craft resilient logic, and communicate your reasoning with confidence.

So next time you stumble on a line like System.out.println(3 + 4 + "7"); you’ll smile a little, because you’ll recognize the pattern: numbers first, then strings, and an output that’s a simple, sturdy "77"—and you’ll know exactly why.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy