• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Actual parameter checking

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are actual parameter types checked for type compatibilty with the formal parameters at runtime or compile-time? Why?
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compile time

Basically this question is associated with method overloading...it is the responsibility of the compiler to decide which method to run and it makes that decision during compile time.
 
Jeet Jain
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then why is the answer to the following question this: C and E are correct. The invocation of parse() will return 123.456. The invocation of
format() will throw an exception.


2. import java.text.*;
3. public class Gazillion {
4. public static void main(String[] args) throws Exception {
5. String s = "123.456xyz";
6. NumberFormat nf = NumberFormat.getInstance();
7. System.out.println(nf.parse(s));
8. nf.setMaximumFractionDigits(2);
9. System.out.println(nf.format(s));
10. }
11. }

Which are true? (Choose all that apply.)
A. Compilation fails.
B. The output will contain "123.45 "
C. The output will contain "123.456"
D. The output will contain "123.456xyz"
E. An exception will be thrown at runtime.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the API says that it will throw a RuntimeException. In this case look at the API at : NumberFormat's super class Format for the method : public final String format (Object obj). It says : Throws IllegalArgumentException, which is a RuntimeException. public final String format(Object o) will try to format a given Object as a number and I guess the reason it went into RuntimeException and not Compiler Error is that this exception can only be detected when the program is running this particular method.
 
Jeet Jain
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh but i thought the signature for format() was format(double d) and format(long l) ???
 
Jeet Jain
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am very confused about when actual parameters are checked with formal parameters. When I saw the API it gives the above 2 versions of format() not one with Object as parameter.

But what I dont get is why when I pass str.concat(5); it gives compiler error and when i say NumberFormat.getNumberInstance.format(str); it compiles.

Please Help!!!
 
Jeet Jain
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i saw the API at http://download.oracle.com/javase/6/docs/api/index.html
 
Saloon Keeper
Posts: 14011
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like Kris has said, format(Object) is declared in Format, NumberFormat's supertype. NumberFormat inherits this method.
 
Greenhorn
Posts: 6
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below code snippet is run; when nf.format(s) is called. As you can see, Long, Short, Integer, Byte, AtomicInteger and AtomicLong is expected by this method, thus you get an IllegalArgumnentException with specified message at runtime.

 
Jeet Jain
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But thats exactly my problem. The format method() you specified (same as in API) above takes 3 parameters but my format() calls with 1 parameter. And in the API it gives both versions of format() methods. So the one with 1 parameter (format(double) or format(long)) should be called, right? Not the one with 3 parameters.
 
Jeet Jain
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And another example:



Why can't the compiler itself determine that Sets are not Lists? Why does it give a runtime exception?
 
Jeet Jain
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry it does give a compiler error. So there's where my problem lies. It gives an error at compile time but number format gives at runtime.

One more doubt related to this was that why does Collections.sort() on a List<StringBuilder> give exception for natural and total ordering when at compile time itself it can see that StringBuilder is not a subtype of Comparable<? super StringBuilder>?

Signature for sort is:
<E extends Comparable<? super E>> void sort(List<E> list)
 
Oguzhan Acargil
Greenhorn
Posts: 6
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeet Jain wrote:But thats exactly my problem. The format method() you specified (same as in API) above takes 3 parameters but my format() calls with 1 parameter.



NumberFormat doesn' t override format(Object), thus we must look at the parent class (Format) implementation. In that class, format(object) method calls an its abstract method (that must be implemented by subclass->NumberFormat ). The abstract method implementation of NumberFormat is at my previous post. ;)

Format class' abstract method:


 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic