Frage

Gibt es einen Unterschied zwischen ++x und x++ in Java?

War es hilfreich?

Lösung

++ x heißt Vorinkrement während x ++ Postinkrement genannt wird.

int x = 5, y = 5;

System.out.println(++x); // outputs 6
System.out.println(x); // outputs 6

System.out.println(y++); // outputs 5
System.out.println(y); // outputs 6

Andere Tipps

ja

++ x erhöht den Wert von x und kehrt dann x
x ++ den Wert von x zurück und erhöht dann

Beispiel:

x=0;
a=++x;
b=x++;

, nachdem der Code ausgeführt wird a und b werden 1 x aber wird 2 sein.

Diese werden als Postfix und Präfix-Operatoren bekannt. Beide werden 1 zu den Variablen hinzufügen, aber es gibt einen Unterschied im Ergebnis der Anweisung.

int x = 0;
int y = 0;
y = ++x;            // result: y=1, x=1

int x = 0;
int y = 0;
y = x++;            // result: y=0, x=1

Ja,

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

druckt 6 und

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

wird 5 drucken.

Ich landete hier von einem seiner letzten dup 's, und obwohl diese Frage mehr als beantwortet ist, kann ich nicht decompiling den Code helfen und fügen hinzu: ‚und noch eine Lösung‘: -)

Um genau zu sein (und wahrscheinlich ein bisschen pedantisch)

int y = 2;
y = y++;

kompiliert in:

int y = 2;
int tmp = y;
y = y+1;
y = tmp;

Wenn Sie javac diese Y.java Klasse:

public class Y {
    public static void main(String []args) {
        int y = 2;
        y = y++;
    }
}

und javap -c Y, erhalten Sie die folgenden JVM-Code (Ich habe mir erlaubt, das Hauptverfahren mit Hilfe der Java Virtual Machine Specification ):

public class Y extends java.lang.Object{
public Y();
  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 // Push int constant `2` onto the operand stack. 

   1:   istore_1 // Pop the value on top of the operand stack (`2`) and set the
                 // value of the local variable at index `1` (`y`) to this value.

   2:   iload_1  // Push the value (`2`) of the local variable at index `1` (`y`)
                 // onto the operand stack

   3:   iinc  1, 1 // Sign-extend the constant value `1` to an int, and increment
                   // by this amount the local variable at index `1` (`y`)

   6:   istore_1 // Pop the value on top of the operand stack (`2`) and set the
                 // value of the local variable at index `1` (`y`) to this value.
   7:   return

}

So haben wir schließlich:

0,1: y=2
2: tmp=y
3: y=y+1
6: y=tmp

Wenn man bedenkt, was der Computer tatsächlich funktioniert ...

++ x. Last x aus dem Speicher, erhöht, zu verwenden, speichern zurück in dem Speicher

x ++:. Last x aus dem Speicher, Verwendung, erhöht, speichere zurück in dem Speicher

Bedenken Sie:     a = 0     x = f (a ++)     y = f (++ a)

wo Funktion f (p) liefert p + 1

x wird 1 (oder 2)

y wird 2 (oder 1)

Und darin liegt das Problem. Hat der Autor des Compiler die Parameter nach dem Abrufen passieren, nach Gebrauch oder nach der Lagerung.

Im Allgemeinen verwenden Sie einfach x = x + 1. Es ist viel einfacher.

In Java gibt es einen Unterschied zwischen x ++ und ++ x

++ x ist ein Präfix Form: Es erhöht der Variablen Ausdruck dann den neuen Wert im Ausdruck verwendet.

Zum Beispiel, wenn im Code verwendet:

int x = 3;

int y = ++x;
//Using ++x in the above is a two step operation.
//The first operation is to increment x, so x = 1 + 3 = 4
//The second operation is y = x so y = 4

System.out.println(y); //It will print out '4'
System.out.println(x); //It will print out '4'

x ++ ist eine Postfix-Form: Der Variablen Wert wird zuerst in dem Ausdruck verwendet werden, und dann wird es nach der Operation erhöht.

Zum Beispiel, wenn im Code verwendet:

int x = 3;

int y = x++;
//Using x++ in the above is a two step operation.
//The first operation is y = x so y = 3
//The second operation is to increment x, so x = 1 + 3 = 4

System.out.println(y); //It will print out '3'
System.out.println(x); //It will print out '4' 

Hoffe, das ist klar. Laufen und mit dem obigen Code spielen sollte Ihr Verständnis helfen.

Ja.

public class IncrementTest extends TestCase {

    public void testPreIncrement() throws Exception {
        int i = 0;
        int j = i++;
        assertEquals(0, j);
        assertEquals(1, i);
    }

    public void testPostIncrement() throws Exception {
        int i = 0;
        int j = ++i;
        assertEquals(1, j);
        assertEquals(1, i);
    }
}

Ja, mit ++ X, X + 1 wird in dem Ausdruck verwendet werden. Mit X ++, wird X in dem Ausdruck verwendet werden, und X wird nur dann erhöht werden, nachdem der Ausdruck ausgewertet wurde.

Also, wenn X = 9, mit ++ X, der Wert 10 verwendet werden, sonst wird der Wert 9.

Wenn es wie viele andere Sprachen ist es notwendig, einen einfachen Versuch hat:

i = 0;
if (0 == i++) // if true, increment happened after equality check
if (2 == ++i) // if true, increment happened before equality check

Wenn die oben nicht geschieht so, können sie gleichwertig sein

Ja, kehrte der Wert ist, den Wert nach und vor der Inkrementierung ist.

class Foo {
    public static void main(String args[]) {
        int x = 1;
        int a = x++;
        System.out.println("a is now " + a);
        x = 1;
        a = ++x;
        System.out.println("a is now " + a);
    }
}

$ java Foo
a is now 1
a is now 2

OK, ich hier gelandet, weil ich vor kurzem über die gleiche Frage kam, als die klassische Stack-Implementierung zu überprüfen. Nur eine Erinnerung daran, dass dies in der Array-basierten Implementierung von Stapel verwendet wird, die ein bisschen schneller als die verknüpfte Liste ist eines.

-Code unten, überprüfen Sie die Push- und Pop-Funk.

public class FixedCapacityStackOfStrings
{
  private String[] s;
  private int N=0;

  public FixedCapacityStackOfStrings(int capacity)
  { s = new String[capacity];}

  public boolean isEmpty()
  { return N == 0;}

  public void push(String item)
  { s[N++] = item; }

  public String pop()
  { 
    String item = s[--N];
    s[N] = null;
    return item;
  }
}

Ja, es ist ein Unterschied, einhüllen von x ++ (Postinkrement) Wert von x wird im Ausdruck und x verwendet wird, um 1 erhöht werden, nachdem der Ausdruck ausgewertet wurde, auf der anderen Seite ++ x (Prä) , x + 1 wird in dem Ausdruck verwendet werden. Nehmen wir ein Beispiel:

public static void main(String args[])
{
    int i , j , k = 0;
    j = k++; // Value of j is 0
    i = ++j; // Value of i becomes 1
    k = i++; // Value of k is 1
    System.out.println(k);  
}

Die Frage ist schon beantwortet, aber lassen Sie mich auch von meiner Seite hinzuzufügen.

Zu allererst ++ bedeutet, um eine Nummer nach und -. bedeutet, Abnahme durch ein

Jetzt x ++ bedeutet Increment x nach dieser Zeile und ++ x bedeutet Increment x vor dieser Zeile.

Überprüfen Sie dieses Beispiel

class Example {
public static void main (String args[]) {
      int x=17,a,b;
      a=x++;
      b=++x;
      System.out.println(“x=” + x +“a=” +a);
      System.out.println(“x=” + x + “b=” +b);
      a = x--;
      b = --x;
      System.out.println(“x=” + x + “a=” +a);
      System.out.println(“x=” + x + “b=” +b);
      }
}

Es wird die folgende Ausgabe geben:

x=19 a=17
x=19 b=19
x=18 a=19
x=17 b=17

Bei i++ nennt man das Postinkrementierung, und der Wert wird in jedem beliebigen Kontext verwendet und dann inkrementiert;++i is preincrement erhöht zuerst den Wert und verwendet ihn dann im Kontext.

Wenn Sie es in keinem Kontext verwenden, spielt es keine Rolle, was Sie verwenden, Postinkrementierung wird jedoch konventionell verwendet.

Es gibt einen großen Unterschied.

Wie die meisten Antworten haben bereits die Theorie darauf hingewiesen, würde Ich mag ein einfaches Beispiel hinweisen:

int x = 1;
//would print 1 as first statement will x = x and then x will increase
int x = x++;
System.out.println(x);

Nun wollen wir sehen ++x:

int x = 1;
//would print 2 as first statement will increment x and then x will be stored
int x = ++x;
System.out.println(x);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top