Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

If 2 numbers begin with a + (ie + 3 + 4) can you assume this means the numbers will be combined (instead of adding)

+3 votes
And then when the two numbers have a plus sign ONLY in the middle (i.e. 3 + 4) can you always assume that you are going to be adding them?  All in the case where the numbers are NOT in quotation marks.
asked Sep 1, 2016 in CSC 211 (F16) by Audrey Hogenkamp (100 points)

2 Answers

+4 votes
Hmm -- expressions shouldn't begin with a "+".  + is an operator that always goes between things.  So "M" + 3 + 4 will result in FIRST doing "M" + 3, which involves turning the "3" into a string, and doing string concatenation.  That gives you "M3", and then you still have to do "M3" + 4, to give you "M34".

On the other hand, with 3 + 4 + "M", Java will do 3+4 first, because the order of operations goes left-to-right when the operator precedence is tied, so it will simplify to 7 + "M", which then becomes "7M".

Does that help?
answered Sep 1, 2016 by Forrest Stonedahl (100 points)
+2 votes

OK, as a Math Nerd, I think it's important to realize that the symbol "+" has two meanings:

  • when it's used like this +3 it means "positive" (and we usually don't bother writing it; 3 written by itself is assumed to be positive). In this case it's essentially an adjective, modifying 3;
  • when it's used like this 3 + 4 it means "add" as a verb

Of course, in CS you can make "+" mean concatenate. 3 + 4 would produce 7 but "3" + 4 (or 3 + "4" if I understand correctly) would produce the string "34"

answered Sep 3, 2016 by Jon Clauss (100 points)
...