 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
James Bond Guest
|
Posted: Sat Aug 14, 2004 11:00 pm Post subject: newbie help interpreting this code from book |
|
|
I have been reading Thinking in Java (2nd E., Bruce Eckel) and there is
one little thing I simply cannot figure out about this one code snippet
in the book. Here it is:
public class IfElse2 {
static int test(int testval, int target) {
int result = 0;
if(testval > target)
return +1;
else if(testval < target)
return -1;
else
return 0; // Match
}
public static void main(String[] args) {
System.out.println(test(10, 5));
System.out.println(test(5, 10));
System.out.println(test(5, 5));
}
}
My question is why is the variable 'result' defined and given a value in
line 3? Since the method returns a literal in every case, I am confused
as to why to author is doing this.
TIA for any help.
James
|
|
| Back to top |
|
 |
Paul Lutus Guest
|
Posted: Sat Aug 14, 2004 11:09 pm Post subject: Re: newbie help interpreting this code from book |
|
|
James Bond wrote:
| Quote: | I have been reading Thinking in Java (2nd E., Bruce Eckel) and there is
one little thing I simply cannot figure out about this one code snippet
in the book. Here it is:
public class IfElse2 {
static int test(int testval, int target) {
int result = 0;
if(testval > target)
return +1;
else if(testval < target)
return -1;
else
return 0; // Match
}
public static void main(String[] args) {
System.out.println(test(10, 5));
System.out.println(test(5, 10));
System.out.println(test(5, 5));
}
}
My question is why is the variable 'result' defined and given a value in
line 3? Since the method returns a literal in every case, I am confused
as to why to author is doing this.
|
It seems the author made a mistake. He apparently intended to use the
"result" variable, but then forgot all about it. This is how the method was
probably meant to look:
static int test(int testval, int target) {
int result = 0;
if(testval > target)
result = 1;
else if(testval < target)
result = -1;
return result;
}
Here is another variation on the basic idea, but not one suitable for a
beginning textbook:
static int test(int testval, int target) {
return (testval < target)?-1:(testval > target)?1:0;
}
The bottom line is, even textbooks aren't perfect. Maybe this was the
author's tricky way to get you to think about the code and not assume that
it is beyond reproach. And maybe it was a simple mistake.
--
Paul Lutus
http://www.arachnoid.com
|
|
| Back to top |
|
 |
thufir.hawat@mail.com Guest
|
Posted: Sat Aug 14, 2004 11:13 pm Post subject: Re: newbie help interpreting this code from book |
|
|
On Sat, 14 Aug 2004, James Bond wrote:
[..]
| Quote: | int result = 0;
[..]
My question is why is the variable 'result' defined and given a value in
line 3? Since the method returns a literal in every case, I am confused
as to why to author is doing this.
[..] |
unless result were final, which it isn't, it's just good form. in java
some primitives are automatically initialised, such as to zero, while
others might have a garbage value without initialization. I think
primitives within methods might have garbage values unless initialized.
There's still the unanswered question of why it's good form, of course ;)
Thufir Hawat
|
|
| Back to top |
|
 |
Tor Iver Wilhelmsen Guest
|
Posted: Sun Aug 15, 2004 7:42 am Post subject: Re: newbie help interpreting this code from book |
|
|
[email]thufir.hawat (AT) mail (DOT) com[/email] writes:
| Quote: | unless result were final, which it isn't, it's just good form. in java
some primitives are automatically initialised, such as to zero, while
others might have a garbage value without initialization.
|
I think the real question was that "result" is defined ad initialized,
but never *used* for anything after that.
|
|
| Back to top |
|
 |
