aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Iimplicit cast Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Barcodes with iOS this week in the iOS forum
or Core Java for the Impatient in the Java 8 forum!

JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Iimplicit cast" Watch "Iimplicit cast" New topic

Iimplicit cast

Fox Hu
Ranch Hand

Joined: Jan 23, 2003
Posts: 49
byte b=127;
byte c=126;
byte d=b-c;
In the third line the compiler complains .
It seams that the result is casted to int implicitly when two number calculate, but why?
And are there any other cases do the implicit cast appears ?
Mellihoney Michael
Ranch Hand

Joined: Nov 27, 2002
Posts: 124
but it is compiled!

a beginner in java
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610

[Blog] [Blogroll] [My Reviews] My Linked In
Paula Jiang

Joined: Feb 15, 2003
Posts: 15
From Kathy/Bert 's book, page 148-149: "the result of an expression involving anything int-sized or smaller is always an int. In other words, add two bytes together and you'll get an int."
So the compiler will complain that it can't convert int to byte, you need to explicit case to byte.

SCJP2 1.4 (96%) SCWCD(96%)
I agree. Here's the link:
subject: Iimplicit cast