This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Need help with some Java theory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help with some Java theory" Watch "Need help with some Java theory" New topic
Author

Need help with some Java theory

Vlada Den
Ranch Hand

Joined: Dec 19, 2004
Posts: 32
Hi!

Here is a piece of code:



I have two questions:

1. Are "intResult" and "doubleResult" local variables?
2. Why the return type of the getAddResult() method is String, if I operate with int and double variables?

Thank you,

Vlada
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
1. yes.
2. because the thing you're returning (everything after the "return" statement) is a String.
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884

1. Are "intResult" and "doubleResult" local variables?


Yes, they're. However, the variables int1, int2, double1 & double2 are not. These should be instance variables, if not, you're going to get a compile-time error.


2. Why the return type of the getAddResult() method is String, if I operate with int and double variables?


Firstly, it was you to declared that the return type of this method to be of String type. And at the return statement, you concatenate String object with the integer & double results which in turn produced a new String object.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Jeff Jetton
Ranch Hand

Joined: Mar 29, 2005
Posts: 71
Originally posted by Chengwei Lee:

Firstly, it was you to declared that the return type of this method to be of String type. And at the return statement, you concatenate String object with the integer & double results which in turn produced a new String object.


Yup. Another way to look at it is to rewrite your code like this:



Now ask yourself: What would you have to put in place of "???" to make this work? In other words, what type should the variable "foo" be?

A String, of course. That's really what you're returning, even though your example doesn't use a variable as an intermediate step like I'm doing.

- Jeff
Vlada Den
Ranch Hand

Joined: Dec 19, 2004
Posts: 32
Thank you very much, guys!

Vlada
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Need help with some Java theory
 
Similar Threads
converting String[][] to int[][]
Method Overloading Examples
How do I input a number and print it?
Passing method parameters dynamically
cannot make static reference to non-static Integer