0

I am working on this very simple java problem which requires to print both the objects money and isTrue without skipping a line.

I have tried casting both objects to string but doesn't work. I know I could have two print statements but they would print on 2 differents lines. I need both to print as 9999.99false

class Main {
  public static void main(String[] args) {
    double money = 9999.99;
    boolean isTrue = false;
    System.out.println(money + isTrue);
  }
}

The output expected is 9999.99false

Thanks!

pharaphoks
  • 51
  • 9

1 Answers1

2

What is happening right now is the compiler is seeing that you are trying to add a double and a boolean together using the + operator, which it does not know how to do. One option is to use the below code:

System.out.println("" + money + isTrue);

The first String literal tells the compiler to add a String and a double, which the compiler can do successfully by implicitly converting the double to a String. The same thing happens with the boolean.

Since String is a class, and double and boolean are primitive types, casting does not work between the two like it would in C# (see here for more information on that).

This produces your expected output when run:

9999.99false
MAO3J1m0Op
  • 423
  • 3
  • 14