• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Return type/PLS HELP getting r eady for test

 
Aleksandra Harper
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I beleive correct cast would be:
return new Integer(a+b+c+d).toString();
Am I right?
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or just do this:
return (""+ a + b+ c + d);
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?"
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry Rob, a typo!
this will work:
return (""+ (a + b+ c + d));
checked it this time
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corey, yours works also and much cleaner code too
nicely done!
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1820
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic