This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Methods with multiple parameters  RSS feed

 
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good morning folks.

This sounds like a very basic question, but it's puzzling me.

Why is starting a print statement argument with "" + critical for the proper output?

As an example, the below code returns a numerical value for horizChar:



The return was:

x!x!x
630

x!x!x
630

x!x!x
630

Yet if the code is modified slightly, it returns something completely different.



The return was:

x!x!x
~~~~~
x!x!x
~~~~~
x!x!x


Is this just something that is the nature of the beast with Java?

Thanks
Bill
 
Sheriff
Posts: 13392
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char is an integer value, meaning Java will normally treat it as a number when it comes to an operation like "+". So, when you use + with horizChar, you are doing arithmetic addition of the char's integer value, which apparently is 126.

You can get the output you want this way:

The reason adding "" to the start of the expression works is because when the Java compiler sees a String as the first term, it then knows to use concatenation instead of integer addition. All the other "+" operations become concatenation after that.

Hence, something like this:

Will give you an output of "252~~~" because the first + will be an integer addition while the remaining + operations will be string concatenations.

Note that this:

will also give you the output you wanted, so it's not really that "" comes as the first term but it's about what Java interprets the "+" operator to be based on the types of the terms on either side of it. If both terms are numeric values such as int or char, then arithmetic addition is performed. If either term is a String, then Java will perform string concatenation.
 
Junilu Lacar
Sheriff
Posts: 13392
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, the subject you used for this thread indicates a little bit of misunderstanding on your part at worst and poor choice of words at best.

In the System.out.println() statement in question, you are only passing in one argument/parameter. You've used a multi-term expression which gets evaluated down to a single value which is then passed to the println() method.
 
Junilu Lacar
Sheriff
Posts: 13392
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you consistently used "return":

Bill Platt wrote:the below code returns a numerical value for horizChar:
...
The return was:

x!x!x
630

...

The return was:

x!x!x
~~~~~


You're going to confuse yourself if you don't separate the idea of "return" vs. "display" -- in this code, nothing is returned where you said "return".  That was all display as a result of calling System.out.println(), which is a void method, meaning it doesn't return anything. It will perform output operations, normally to the console/display device. There's a big difference and if you don't keep those separated, you're likely to confuse yourself to no end later on.
 
Bill Platt
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I think I got it now. I'm didn't realize that the "Char" was an integer value. I was thinking that it was its own deal but as a sort of "mini-String" (another poor choice I works I know but it's how I thought of it)



Junilu Lacar wrote:By the way, the subject you used for this thread indicates a little bit of misunderstanding on your part at worst and poor choice of words at best.

In the System.out.println() statement in question, you are only passing in one argument/parameter. You've used a multi-term expression which gets evaluated down to a single value which is then passed to the println() method.



My choice of words was based on the screenshot below from my text.

My thinking with call it "multiple parameters" was that when the method was defined, char horizChar & char vertChar were both required parameters.
Am I still misunderstanding this?


Thanks for the clarification on the return versus Display. I wasn't really considering that there is a difference. I can see where there is (now) though.

My thought process was that if something was displayed and/or output (somewhere) then it was being "returned"

What does it mean then, when something is being returned? Does this simply mean that the current method is complete, and it's going back to where it was/ moving along to the next one?

I apologize for so many silly questions, but it seems that my understanding is not what I thought it was.


Bill
screenshot2.jpg
[Thumbnail for screenshot2.jpg]
 
Junilu Lacar
Sheriff
Posts: 13392
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we say a method "returned" something, that just means it produces a value that can be used. A void method returns nothing, not even null, hence a call to a void method cannot be used where a value can be used.

This is an expression:

You can replace one of the terms in that expression with a call to a method that returns a value. Assuming rand is an instance of Random:

You can't do this though:

even though this by itself:

will display something.
 
Junilu Lacar
Sheriff
Posts: 13392
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bill Platt wrote:My thinking with call it "multiple parameters" was that when the method was defined, char horizChar & char vertChar were both required parameters.
Am I still misunderstanding this?


No, you're not misunderstanding it because that actually is a method that is defined with multiple parameters. However, your subject and the fact that the printTicTacToe() method is called with two parameters has absolutely nothing to do with the question/problem you had, so it's a poor choice of words. A better subject might have been "Why am I getting a number when I print out characters?" or even "Why does this work if I add "" in front?"
 
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a stickler for asthetics. I prefer this spaced output.

Output:
 
Junilu Lacar
Sheriff
Posts: 13392
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's certainly one way. I prefer not to depend too much on formatting to make the intent clear though. Unless you take precautions against it, an IDE's auto code formatting feature can easily wreak havoc with that kind of code.
 
Carey Brown
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I wish there was an annotation to turn formatting on or off for a small section of code.
 
Junilu Lacar
Sheriff
Posts: 13392
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Yeah, I wish there was an annotation to turn formatting on or off for a small section of code.


There is in Eclipse and I believe it should work in IntelliJ IDEA as well.

You have to turn on a setting for this though.

How to disable autoformatting eclipse
How to disable autoformatting intellij

Since there's no guarantee other people who edit the code will have the feature turned on, you still have some risk of getting the code messed up.
 
Bill Platt
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:I'm a stickler for asthetics. I prefer this spaced output.

Output:



This was an exercise from my textbook. It was all about being able to call methods that included two parameters.
As with most things, there is more than one way to get the job done (some are better than others).

BIll
 
Bill Platt
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Bill Platt wrote:My thinking with call it "multiple parameters" was that when the method was defined, char horizChar & char vertChar were both required parameters.
Am I still misunderstanding this?


No, you're not misunderstanding it because that actually is a method that is defined with multiple parameters. However, your subject and the fact that the printTicTacToe() method is called with two parameters has absolutely nothing to do with the question/problem you had, so it's a poor choice of words. A better subject might have been "Why am I getting a number when I print out characters?" or even "Why does this work if I add "" in front?"



Noted, I will try to keep future questions along the lines of what's actually happening, as opposed to what I think it's called.

Bill
 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!