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 / August 2007

Tip: Looking for answers? Try searching our database.

I have doubt

Thread view: 
Manju - 14 Aug 2007 10:03 GMT
int x=2;
int y;
y=x++*++x;

For this output is 8
I would like to know how this internally works
LewyG - 14 Aug 2007 10:34 GMT
> int x=2;
> int y;
> y=x++*++x;
>
> For this output is 8
> I would like to know how this internally works

y = 2 [3] * [4] 4;

y = 2*4 = 8;

in [] is value after incrementation to show how this goes.
Manju - 14 Aug 2007 12:38 GMT
hii

thanks for reply. I perfectly agree with u but when I think about
memory allocation of this expression I am not able to analyze it.
Could u explain that??

Best Regards

Manjiri (Manju)

> > int x=2;
> > int y;
[quoted text clipped - 8 lines]
>
> in [] is value after incrementation to show how this goes.
Andrew Thompson - 14 Aug 2007 13:19 GMT
>hii
>
>thanks for reply.

A couple of points to consider..
1) Most folks around these groups prefer 'best effort'
spelling.  That excludes 'cute' SMS style abbreviations
like 'u' for 'you', or the term 'hii' instead of the more
generic/common 'hi'.  
2) Use of a single capital letter at the start of each
sentence (or important expression, like 'Hi') can make
a post a lot easier to 'skim' or read quickly - this also
encourages people to help.
3) A lot of folks prefer if you 'post in-line with trimming'
rather than top-post replies.  See Lew's reply for an
example, but for more details look to..
<http://www.physci.org/codes/javafaq.html#toppost>
..and..

> ...I perfectly agree with u but when I think about
>memory allocation of this expression I am not able to analyze it.
>Could u explain that??

4) One single '?' mark - quite intelligently used to identify
your (latest) technical question - is always enough.

I hope you find the technical resolution to the problem,
and also hope that my tips might be of use to you.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Andrew Thompson - 14 Aug 2007 13:28 GMT
>>hii
>>
>>thanks for reply.
>
>A couple of points to consider..

5) A subject line of 'I have doubt' is a very poor subject
line as read by this (western) mind.  "If you have doubt,
consult your guru/priest."  The word 'doubt' probably has
a significantly different meaning to you, than it does to me.
Sticking to a technically specific subject line, perhaps
lacking *any* verbs like 'doubt' or references to people
(nouns),  like 'I' - would be best.  Even something like..
  Internal works '++'?
..(with or without the question mark) would have been a
better 'subject line' for this post, in that anybody who had
an in depth knowledge of the JLS (Java Language Spec.)
would be tempted, even from the title, to read the post,  
& 'puzzle out' the question you had.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Lasse Reichstein Nielsen - 14 Aug 2007 23:07 GMT
>>>hii
>>>
[quoted text clipped - 4 lines]
> 5) A subject line of 'I have doubt' is a very poor subject
> line as read by this (western) mind.

I once heard explained that this is indeed a common mistranslation
of "I have a question". I don't remember the language that causes
this mistake, but the original poster's IP places him in India,
so Indian is a good guess.

/L
Signature

Lasse Reichstein Nielsen  -  lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
 'Faith without judgement merely degrades the spirit divine.'

Andrew Thompson - 15 Aug 2007 00:50 GMT
> >>>hii
>
[quoted text clipped - 7 lines]
> I once heard explained that this is indeed a common mistranslation
> of "I have a question".

I's say that is a good bet.

I aim to correct it wherever I see it.  There
are particular mangling's of the English
language that I will overlook, and still others
that I do myself, but *that* is not one that I
can let pass without comment..

It is my problem, I know, and I'm dealing with it.
..In a very public, ..archived way.   ;-)

Andrew T.
Lew - 15 Aug 2007 02:04 GMT
> It is my problem, I know, and I'm dealing with it.
> ...In a very public, ..archived way.   ;-)

I have a doubt that that will worry you for very long.

Signature

Lew

Lasse Reichstein Nielsen - 15 Aug 2007 07:32 GMT
>> It is my problem, I know, and I'm dealing with it.
>> ...In a very public, ..archived way.   ;-)
>
> I have a doubt that that will worry you for very long.

Yeah, I question that too.

/L ;)
Signature

Lasse Reichstein Nielsen  -  lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
 'Faith without judgement merely degrades the spirit divine.'

blmblm@myrealbox.com - 15 Aug 2007 11:46 GMT
> > >>>hii
> >
[quoted text clipped - 15 lines]
> that I do myself, but *that* is not one that I
> can let pass without comment..

Skitt's law [*] in action?  (Did you really want that apostrophe
in "mangling's"?)

[*] http://en.wikipedia.org/wiki/Skitt%27s_law .  It's amazing
some of the stuff one can find in Wikipedia ....

> It is my problem, I know, and I'm dealing with it.
> ..In a very public, ..archived way.   ;-)

This usage ("doubt" for "question") bugs me too.  But I had
a discussion of it in some newsgroup not long ago, and one of
the other posters claimed that it was standard usage in India,
and that perhaps the people doing it had a good claim to be
regarded as native speakers of yet another version of English
("Indian English" as opposed to "US English", maybe).  So I'm
less inclined than I used to be to call it a mistake.  FWIW,
maybe.

Signature

B. L. Massingill
ObDisclaimer:  I don't speak for my employers; they return the favor.

Andrew Thompson - 14 Aug 2007 13:34 GMT
..
>3) ... See Lew's reply ...

Oops!  Make that *LewyG*!  

My apologies to both of you - I'll try to check the
attribution lines more carefully in future.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Lew - 14 Aug 2007 13:44 GMT
> 3) A lot of folks prefer if you 'post in-line with trimming'
> rather than top-post replies.  See Lew's reply for an

