aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes please explain what is happing behind the code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "please explain what is happing behind the code" Watch "please explain what is happing behind the code" New topic
Author

please explain what is happing behind the code

varinder mahajan
Ranch Hand

Joined: Jun 11, 2008
Posts: 47
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 ]

Beat the world,if you can.......
Shyam Sunder V R
Greenhorn

Joined: Jul 29, 2008
Posts: 20
Please run the program properly.The outputs in both the cases is a:11
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Have you tried it on your own machine, because on my machine it correctly showing the output this is "a = 11";


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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..


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Vinod Kumar Kommineni
Ranch Hand

Joined: Jun 12, 2008
Posts: 54

ya its 11 in both cases please check once again


Regards
vinod SCJP 6.0
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please explain what is happing behind the code
 
Similar Threads
parameter passing
Referenec passing
what wll be the out put and why?
Referenec passing vs. Valuse passing
Turning an Int into a String