Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

different behaviour of operators

Thread view: 
Kavya - 27 Oct 2006 16:10 GMT
Here's the code

public class Main
{

   public static void main (String[] args)
   {
       int b=10;
       double r=0.0;
       b =b + r*10.0;//This gives error
       b+=r*10.0;// This works fine
       System.out.println (b);
   }
}

Error comes as
found: double
required:int

Why += version working?
Patricia Shanahan - 27 Oct 2006 16:27 GMT
> Here's the code
>
[quoted text clipped - 16 lines]
>
> Why += version working?

"A compound assignment expression of the form E1 op= E2 is equivalent to
E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is
evaluated only once."

[http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5304]

Your += is equivalent to:

b = (int)((b) + (r*10.0));

except for only evaluating b once, which makes no difference because
evaluating b has no side effects.

The line that gives the error does not have the (int) cast that is
implied by the +=.

Patricia
Kavya - 28 Oct 2006 02:18 GMT
> > Here's the code
> >
[quoted text clipped - 32 lines]
> The line that gives the error does not have the (int) cast that is
> implied by the +=.

Thank You.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.