You mean "LewyG"'s reply.  "Lew" is a different person from "LewyG".  By
calling it "Lew"'s reply some might think you meant a different "Lew".

Perhaps it's the name, but "LewyG" does seem most wise and perspicacious.

Signature

Lew

Manju - 16 Aug 2007 05:21 GMT
Hi

I will follow your tips given to me.

Thanks

Best Regards

Manjiri Tatke

> >hii
>
[quoted text clipped - 29 lines]
>
> Message posted via JavaKB.comhttp://www.javakb.com/Uwe/Forums.aspx/java-general/200708/1
Patricia Shanahan - 14 Aug 2007 13:52 GMT
Like many expressions, evaluation of x++*++x requires the use of a
couple of temporary storage locations. In the bytecode model, they will
be locations on the virtual machine's stack, but most real machines use
registers instead.

Either way, I would not expect any memory to be allocated during
evaluation of the expression.

Patricia

> hii
>
[quoted text clipped - 16 lines]
>>
>> in [] is value after incrementation to show how this goes.
LewyG - 14 Aug 2007 14:33 GMT
A: Because this breaks natural reading order
Q: Why?
A: Answering over the post.
Q: What makes people angry while reading Usenet?

;-)
Patricia Shanahan - 14 Aug 2007 16:14 GMT
> A: Because this breaks natural reading order
> Q: Why?
> A: Answering over the post.
> Q: What makes people angry while reading Usenet?
>
> ;-)

A: Because the order makes no sense at all
A: Mixing top and bottom posting in the same article
Q: What makes people really confused while reading Usenet?
Q: Why?

:-)

Patricia
Daniel Pitts - 14 Aug 2007 20:48 GMT
> > "Manju" <manjiri...@gmail.com> wrote in message
>
[quoted text clipped - 12 lines]
>
> > in [] is value after incrementation to show how this goes.

> hii
>
[quoted text clipped - 5 lines]
>
> Manjiri (Manju)

I think the one thing that nobody thought to mention.  If you don't
understand the results clearly, then the expression is too complex.
You might have understood it better as
int x = 2;
int y = x;
x++;
++x;
y *= x;

Anyway, my point is that you should avoid ambiguous and confusing
expressions. Even if the compiler knows what you want, and its "well
defined" behavior, source code is intended for humans, not compilers.
You are writing code to be understandable in the future.
Alexey - 14 Aug 2007 20:53 GMT
> hii
>
[quoted text clipped - 22 lines]
>
> > in [] is value after incrementation to show how this goes.

Sometimes the best thing to do is show rather than tell.

/cygdrive/d/dl# cat TestPP.java
public class TestPP
{
       public static void main(String[] args)
       {
               int x = 2;
               int y = x++ * ++x;
               System.out.println(y);
       }
}
/cygdrive/d/dl# javac TestPP.java
/cygdrive/d/dl# java -cp . TestPP
8
/cygdrive/d/dl# javap -c -classpath . TestPP
Compiled from "TestPP.java"
public class TestPP extends java.lang.Object{
public TestPP();
 Code:
  0:   aload_0
  1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
  4:   return

public static void main(java.lang.String[]);
 Code:
  0:   iconst_2
  1:   istore_1
  2:   iload_1
  3:   iinc    1, 1
  6:   iinc    1, 1
  9:   iload_1
  10:  imul
  11:  istore_2
  12:  getstatic       #2; //Field java/lang/System.out:Ljava/io/
PrintStream;
  15:  iload_2
  16:  invokevirtual   #3; //Method java/io/PrintStream.println:(I)V
  19:  return

}
Roedy Green - 14 Aug 2007 21:55 GMT
>y=x++*++x;
>
>For this output is 8
>I would like to know how this internally works

This sort of code is not really Java.  I would fire anyone who wrote
like this.  This is the stuff for the unmaintainable code essay.

http://mindprod.com/jgloss/unmain.html

There are several ways to solve this problem.  One is to read the JLS
on pre and post increment.  see http://mindprod.com/jgloss/jls.html
Basically post increment samples the value the increments.
Preincrement increments the value then samples.

Another way is to examine the byte code to see how it compiled.

public class PlusPlus
  {

  /**
   * test harness
   *
   * @param args not used
   */
  public static void main ( String[] args )
     {
     int x=2;
     int y = x++*++x;
     System.out.println( y );
     }
  }

I disassembled with Javap which comes with the JDK.

javap.exe -classpath . -c -private -s -verbose PlusPlus

The Byte code looks like this:

  0:   iconst_2
  1:   istore_1
  2:   iload_1
  3:   iinc    1, 1
  6:   iinc    1, 1
  9:   iload_1
  10:  imul
  11:  istore_2

The byte code is pretty pedestrian, not in the least optimised. It is
postfix stack based. Here is an approximation to what it does in
simple Java.

int  temp1=2;
int calc1 = temp1;
temp1++;
temp1++;
calc2 = temp1;
calc3 = calc1*calc2;
int temp2 = calc3;

So in English what is happening is this:

// Be very careful reusing a pre or post increment variable in an
expression.
int x = 2;
int y = x++ * ++x;  // result is y = 8;

// it works as if you had coded:
int x = 2;
int a = x++;   // a=2; x=3;
int b = ++x;   // b=4; x=4
int y = a * b; // y = 8

The key to understanding is the postincrement is done immediately
after sampling, not after the whole expression is computed.

Another way to solve this problem would be to read the precedence
entry in the Java glossary at:
http://mindprod.com/jgloss/precedence.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



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.