James Bond Guest
|
Posted: Sun Aug 15, 2004 7:52 am Post subject: Re: newbie help interpreting this code from book |
|
|
Paul Lutus <nospam (AT) nosite (DOT) zzz> wrote in
news:10ht6su39f0j22 (AT) corp (DOT) supernews.com:
| Quote: | James Bond wrote:
I have been reading Thinking in Java (2nd E., Bruce Eckel) and there
is one little thing I simply cannot figure out about this one code
snippet in the book. Here it is:
public class IfElse2 {
static int test(int testval, int target) {
int result = 0;
if(testval > target)
return +1;
else if(testval < target)
return -1;
else
return 0; // Match
}
public static void main(String[] args) {
System.out.println(test(10, 5));
System.out.println(test(5, 10));
System.out.println(test(5, 5));
}
}
My question is why is the variable 'result' defined and given a value
in line 3? Since the method returns a literal in every case, I am
confused as to why to author is doing this.
It seems the author made a mistake. He apparently intended to use the
"result" variable, but then forgot all about it. This is how the
method was probably meant to look:
static int test(int testval, int target) {
int result = 0;
if(testval > target)
result = 1;
else if(testval < target)
result = -1;
return result;
}
Here is another variation on the basic idea, but not one suitable for
a beginning textbook:
static int test(int testval, int target) {
return (testval < target)?-1:(testval > target)?1:0;
}
The bottom line is, even textbooks aren't perfect. Maybe this was the
author's tricky way to get you to think about the code and not assume
that it is beyond reproach. And maybe it was a simple mistake.
|
Okay, thank you. I suspected that it was just an error, but being such a
newbie I knew there could just as easily be something else I was missing.
I appreciate your (and the other poster's) time in helping me with this.
James
|
|
| Back to top |
|
 |
thufir.hawat@mail.com Guest
|
Posted: Sun Aug 15, 2004 9:15 am Post subject: Re: newbie help interpreting this code from book |
|
|
On Sun, 15 Aug 2004, Tor Iver Wilhelmsen wrote:
| Quote: | thufir.hawat (AT) mail (DOT) com writes:
unless result were final, which it isn't, it's just good form. in java
some primitives are automatically initialised, such as to zero, while
others might have a garbage value without initialization.
I think the real question was that "result" is defined ad initialized,
but never *used* for anything after that.
|
doh!
Thufir Hawat
|
|
| Back to top |
|
 |
Chris Smith Guest
|
Posted: Wed Aug 18, 2004 2:53 pm Post subject: Re: newbie help interpreting this code from book |
|
|
[email]thufir.hawat (AT) mail (DOT) com[/email] wrote:
| Quote: | unless result were final, which it isn't, it's just good form. in java
some primitives are automatically initialised, such as to zero, while
others might have a garbage value without initialization. I think
primitives within methods might have garbage values unless initialized.
|
I hate to be critical, but this is dead wrong and very misleading. If
you were talking about C or C++, then you'd be right that uninitialized
local variables may have arbitrary values and should not be used.
However, the picture in Java is much different.
In Java, an uninitialized local variable can never be accessed. Before
you can write (and successfully compile) any expression that reads the
value of a local variable, the compiler has to be able to prove to
itself that the variable has been successfully initialized. This
changes everything; whereas it's good form in C and C++ to initialize a
variable as it's declared, it is bad form in Java -- unless of course
the algorithm itself asks for that variable to have that value at that
time. By senselessly initializing variables, you are lying to the
compiler and avoiding some of the help it could provide you by
complaining about an uninitialized variable. After all, a senseless
initialization that's added out of habit is almost as little use as an
undefined value.
Unfortunately, the instinctive initialization of locals is carried over
from C and C++ programmers, and propogated by Java programmers who
imagine themselves in a duel against the compiler and refuse to make use
of its help, which is freely offered.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
|
|
| Back to top |
|
 |
Tony Morris Guest
|
Posted: Fri Aug 27, 2004 4:40 am Post subject: Re: newbie help interpreting this code from book |
|
|
All fields will have a default value during initialisation (even if they
have an explicit assignment).
All locals (variables that aren't fields) will not have a value, and it will
be a compile-time error to use them if they don't have "definite assignment"
(to quote the title of the chapter from the JLS).
A variable will never hold a "garbage value".
--
Tony Morris
http://xdweb.net/~dibblego/
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|