GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Possible error in Schildt's book? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Possible error in Schildt Watch "Possible error in Schildt New topic
Author

Possible error in Schildt's book?

Jagan Nambi
Greenhorn

Joined: Jan 16, 2004
Posts: 15
Hi all,

I'm learning Java 5 using Herbert Schildt's book "The Complete Reference - Java J2SE 5 Edition".

On page # 47 (Chapter 3: Data Types, Variables and Arrays), he mentions:



I tried the same using the following code sample shown below:


I compiled it and executed it using J2SE 1.5, without any compilation or runtime errors and the program correctly prints "100". In my observation, there isn't any problem with this piece of code.

However, the author says,

However, as I said, according to my observations, this typecasting is unnecessary works fine by itself.

Further, consider the following code:


In both cases, whether you typecast or not, both b and c would print out -56, because the result is going to overflow.

So, am I missing out something? Or did the author mean something else? Please help. Thanks.

Regards,
Jags.
[ August 10, 2005: Message edited by: Jagan Nambi ]
Sherry Jacob
Ranch Hand

Joined: Jun 29, 2005
Posts: 128


Are you sure this code compiled ? It gives an error all over my place !!
The type casting is necessary.

I don't understand how it compiled at your end.


<strong><br />Cheers !!<br /> <br />Sherry<br /></strong><br />[SCJP 1.4]
Jagan Nambi
Greenhorn

Joined: Jan 16, 2004
Posts: 15
Originally posted by Sherry Jacob:

Are you sure this code compiled ? It gives an error all over my place !!
The type casting is necessary.

I don't understand how it compiled at your end.


Hi Sherry,

Yep! Compiled perfectly using both Java 1.4.1 and Java 1.5 compilers. Absolutely no compilation or execution problems. Compiled & executed with the correct results.

What errors are you getting at your end?

Regards,
Jags.
Emilia Ipate
Greenhorn

Joined: Aug 09, 2005
Posts: 7
Hi Jagan!

I've tested AGAIN the code and I MUST THAT THE FIRST TIME I WAS WRONG (thanks Liviu) and here are my results:

In the first case:

there is a compile error because when getting to the line b * 2, the compiler promotes the byte b to int (because 2 is int). and so b * 2 is an int and it cannot be converted to byte.

In the second case:

there is a compile error at line : c = c * 4 because the int (c * 4 is an int) cannot be converted to a byte number. You must do a cast, so the correct line is : c = (byte) (c * 4).

Do let me know if you find more problems.
[ August 11, 2005: Message edited by: Emilia Ipate ]
Liviu Carausu
Ranch Hand

Joined: Oct 07, 2004
Posts: 158
After my understanding, this code is not compilable

My compiler says the same.
b *2 has the type int, downcast conversions are not implicit in Java. I'm very curious how you have compiled this piece of code... Please let us know.


Oracle Certified Master Java SE6 Developer(SCJD),
OCE JEE 6 JSP and Servlets Developer.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I agree with Liviu and Sherry - the original code for ByteError (using "b = b * 2;") does not compile. JDK versions 1.3.1_15, 1.4.2_08, and 1.5.0_04 all give the exact same error message:

I'm not sure what is going on with Jagan and Emilia. I believe either you've changed the code somehow, or you're not compiling what you think you're compiling. (Or possibly you've got a specific compiler version with a very strange bug.) Please try compiling the following:

The key line "b = b * 2;" is unchanged (and yes you must have the line exactly like this); I've just added some diagnostic print statements. If the program compiles for you, please report exactly what output you get. Thank you.


"I'm not back." - Bill Harding, Twister
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
hi i have tried it ...

the code doesnt compile giving a message Error(5,13): possible loss of precision: int, required: byte

thanks & regards

srikanth


Thanks & Regards<br /> <br />-Srikanth
Victor Flores O'connor
Greenhorn

Joined: Aug 05, 2005
Posts: 2
does not compile,
This is what i get:

Emilia Ipate
Greenhorn

Joined: Aug 09, 2005
Posts: 7
Yes, you are all right, it does not compile.
I have mistaken when I first entered the reply. SO, I HAVE CORRECTED my previous answer.
AnuragSudha Jain
Greenhorn

Joined: Aug 10, 2005
Posts: 6
The code does not compile , Gives the error message Type mismatch cannot convert from int to byte.
Jagan Nambi
Greenhorn

Joined: Jan 16, 2004
Posts: 15
Hi all,

Just quickly saw the replies to this post. Sorry I haven't been able to respond. I'm away at a conference. So, I may not be able to respond back until Tuesday. Also, I had to re-image my machine. But starting Tuesday, when I get back, I will have more time to get back to this topic and SCJP study in general. Until then, you folks take care and have a wonderful weekend.

Ciao,
Jags.
Arulkumar Gopalan
Ranch Hand

Joined: Oct 13, 2003
Posts: 104
Author is right!


Anbudan & Mahalo,<br />Arul<br /> <br />-Not a sun certified Java professional :-)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
OK, everybody but Jagan has agreed this code doesn't compile. I don't think we need any more posters saying the same thing - at least not until Jagan has an opportunity to look again at what's going on.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Possible error in Schildt's book?