• Post Reply Bookmark Topic Watch Topic
  • New Topic

No clue what this means...  RSS feed

 
Matthew Klozeov
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I'm reading Head First Java Second Edition and I'm having a bit of trouble with an exercise.
[Added code tags - see UseCodeTags for details]
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matthew. Welcome to the Ranch!

"" just means an empty String. If you have
then you're adding two integers. So if x = 2 and y = 3, for instance, it will print 5. But if you want it to append them and print 23, then you need to treat them like Strings. One way to do that is to add x to the empty String "" - whenever you add anything to a String it gets converted to a String first.

At the end of the line you've got " " - that's just a space. So the line will print the value of x, the value of y, and then a space.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's one way of performing String concatenation.
Tutorial wrote:
Strings are more commonly concatenated with the + operator, as in

"Hello," + " world" + "!"

which results in

"Hello, world!"

The + operator is widely used in print statements. For example:

String string1 = "saw I was ";
System.out.println("Dot " + string1 + "Tod");


which prints

Dot saw I was Tod

Such a concatenation can be a mixture of any objects. For each object that is not a String, its toString() method is called to convert it to a String.

 
Matthew Klozeov
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answers, I understand it now. I didn't realise the strings were there only to seperate the variables so that they didn't add x + y. At least I think thats what they are there for.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Klozeov wrote:Thanks for the answers, I understand it now. I didn't realise the strings were there only to seperate the variables so that they didn't add x + y. At least I think thats what they are there for.


... which if you think about it a bit, is kinda a silly technique. Basically, the application has to perform an extra concatenation at runtime, because the code is written so that the compiler can pick the right operation for the plus operator at compile time.

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Klozeov wrote:Thanks for the answers, I understand it now. I didn't realise the strings were there only to seperate the variables so that they didn't add x + y. At least I think thats what they are there for.

When the compiler sees a + character, is says "hmmm...what is being added together? I need to look at both things to decide what to do...".

If you had x + y, it sees an integer value and an integer value. so it says "ok...that is mathematical addition".

When it sees: x + "" + y, first it looks at the x and the "". it says "ok, i have an integer and a string literal" (note: it doesn't care what is IN the string literal, just that it is one). It goes on: "ok...so how do i 'add' an integer value and a String? I'll convert the integer value into a String, and concatenate them".

then the x + "" gets converted to a String, and we effectively have another String plus an integer value, and we go through the same logic.

if you had something like

x + y + "" + z

it would first do arithmatic addition of x and y, then convert THAT to a string, and concat it to the z value...
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an example in the Java Language Specification; look for string catenation or similar. Also remember that in Java executuion always goes left‑to‑right.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!