The Artima Developer Community
Sponsored Link

Java Answers Forum
a beginner's question

3 replies on 1 page. Most recent reply: Nov 5, 2008 8:27 AM by Daryl Lee

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Jason Lee

Posts: 1
Nickname: jamsbomd
Registered: Oct, 2008

a beginner's question Posted: Oct 27, 2008 7:12 PM
Reply to this message Reply
Advertisement
I'm a java beginner from China. First forgive me for my bad English. I have some questions about java.

First question

int a = 0;
a = a++;
System.out.println(a);

why final ouput is 0?

in C++,similar code's output is 1;

another question

byte b = 127;
b+=2;
b=b+2;

why b=b+2 get compiler error and b+=2 not?


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: a beginner's question Posted: Oct 29, 2008 6:00 AM
Reply to this message Reply
these are actually not beginner questions :)
Most people will never have to think avout this.

to question 1 I can only assume that since the ++ stands right of the variable the value gets first copied into a cache, then a gets incremented, then a gets overwritten with the value in the cache/register of the processor.
In this case the increment command would be executed before the value gets copied.
a=++a;

In question 2 you have 2 different processor commands.

In one you call a increment command.

The other one consists of a arithmetic operation and a assignment.

Seems the compiler does no precision check at the increment command but only when you assign a value to a variable.

sheela tiwari

Posts: 3
Nickname: tiwari08
Registered: Nov, 2008

Re: a beginner's question Posted: Nov 4, 2008 3:54 AM
Reply to this message Reply
but if we write a=a+1 it is giving the output 1.

Daryl Lee

Posts: 1
Nickname: radlyeel
Registered: Aug, 2008

Re: a beginner's question Posted: Nov 5, 2008 8:27 AM
Reply to this message Reply
The first problem (a = a++) actually exposes a huge difference between C++ and Java.

Here's what happens in C/C++, step by step:
1. The address of a's storage location is loaded into a register.
2. The value of a is read and copied into a using the register pointer. (At this point, a is 0.)
3. a is incremented using the register pointer. So a now equals 1.

Here's what happens in Java, step by step:
1. The value of a is read into a register.
2. The value is copied into a's storage location. a is still 0.
2. The value is incremented, but the copy has already been done so the new value is thrown away. a is still 0.

The distinction is that in C/C++, variables have addresses and those addresses are used for accessing the variables. In Java, variables have references, not addresses. This is how Java afficionados can trumpet "no pointers in Java."

I'd also mention that "a = a++" is bad usage in either language. As readers of the program, we want it to mean "let a = a + 1". But your demo has shown that's not reliable. If you want "a = a + 1", write "a++;" (not a = a++;) in both languages and you get the same results.

As to your b = b + 2 question, alas, you have run afoul of one of Java's interesting "why did they do it this way" situations. The best explanation I've found is at http://www.jguru.com/faq/view.jsp?EID=13647.

Flat View: This topic has 3 replies on 1 page
Topic: check this out....what is the output Previous Topic   Next Topic Topic: Any website for cheking the code output

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use