File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes diffrence between a++ and a=a+1 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 "diffrence between a++ and a=a+1" Watch "diffrence between a++ and a=a+1" New topic
Author

diffrence between a++ and a=a+1

mridul makkar
Greenhorn

Joined: Feb 15, 2005
Posts: 14
is there any diffrence between
a=a+1 and a++. i just need to know how a++ works.Just now i gone through a code
byte a=10;
a++;
a=a+1
second line wont give error whereas third line gives whats the reason.
Kristof Janssens
Greenhorn

Joined: Jan 05, 2005
Posts: 23
There are some rules:

For unary operators, there are 2 rules:

* if the operand is a byte, a short or a char, it is converted to an int (unless the operator is ++ or --, in which case no conversion happens)
* else there is no conversion

For binary operators, there are 4 rules:

* if one of the operands is a double, the other operands are converted to a double
* if one of the operands is a float, the other operands are converted to a float
* if one of the operands is a long, the other operands are converted to a long
* else both operands are converted to ints

so, with the unary operator ++ or --, there is no conversion. For the + operator, it is always converted to ints (unless one of the operands is a double, float, long)

so, if you want to make your code work, you need to cast the + operation to byte

a = (byte) a+1;


SCJP 1.4 (90%)<br />SCJD (in progress)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: diffrence between a++ and a=a+1
 
Similar Threads
Diffrence from JDK , JDBC ,JSDK , JVM , JSDKEE
Please anwer to my question?
Exception
XSL and XSLT
regarding Tansaction Exceptions