• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how this uotput comes?

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SCJP questions, like this one, go in the SCJP forum. I'll move it there for you.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
based on operator precedence,

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

but still, both version give 3 as output
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
 
Simi gupta
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Tilo u r rite, the first answer is correct
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic