wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Return type/PLS HELP getting r eady for test Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Return type/PLS HELP getting r eady for test" Watch "Return type/PLS HELP getting r eady for test" New topic
Author

Return type/PLS HELP getting r eady for test

Aleksandra Harper
Greenhorn

Joined: Jan 29, 2002
Posts: 10
Hi guys...I'm getting ready to take the SCJP exam and I am freaking out. I bought the Sybex training and virtual test center and keep getting a lot of conflicting answers. For instance:
public class Calc{
int a = 3;
int b = 4;
int c = 6;
int d = 8;
public String calc() {
return a + b
+ c + d;
}
public static void main(String[] args)
{
Calc c = new Calc();
System.out.println(c.calc());
}
}
correct answer by Sybex is it will compile and print 21. When I try to compile this code on my Java Compiler I get: incompatible types found int; required java.lang.String at line "return" Who's right? PLEASE HELP!!! aharper@houston.rr.com


Alex
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Interesting! Even though the compiler is telling you what the problem is, you still think the book might be correct? If the book is right, does that mean your compiler is lying to you then?
You told the compiler that your calc() method was going to return a String object, right?
So what are you returning? Aren't you returning a numeric value? What is the type of a+b+c+d, if those variables are all type int? Did you think the String fairy would wave her magic wand and turn your int into a String all by itself? :roll:
Rob
[ January 29, 2002: Message edited by: Rob Ross ]

Rob
SCJP 1.4
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172

Did you think the String fairy would wave her magic wand and turn your int into a String all by itself?

lol so Rob its my guess he needs to cast this, I've never seen this type of cast, can you or someone demonstrate?
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Righto Paul!
You can't acutally cast a primitive to an object or vice-versa. But you can convert the int to String.
In this case I would suggest looking up the static toString() method in the Integer class.

Rob
Tarik Makota
Greenhorn

Joined: Jan 29, 2002
Posts: 23
I beleive correct cast would be:
return new Integer(a+b+c+d).toString();
Am I right?
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
or just correct the method definition to
public int calc()
Originally posted by Tarik Makota:
I beleive correct cast would be:
return new Integer(a+b+c+d).toString();
Am I right?

[ January 29, 2002: Message edited by: mark stone ]
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
or just do this:
return (""+ a + b+ c + d);
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Actually Roy, that wouldn't work. The returned value would be a String, but it would be "3468" and not 21.
Try it and see!

Rob
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
rob: why won't public int calc() this work ? just wanted to be sure if there is some pitfall here.
Originally posted by Rob Ross:
Actually Roy, that wouldn't work. The returned value would be a String, but it would be "3468" and not 21.
Try it and see!

Rob
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Roy Ben Ami:
or just do this:
return (""+ a + b+ c + d);

Even though this won't work, I believe this will:

I haven't tested it, but if my order of operation knowledge hasn't failed me, this will do all of the addition first and then turn it into a String.
Corey
So does anyone think this'll show up on Letterman under "Stupid Java Tricks?"


SCJP Tipline, etc.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by mark stone:
rob: why won't public int calc() this work ? just wanted to be sure if there is some pitfall here.

Mark, changing the method definition to public int calc() will work. Rob was pointing out that the string concatenation solution that Roy suggested wouldn't work, not that changing the method definition wouldn't work. Check out my previous response.
Corey
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
sorry Rob, a typo!
this will work:
return (""+ (a + b+ c + d));
checked it this time
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
Corey, yours works also and much cleaner code too
nicely done!
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Roy Ben Ami:
Corey, yours works also and much cleaner code too
nicely done!

Personally, I'd never cast an int to a String that way. If I had an int value and wanted to turn it into a String, I'd use Integer.toString() to get it done. This is really just a trick you can use to make the compiler do what you want it to, but I think it makes the code less readable.

Which one is more readable? Ask yourself another question: Which one is more readable to someone that doesn't know Java well? Use the first one.
Corey
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
well, i guess its a matter of taste.
to me the second is more logical than the first
(why the hell use the class integer if we dont need to, and why find a special static toString() methid which takes ints).
to me, i know that primitive types can be converted to String easily just by appending them to other String.
makes life easier for me as no methods are needed, and thats what i teach usually also.
but again, its a matter of taste
Bob Graffagnino
Ranch Hand

Joined: May 30, 2001
Posts: 81
primitive types can be converted to String easily just by appending them to other String.

EDIT:
True, but wouldn't you be creating 2 string objects ("", "21"), neither would ever be eligible for GC?
Integer.toString() only creates 1 string.
[ January 30, 2002: Message edited by: Bob Graffagnino ]
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
I'm with Roy Ben in this instance, I've this method used more w/ javascript as well.
Anyway its good to learn the Integer.toString method as well
Cheers
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1744
    
    2
Any reason folks are avoiding the overloaded String.valueOf() method for this?
return String.valueOf( a + b + c + d ) ;
It has the added advantage that if the variables ever get switched to a different numeric type, this line of code doesn't need to change.
Aleksandra Harper
Greenhorn

Joined: Jan 29, 2002
Posts: 10
Originally posted by Alex Harper:
Hi guys...I'm getting ready to take the SCJP exam and I am freaking out. I bought the Sybex training and virtual test center and keep getting a lot of conflicting answers. For instance:
public class Calc{
int a = 3;
int b = 4;
int c = 6;
int d = 8;
public String calc() {
return a + b
+ c + d;
}
public static void main(String[] args)
{
Calc c = new Calc();
System.out.println(c.calc());
}
}
correct answer by Sybex is it will compile and print 21. When I try to compile this code on my Java Compiler I get: incompatible types found int; required java.lang.String at line "return" Who's right? PLEASE HELP!!! aharper@houston.rr.com
Aleksandra Harper
Greenhorn

Joined: Jan 29, 2002
Posts: 10
Originally posted by Alex Harper:
Hi guys...I'm getting ready to take the SCJP exam and I am freaking out. I bought the Sybex training and virtual test center and keep getting a lot of conflicting answers. For instance:
public class Calc{
int a = 3;
int b = 4;
int c = 6;
int d = 8;
public String calc() {
return a + b
+ c + d;
}
public static void main(String[] args)
{
Calc c = new Calc();
System.out.println(c.calc());
}
}
correct answer by Sybex is it will compile and print 21. When I try to compile this code on my Java Compiler I get: incompatible types found int; required java.lang.String at line "return" Who's right? PLEASE HELP!!! aharper@houston.rr.com

Hi guys...thanks for all the responses.(by the way, it's a she)...and I do see that it does not make any sense, but I do not get how can somebody like Sybex have a bunch of wrong answers out there in their material? And if that's wrong, how do I know what else is wrong (unless I can compile it). Anybody out there worked with their stuff and any feedback?
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Well, it's a bug, and bugs happen! That's life.
The thing to remember though is that the actual Sun tests are much more thoroughly quality-tested, so you won't have to worry about these problems on the actual test.
In the meantime, treat all mock-exams with a grain of salt, and ask questions here at javaranch if you're unsure about something.
PLUS, it will give you that oh-so-smug feeling of satisfaction when you correctly know that the answer in the book is wrong.
Rob
Joe Johnson
Greenhorn

Joined: Jan 30, 2002
Posts: 1
Ok I heard that the questionions on Virtual test centre are ful of errors (sorry if you bought it already). It seems they might have tried to computer generate the questions, and that is how they ended up with so many errors...anyway all I would say is if you can spot all their errors it might be good practise for the test?
 
 
subject: Return type/PLS HELP getting r eady for test
 
Similar Threads
Confusion regarding return type
Help fast plz!!!!!!
Help me for these questions from John Hunt's mock exam
for loop
RMI Newbie