Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how this uotput comes?

 
amrita sankar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Q13
{
static int call(int x)
{
try
{
System.out.println(x---x/0);
return x--;
}
catch(Exception e)
{
System.out.println(--x-x%0);
return x--;
}
finally
{
return x--;
}
}
public static void main(String[] args)
{
System.out.println(" value = "+ call(5));
}
}

For this question output is 3.How it comes? Please explain.
 
harish shankarnarayan
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after the call(5)

control goes to the method

as divison by zero ,it goes to the exception block there the value is 4,
and then as finally block always executes in the finally block value becomes 3
hope so i am right

good book for SCJP is by kathy sierra and bert bates,ita available inall the book stores.

hope this helps u
good luck
 
amrita sankar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by harish shankarnarayan:
after the call(5)

control goes to the method

as divison by zero ,it goes to the exception block there the value is 4,
and then as finally block always executes in the finally block value becomes 3
hope so i am right

good book for SCJP is by kathy sierra and bert bates,ita available inall the book stores.

hope this helps u
good luck



Thank you.I understood.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SCJP questions, like this one, go in the SCJP forum. I'll move it there for you.
 
vandu matcha
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the statement...System.out.println(x---x/0);
how it is considered..it can be taken as
(x--) -(x/0) aswell as
(x)- (--x/0..)
so for the first one ..ans is 2
second one ans is 3...
i am a bit confused.....how this statement is to be considered...
 
ven kaar
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
based on operator precedence,

(x--)-(x/0) is my guess

but still, both version give 3 as output
 
Tilo Hemp
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ven, you are right. a simple test reveals it. if anybody wants to cut and paste, for your convenience:

public class Minusminus {
static public void main(String...args){
int i = 3, j = 2;
System.out.println("the mysterious expression is: " + (i---j) +" i = "+i+", j = "+j);
}
}
[ January 25, 2006: Message edited by: Tilo Hemp ]
 
ven kaar
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
based on operator precedence,

(x)-(--x/0) prefix goes before postfix

http://highered.mcgraw-hill.com/sites/0072488190/student_view0/operator_precedence_chart.html
 
vandu matcha
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank u tilo for the example...and ven ..for the chart repersenting the operators and their precendence..thank a lot...going to write exam within 2 days..bit tensed.....thanks for the help
 
Naresh Gunda
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
operators chart is good but, the chart is not showing any bitshift operators.
 
ven kaar
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html
 
Simi gupta
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
 
Simi gupta
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry,
The last post was by mistake, i want to know what happens when an Exception occurs in catch block itself as in the above code it does but it doesn't print the exception stack trace but simply prints the output from main().
Regards,
Simi
 
Tilo Hemp
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ven, I think your first explanation was right... prefix has a higher priority than postfix, but the expression is evaluated from left to right.

here's another one:
int i = 3, j = 2;
System.out.println("the expression is: " + (i---++j) +" i = "+i+", j = "+j);

quite simple i guess.

but I wonder where the precedence "prefix before postfix" ever is of importance? an expression like ++i-- is not accepted by the compiler.
 
ven kaar
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Tilo u r rite, the first answer is correct
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic