Show what this program prints. Be exact and complete. Can you explain the behavior of each print statement?1234567public class Test { public static void main(String[] args) { System.out.println("39 + 3"); System.out.println(39 + 3); System.out.println("39" + 3); }}
Question
Show what this program prints. Be exact and complete. Can you explain the behavior of each print statement?1234567public class Test { public static void main(String[] args) { System.out.println("39 + 3"); System.out.println(39 + 3); System.out.println("39" + 3); }}
Solution
This program will print:
39 + 3
42
393
Here's the explanation for each print statement:
-
System.out.println("39 + 3");- This statement prints the string "39 + 3". The double quotes indicate that "39 + 3" is a string, so it's printed exactly as it appears. -
System.out.println(39 + 3);- This statement prints the result of the addition operation 39 + 3, which is 42. There are no quotes around 39 + 3, so Java interprets it as an arithmetic operation, not a string. -
System.out.println("39" + 3);- This statement prints the string "39" concatenated with the number 3, resulting in "393". In Java, using the + operator with a string and a number results in string concatenation, not arithmetic addition. The number is converted to a string and then appended to the end of the existing string.
Similar Questions
e correct answerWhat is the output of the following Java program?class Main { public static void main(String args[]) { final int i; i = 20; i = 30; System.out.println(i); }}
What will be the output of the following Java program?class mainclass { public static void main(String args[]) { boolean ct1 = true; boolean ct2 = false; if (ct1) System.out.println(ct1); else System.out.println(ct2); } }Options10truefalse
What will be the output of the following Java program? class mainclass { public static void main(String args[]) { char a = 'Z'; a++; System.out.print((int)a); } }Options9091122123
What will be the output of the below program?public class Test { public static void main(String[] args) { char c = 65; System.out.println("c = " + c); }} Note : explain clearly in the answer
What will be the output of the following program?class string_class {public static void main(String args[]){String obj = "hello";String obj1 = "world"; String obj2 = obj;obj2 = " world";System.out.println(obj + " " + obj2); }}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.