beginner in java

As i am a beginner in java.i could not understand this program.this is a simple program of postfix and prefix increment operator.but i could not understand the output of this program.i understand the output of variable c and d but the output of a and b confuses me.if somebody please help me.pls help me so that i can move forward in java.thank you.



class in
{
public static void main(String args[])
{
int a = 3;
int b = 7;
int c = a++;
int d = ++b;
System.out.println("a = "+a);
System.out.println("b = "+b);
System.out.println("c = "+c);
System.out.println("d = "+d);
}
}


output is
a = 4
b = 8
c = 3
d = 8

Comments

  • In a postfix usage, the value of a is assigned to c, then incremented by one. In the prefix usage, the value of b is incremented, then assigned to d.
  • sir,
    i also knew this but my question is that why the values of a and b incremented despite the fact we don't use any increment operator with both these variables.please explain me the outcome of the following code:
    System.out.println("a = "+a);
    System.out.println("b = "+b);
    why the outcome of this code is:
    a = 4;
    b = 8;
    i am very grateful to you.if you or anybody explain this.

  • tarun01,
    The add 1 prefix and postfix operator is, ++ (notice it is [b]two + signs[/b]). Their location before, prefix, or after, postfix, a variable determine when the value of the variable is incremented by one.

    The postfix and prefix operations on the variables occurs in these lines:
    [code]
    int c = a++; // c is assigned value of a, 3, then a is incremented by 1
    int d = ++b; // b is incremented by 1, then d is assigned value of b [/code]
    What happens in these lines
    [code]
    System.out.println("a = "+a);
    System.out.println("b = "+b);[/code]
    is that the variables are printed. The [b]single + sign[/b] in these expressions is a [b]concatenation operator[/b] that unites the strings on both sides of the + sign.
  • do not need to worry

    first your a=3 then in your program your given c=a++

    here a++ is post increment operator.here in a++ a value become 4
    and it is assigned after so a=4 and c=3


  • Ok, I'm new to Java, but I think I got this.

    So what happens in this line:
    int c = a++; --> 'a' is now incremented to = 4

    So by the time we approach:
    System.out.println("a = "+a);
    --> you're getting "a =" (a String) + (concatenated) "+a" which is printing out the String form of "a", which because of the above statement now equals '4'.

    To prove, if you comment out the line:
    int c = a++;
    --> System.out.println("a = "+a); will produce a String output of: "a = 3"

    Something else to try is if you modify to:
    System.out.println("a = " + a + 1);
    --> you'll get '4'(String) + concatenated with '1' (String) with an output of: "a = 41)

    Hope this helps.
  • this is simple
    a++ is like a=a+1 so a=4
    and ++b is the same in this case but has other signification in other case
  • These things are avaiable in all major browsers
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories