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 / September 2006

Tip: Looking for answers? Try searching our database.

I am always afraid of this  code

Thread view: 
gk - 14 Sep 2006 15:34 GMT
I am always afraid of this

public class Static
{
     static
     {
            int x = 5;
     }

     static int x,y;
     public static void main(String args[])
     {
            x--; myMethod();
            System.out.println(x + y + ++x);
     }

     public static void myMethod()
     {  System.out.println(x );
            y = x++ + ++x;

        System.out.println(x );
     }
}

ohh...horrible of ++  and  ++  operator ......

+x  means ...i understand...first increment and then its used  in the
next statement

x+ means .....first utilized and then increment in the next statement

but  still ...its pathetic to predict the output .....my headache.
Tor Iver Wilhelmsen - 15 Sep 2006 17:48 GMT
> I am always afraid of this
>
[quoted text clipped - 3 lines]
>      {
>             int x = 5;

This is a method variable in the static initializer that masks the
static field. Just so you know.

>      }
>
>      static int x,y;

These are initialized to 0.

> ohh...horrible of ++  and  ++  operator ......

Yes, but at least the precedence and consequence are specified in the
JLS - in C, you would have no idea since it's not defined. Unless the
standards have been updated, but even then you have no guarantee a
given compiler follows the standard.
Patricia Shanahan - 15 Sep 2006 18:14 GMT
> I am always afraid of this
>
[quoted text clipped - 24 lines]
> +x  means ...i understand...first increment and then its used  in the
> next statement

In Java, ++x is an operation that increments x by one, and whose result
is the incremented value.

> x+ means .....first utilized and then increment in the next statement

Similarly, x++ is an operation that increments x by one, and whose
result is the value of x before the increment.

The result will not appear to be used until after x++ has been
completely executed.

> but  still ...its pathetic to predict the output .....my headache.

The output can be predicted by working through each expression, in the
light of the JLS. However, as the JLS says,

"Code is usually clearer when each expression contains at most one side
effect, as its outermost operation, and when code does not depend on
exactly which exception arises as a consequence of the left-to-right
evaluation of expressions."

"y = x++ + ++x;" has three side effects. Not good code.

Patricia


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.