aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Arithmetic promotion" Watch "Arithmetic promotion" New topic
Author

Arithmetic promotion

Raj Neets
Ranch Hand

Joined: Nov 12, 2002
Posts: 46
Hai
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;
c+=b;
i tried this, but there is no compiler error.
Thanx
raj
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: http://aspose.com/file-tools
 
subject: Arithmetic promotion
 
Similar Threads
arithmetic promotion and casting
Litteral Conversion Auto Cast
Another conversion question
RouhdUp Game
Increment and Decrement Operators