I have doubt

M

Manju

int x=2;
int y;
y=x++*++x;


For this output is 8
I would like to know how this internally works
 
L

LewyG

Manju said:
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.
 
M

Manju

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;
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.
 
A

Andrew Thompson

Manju said:
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..
...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.

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200708/1
 
A

Andrew Thompson

Andrew said:
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.

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200708/1
 
A

Andrew Thompson

Andrew Thompson wrote:
..
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.
 
L

Lew

Andrew said:
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.
 
P

Patricia Shanahan

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

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;
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.
 
L

LewyG

A: Because this breaks natural reading order
Q: Why?
A: Answering over the post.
Q: What makes people angry while reading Usenet?

;-)
 
P

Patricia Shanahan

LewyG said:
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
 
D

Daniel Pitts

y = 2 [3] * [4] 4;
y = 2*4 = 8;
in [] is value after incrementation to show how this goes.
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)

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.
 
A

Alexey

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)

y = 2 [3] * [4] 4;
y = 2*4 = 8;
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

}
 
R

Roedy Green

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
 
L

Lasse Reichstein Nielsen

Andrew Thompson said:
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
 
A

Andrew Thompson

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.
 
B

blmblm

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..

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

[*] http://en.wikipedia.org/wiki/Skitt's_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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top