• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why parenthesis?  RSS feed

 
Travis Rademacher
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Often when reading my java book and when their code wants to display an output, the statement will look something like this:

System.out.println(x + "" + y +" ") ;

I just don't get why those parenthesis are there. If I am already asking the JVM to print the following output on the command line, why do I need to act as if it should be printing more. Would it work if the code looked something like this:

System.out.println(x + y ) ;

What is the difference between the two of these?
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a perfectly valid print statement:


but unless you add a space in the middle, x and y will be output right next to each other. Your book could also have those there for debug statements maybe? What book is it?


Hunter
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are called parentheses -> ()

These are called double quotes -> ""
 
Travis Rademacher
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Woops, yes those are quotations. Okay I think it is just for spacing it out because I haven't gotten far enough into my book for debugging yet.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A lot of books I've do stuff like this for formatting as well.


Hunter
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hunter McMillen wrote:A lot of books I've do stuff like this for formatting as well.



You need to know one thing to understand this kind of code: Java uses the plus character, or +, to assemble smaller strings into one longer one. A single println() call can only print one String; if you want to print a number of things at once, you have to add them together to make one longer string first. So here we just want to print

3 + 2 = 5

we have to put five things together: the value of x, the symbol + with spaces around it, the value of y, the symbol = with spaces around it, and the value of z. We glue those together with the plus sign, and get

x + " + " + y + " = " + z

and then pass the whole thing to the println method.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if x and y are numbers, they'll actually be added, then printed (x=1, y=2, it'd print 3) whereas if they're strings, they'd be concatenated (it'd print "12"). So the spaces etc. can, in addition to adding formatting, cause other behavior.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:...if they're strings, they'd be concatenated (it'd print "12")...

That's how I read this. In the original post, an empty String (not a space) is used. So if x and y are ints, then println(x + "" + y) will convert these to Strings and concatenate them. So I took this as a way to print "12" rather than "1 2" (or "1 + 2" or "3").
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just wanted to make it explicit :)
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose that x and y are numbers (integers, for example). Look at this difference:

Output:

25
7

See?
 
akhter wahab
Ranch Hand
Posts: 151
Java MyEclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Travis Rademacher wrote:Often when reading my java book and when their code wants to display an output, the statement will look something like this:

System.out.println(x + "" + y +" ") ;

I just don't get why those parenthesis are there. If I am already asking the JVM to print the following output on the command line, why do I need to act as if it should be printing more. Would it work if the code looked something like this:

System.out.println(x + y ) ;

What is the difference between the two of these?


in System.out.println(x + "" + y +" ") ; actually you are giving the space after the value of the "Y"
and in System.out.println(x + y ) ; you are giving the space after Y but not in the parenthesis so it will not be included
in Sout statement if you want to get print out space you have to put it in the parenthesis...
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
akhter wahab wrote: . . . in System.out.println(x + y ) ; you are giving the space after Y . . .
Not convinced. I think you are simply formatting the code with whitespace between tokens.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!