Jack Pettersson
Jack Pettersson

Reputation: 1666

increment operator in java

Just started learning java, so sorry asking such an arbitrary and silly thing... But i can't wrap my head around the increment operators in java.

Why does this work:

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

>>6

When this doesn't:

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

>>Compilation error: Found value - Required variable.

Shouldn't this operator work like any other arithmetic operator? Why does this one in particular need a variable? I mean a = 5+1; works, so why doesn't a = ++5?

Is there any way to use ++ directly with values?

Upvotes: 4

Views: 388

Answers (4)

Tano
Tano

Reputation: 619

Because ++i is a prefix increment , which is increment by 1 and then use the new value of i in the expression, but i++ is postfix increment which is actually: use the current value of i and then increase it by 1. Here you are some example to understand it better: public static void main(String [] args){

    int a = 3;
    int b = 5;
    System.out.println(++a);
    System.out.println(b++);
    System.out.println(b);

}

Here is the output:

  • 4
  • 5
  • 6

Upvotes: 0

arcy
arcy

Reputation: 13103

In case anyone is interested...

A faint echo I hear behind the original question is "Why is this operator here?". The answer involves a bit of computing history.

Digital Equipment Corporation had created a small computer -- at the time (around 1970), most "computers" as we knew them were Big Iron, IBM ruled the computing world, and DEC's smaller machines were upstarts without wide distribution. I believe the US Government at the time only bought "computers" from IBM, and therefore DEC called their machines "programmable data processors" (PDPs). Their PDP machines were leaders in the 16-bit computing world for years, though there were others. They could be bought for less than a million dollars!

An early machine of theirs was made available to some researchers from AT&T; the machines came without much of an operating system or software, and the researchers developed both a language (named C) and an operating system (Unix).

Starting from earlier languages named 'a' and 'bcpl', the researchers were developing a "system" language, a "structured assembler" -- it was not, and was not meant to be, a 'high-level' language, but instead one that could be used to program these smaller, slower machines for efficient code, allowing control of code size and speed at a level below that of FORTRAN and COBOL, but with programming structure above that of assembler.

The PDP machines had addressing modes that allowed the use of one register to point to the beginning of a block of memory, and another register to 'index' (be an offset) from the first one. It is common, of course, for code to progress through an array of, say, pointers, doing something to each element in the array in turn. So there was an addressing mode in the machine language that allowed obtaining a value from memory from a position in the 'current' element of the array while incrementing the index, all in one machine language instruction.

So the developers of the C language built this autoincrement and autodecrement into the language, allowing compiler writers the chance to use the addressing mode to generate more efficient machine code from the C code than it could otherwise. And the operator was carried forward from C to Java, which borrowed a lot of C's syntax and operator usage.

Upvotes: 0

sepp2k
sepp2k

Reputation: 370082

++x is not a shortcut for x + 1, it's a shortcut for x += 1. That is, in addition to evaluating to x+1, it also increments the value of x by one. This means two things:

  1. x = ++x; is redundant as ++x; by itself already accomplishes the same thing.
  2. ++1 makes no sense as you can't change the value of 1 - it is a constant.

Upvotes: 11

Greg Hewgill
Greg Hewgill

Reputation: 992707

The answer is that ++x changes the value of x, as well as providing an expression result. You can't change the value of 5, if you were to try to use ++5.

If you want a value that is "one more than x", and you don't need to change the value actually stored in x, then the usual way to do this is of course x + 1.

Upvotes: 3

Related Questions