Click here to Skip to main content
15,902,198 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Assume that a = “6” ,b = 7 ,y = 10
and z = 4.
What is printed by the following two statements, S
System.out.println (a + b + y + z);
Systemout.println(y + z + a + b);

What I have tried:

i assume answer are
621
1267
Posted
Updated 11-Dec-18 14:27pm
Comments
Patrice T 11-Dec-18 20:13pm    
There is an easy way to know the answer: just try it.

1 solution

Well, it seems you guessed wrong.
Question: why do you have to guess? Why can't you compile and see what is the actual output and try to figure out why you get what you get?

In simple words:
in case of
Java
System.out.println (a + b + y + z);  // first parameter in the equation is string, all the other parameters afterward was converted into string
Systemout.println(y + z + a + b); // first two parameters are number, from a to rest was converted to string, since a is string. 



Here is a test
Java
public class Main2 {
  public static void main(String []strings) {
      String a = "6";
      double y = 7.1;
      int b = 10, z = 4;
      TestClass t = new TestClass();
      TestClass t2 = new TestClass();
      System.out.println(t);
      System.out.println(t + a);
      System.out.println(b + t.toString()); //       System.out.println(b + t); Compiler will reject, even System.out.println(t + b); will be rejected
      System.out.println(t2.toString() + t); // System.out.println(t2 + t); // will fail
  }
}

class TestClass {
  int i;
  int j;
  
  TestClass() {
    i = 10;
    j=23;
  }

  @Override
  public String toString() {
    return String.valueOf(i) + j;
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900