Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Arithmetic promotion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Arithmetic promotion" Watch "Arithmetic promotion" New topic

Arithmetic promotion

Raj Neets
Ranch Hand

Joined: Nov 12, 2002
Posts: 46
The following is a piece of code, in which i try to use x=x"op"y operator. I have a doubt , whether there is any Arithmetic promotion applied when two different type of primitive date type is involved.
double b= 8.0;
byte c=10;
i tried this, but there is no compiler error.
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
Hi there Raj,
it is sort of the same as this situation...
byte b;
b++; // no problem...
byte b = b + 1; // compiler error
so if you want to make a compiler error, rearrange the wording of your code like this...
class MathStuff {
public static void main(String args[]) {
double b= 8.0;
byte c=10;
c = c + b; // compiler error here!
so the compiler does a short cut for you, in that it casts it as a byte for you when you type the addition the way you did.

giddee up
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
There is no compiler error because this happens:

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
congrats Barry on a somewhat more succinct answer
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
This is suitable JLS 5.6 Numeric promotions

SCJP2. Please Indent your code using UBB Code
I agree. Here's the link:
subject: Arithmetic promotion
It's not a secret anymore!