File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please explain what is happing behind the code

 
varinder mahajan
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class a
{
public static void main(String str[])
{
int a=10;
++a;
System.out.pritnln("a:"+a);
}
}


public class a
{
public static void main(String str[])
{
int a=10;
a++;
System.out.pritnln("a:"+a);
}
}

The out put in case first is 10 and in case second it is 11...why it is so..
it seems as output should be same in both cases. but actually it is not so..
[ August 11, 2008: Message edited by: varinder mahajan ]
 
Shyam Sunder V R
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please run the program properly.The outputs in both the cases is a:11
 
Sagar Rohankar
Ranch Hand
Posts: 2904
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried it on your own machine, because on my machine it correctly showing the output this is "a = 11";
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you modify the code like this then it will give output 11 and 10.

public class a
{
public static void main(String str[])
{
int a=10;
System.out.pritnln(++a);
}
}


public class a
{
public static void main(String str[])
{
int a=10;
System.out.pritnln(a++);
}
}

then it becomes a problem of postfix vs prefix......

I will not explain it..I am tired of these prefix and postfix problems..
 
Vinod Kumar Kommineni
Ranch Hand
Posts: 54
Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya its 11 in both cases please check once again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic