Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Possible error in Schildt's book?

 
Jagan Nambi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
Jagan Nambi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Victor Flores O'connor
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does not compile,
This is what i get:

 
Emilia Ipate
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code does not compile , Gives the error message Type mismatch cannot convert from int to byte.
 
Jagan Nambi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Author is right!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic