• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

No clue what this means...

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13002
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Marshal
Posts: 74025
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
She'll be back. I'm just gonna wait here. With this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic