Madeline
Madeline

Reputation: 5

Concatenating issue when printing out objects with System.out.println();

I'm learning to code on Codecademy and keep coming up with a concatenating issue. For example, when I wrote:

System.out.println(lemonadeStand + cookieShop);

I got back the error:

Store.java:32: error: bad operand types for binary operator '+' System.out.println(lemonadeStand + cookieShop);

But when I wrote:

System.out.println(lemonadeStand);
System.out.println(cookieShop);

The code worked. Can someone tell me why doesn't the first one work? (And thanks)

---edit--- Thanks for all the help everyone! This was my first time posting a question here and I'm amazed at how kind and helpful the community is!

Upvotes: 0

Views: 752

Answers (3)

itwasntme
itwasntme

Reputation: 1462

Too broad...
But, a method accepting a String needs a String as a parameter.
The println is overloaded in PrintStream class so it accepts all of the primitive types, String and the Object type.

bad operand types for binary operator '+'

The point is in "binary" operator. The plus "+" is normally considered as arithmentic (a binary) operator between two numbers. And secondly, the "+" COULD BE CONSIDERED as String concatenation operator. [Side note, Java doesn't allow overloading of operators]
Binary, therefore arithmetic, logical and bitwise operators expects the defined types for them selfs.
In the case of "+" operator, it is most ofen considered as addition operator.
Expressions with numeric types are being automatically promoted to larger primitive types if needed.

So again, the call to a + b is only possible with numeric or String operands. If one of a or b is not numeric or String operand, the error will be thrown.
It's the implementations of println/print methods of PrintStream class that tries to get a default result of toString method for given parameter.
The System.out.print(...) is kind of combination of objects and calls. The System is static object representing a System in runtime environment, the out represents standard output stream for a current thread and print is a method called on that out PrintStream. There are several different streams, just for example an err which is standard error output stream.
The standard print or println method is overloaded to accept a Object type where those methods tries to call the toString method on given Object as a parameter. But it cannot evaluate the given expression with + operand, if the whole expression is not evaluated to Number or String.

Cutting to the chase, the "+" at first works as arithmetic operator, if next operator is a String it converts the rest of expression to String.
When "+" has two object or incompatibile operators, it cannot determine how the operands should be handled. How it could add list to an array or simply true + true? For the second (true + true) the logical && must be used.
In your case, when you tried to + between the different types, the println gave you an error, because the "+" is only able to concatenate Strings or sum both of the opearands which inherits from Number class.

Answer from @Moyebang is kind of good, because the expression given as parameter in printnl method evaluates to String type. The answer from @Lotsa isn't very good because the operands are evaluated from left to right, so if the operands before + are incompatible, the program would throw an error (the second part of an answer is ok).

Such expressions at first are evaluated in their compile time.
They are evaluaded on standard basics from left to right:

System.out.println(1 + 1 + ""); //this gives 2
System.out.println("" + 1 + 1); //this gives 11
System.out.println(1 + "" + new ArrayList<>()); //this gives 1[]
System.out.println(new ArrayList<>() + "" + 1); //this gives []1
System.out.println(new ArrayList<>() + 1 + ""); //and this gives an error

So, from left the mathematical equasions are being evaluated, when it's a String the others are being addeded to that String. The last shows an error because at first the ArrayList object is being added to "1", where there's no common approach on how to add the List to an object.

Upvotes: 0

Lotsa
Lotsa

Reputation: 412

Because the single variables alone cause a call to their .toString() method. When you add the operand between two non-string variables, it's not sure exactly what you mean. You will sometimes see this ...

System.out.println(lemonadeStand + cookieShop + "");

That + "" in there confirms this is all boiling down to a string (if that's any way to explain it technically to you.)

What would also work is ...

System.out.println(lemonadeStand.toString() + cookieShop);
or...
System.out.println(String.valueOf(lemonadeStand) + cookieShop);

It depends what those variables actually are. But if they aren't strings ... it's ambiguous as to what you are telling it to do ... add them together maybe?

Placing a single + "" somewhere in there is a common way to force it all to be interpreted as a string concatenation.

Hope that explains it in non technical terms.

Upvotes: 1

Moyebang
Moyebang

Reputation: 11

please before concatening two variable you must verify they are same type or try System.out.println(first + " " + second);

Upvotes: 1

Related Questions