Day 11 | Guess the output | Java Interview Questions | #corejava #java #springboot

preview_player
Показать описание
Day 11 | Guess the output | Java Interview Questions | #corejava #java #springboot

Output: 11511

Explanation: The string concatenation operator works as follows: if both the operands are numbers, it performs the addition; otherwise it concats the arguments by calling the toString() method if needed. It evaluates from left to right. Hence, the expression in the program results in the string 115110.

Java programming interview questions
java8
simple Java interview questions
play with Java
learn Java

java interview question #shorts #interview #interviewquestions #datatypesinjava #programming #javaprogramming #springboot
#softtechtuber
Рекомендации по теме
Комментарии
Автор

It should be 115110.
Why though?
The statements in lines 3 and 4 can be expanded as such:
int result1 = 1 + 10; // 11
String result2 = result1 + new String("5"); // „115“
String result3 = result2 + 1; // „1151“
String result4 = result3 + 10; // „115110“
System.out.println(result4);
Also just as an aside don’t use the String copy constructor unless you need a copy – which most of the time you won’t.

darkdaysss
welcome to shbcf.ru