0

Q. What is the output after executing the following code snippet?

public static void main(String[] args) {                                                                              
    double a = 6;                                                                                     
    int b = 4;
    System.out.println("The passcode is " + a + b);
}

I am new to this programming language JAVA, thus from my understanding, I thought the answer should be "

The passcode is 10"

however the answer is

"The passcode is 6.04"

Can anyone help me resolve my doubt?

Naman
  • 27,789
  • 26
  • 218
  • 353
Kelly Tan
  • 93
  • 1
  • 5
  • 1
    the answer of that question can be tested by yourself ... no need a community to get an answer like that... – ΦXocę 웃 Пepeúpa ツ Sep 28 '17 at 10:08
  • 1
    You are concatenating `a` and `b` to the string "The passcode is ", you are not adding `a` and `b`. Just play around a little bit and you'll see. Cheers. – lrnzcig Sep 28 '17 at 10:08
  • try System.out.println("The passcode is " + (a + b)); – Laazo Sep 28 '17 at 10:09
  • You can [test it and see it live **here**](https://ideone.com/JFCRJX), it's a String concatenation. – cнŝdk Sep 28 '17 at 10:14
  • Read this: https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.7 then that: https://stackoverflow.com/questions/47605/string-concatenation-concat-vs-operator – ParkerHalo Sep 28 '17 at 10:14
  • 2
    Good question. In this respect Java is seriously broken. (The string on the left hand side puts `+` into some sort of weird concatenation mode and the `toString()` method is essentially called for all other arguments, with yet another kludge for primitive types which themselves are kludges anyway) It's a pity you've stumbled upon one of its many shortcomings so soon. – Bathsheba Sep 28 '17 at 10:26

3 Answers3

1

In fact this is a String concatenation, so you will get the following result:

The passcode is 6.04

Because the statement is evaluated as : "The passcode is "+"6.0"+"4".

Because here both the double and int value are interpreted as strings in the concatenation, as the expression is evaluated from left to right.

What you need to write in order to avoid this is:

System.out.println("The passcode is " + (int)(a + b));

Where (a+b) will be a double, then we cast it to int with the following (int).

You can check this live demo here to see the difference between both of them.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
  • the need to write statement still result into expected output... [`10` vs `10.0`](https://stackoverflow.com/a/46466533/1746118) – Naman Sep 28 '17 at 10:19
  • @nullpointer well pointed out, I updated it, I just wanted to point out the situation here. – cнŝdk Sep 28 '17 at 10:22
0

You can use :

System.out.println("The passcode is " + (a + b)); //10.0
//concatenates the double value to string after manipulation

or

System.out.println("The passcode is " + (int)(a + b)); //10
//concatenates the int value to string after manipulation

instead of

System.out.println("The passcode is " + a + b); 
// concatenates all the arguments to string moving left to right to start with " " +
Naman
  • 27,789
  • 26
  • 218
  • 353
  • Haven't downvoted but I think your answer won't help the OP. Since his question is so elementary basic, I think it he'd learn more if you'd give him some information or maybe links so he can find it out himself. – ParkerHalo Sep 28 '17 at 10:17
  • your answer is OK but I do not understand how some people just want to vote down any one. – Mohammed Ahmed Sep 28 '17 at 10:51
-1

Variable a is a double, so 6 becomes 6.0.

The + operator with a String doesn't operate an addition but a concatenation. Java converts automatically your double and int scalar types to String's.

Your result is like:

"The passcode is " + "6.0" + "4"

Which outputs:

The passcode is 6.04