• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

ques. from JWhiz seems to have a wrong answer

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends,
following is a ques. from jwiz.
What will the result be for the following block of code when it is executed?
int i = 3;
int j = 0;
float k = 3.2F;
long m = -3;
if (Math.ceil(i) < Math.floor(k))
if (Math.abs(i) == m)
System.out.println(i);
else
System.out.println(j);
else
System.out.println(Math.abs(m) + 1);
the correct answer given for this is that output wiil b 4.
but what i guess is that it should give a compile time error,as ceil method takes only double as an argument.if we pass integer it will give a compile time error.
can anybody help me.
neha.
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by neha jain:
the correct answer given for this is that output wiil b 4.
but what i guess is that it should give a compile time error,as ceil method takes only double as an argument.if we pass integer it will give a compile time error.
can anybody help me.
neha.


The correct answer is 4. No compile error in this case. It is *always* possible to pass an integer to a method which takes double as per conversion rules. Conversion rules for primitives take place in -

    * Assignemnt
    * Method Calls
    * Arithetic operations

    HTH,
    - Manish
 
Sometimes you feel like a nut. Sometimes you feel like a tiny ad.
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic