+ 1
Why the output of i is 3 instead of 2?
8 Answers
+ 4
public class Arrays
{
public static void main(String[] args) {
int a[] ={5,1,15,20,25};
int i,j,m;
i = ++a[1]; // đhere , i becomes ++1 ie 2
j = a[1]++;
m = a[i++]; // đhere , i becomes ++2 ie 3
System.out.println(i);
System.out.println(j);
System.out.println(m);
System.out.println(i+j+m);
}
}
+ 3
no , see it again a [1] was 1 already ,
then i = ++ a [1] , ie a [1] becomes 2
now , j=a [1]++ ie j=2++ means j will store 2
hope u got it đ
+ 3
int a = 1 ;
int b = ++a; //b will store value 2 , ie first increase then assign
int c =b++ ; //c will store value 2 , ie first assign then increase
+ 2
public class Arrays
{
public static void main(String[] args) {
int a[] ={5,1,15,20,25};
int i,j,m;
i = ++a[1];
System.out.println(a[1]); //đhere a [1] becomes 2
j = a[1]++; // đnow j=a [1]++ = a [1] = 2
m = a[i++];
System.out.println(i);
System.out.println(j);
System.out.println(m);
System.out.println(i+j+m);
}
}
+ 1
if that is a case why j didin't come to 15?...refer the increment operator at j...it look like that of i
+ 1
think of this......
if a = 2
b= 2
then
++a = 3
and
b++ = 2.....
you want to mean that it work different in arrays????
+ 1
so what is the effect of increment operator before and after the variable???
+ 1
thanx...guyz!!