6 Replies Latest reply: Nov 30, 2010 3:01 AM by 819746 RSS

    about java _what iwll be the output of this and why

    759887
      public class Test {
      public static void printValue(int i, int j, int k){
      System.out.println("int");
      }
      public static void printValue(byte...b){
      System.out.println("long");
      }
      public static void main(String... args) {
      byte b = 9;
      printValue(b,b,b);
      }
      }

      what iwll be the output of this and why
        • 1. Re: about java _what iwll be the output of this and why
          Darryl Burke
          | Moderator advice: | Please read the announcement(s) at the top of the forum listings and the FAQ linked from every page. They are there for a purpose. |
          | | Don't double post. I've removed the other thread you started with the same question. |
          | | Don't post inane responses to old threads and don't ask for another member's email address. I've deleted two of your posts in other threads. |
          | | A forum is not a homework dump. Recommended reading: [url http://catb.org/~esr/faqs/smart-questions.html]How to ask questions the smart way |

          db
          • 2. Re: about java _what iwll be the output of this and why
            759887
            ok i will see to it that it never happens again , the code piece which i posted is ok, or you want me to improve on that too, is it the right way or u can suggest
            • 3. Re: about java _what iwll be the output of this and why
              796440
              gaurav wrote:
              what iwll be the output of this and why
              For the "what" part, run it and see. For the "why" part, if you don't understand why it gives that output, indicate what you thought it would give instead, and why.

              Also, when posting code, put { cdoe } before and after it (without the spaces inside the brackets) so it will be readable. Use the Preview tab to see how your post will look before submitting.
              • 4. Re: about java _what iwll be the output of this and why
                759887
                output is int and i don't know why
                • 5. Re: about java _what iwll be the output of this and why
                  796440
                  The JLS has a rather lengthy section that spells out the rules for which signature is called when multiple signature could apply. The main rule is that the "most specific" signature is called. Sometimes, this is obvious. For instance:
                  void foo(Object o) {}
                  
                  void foo(String s) {}
                  
                  ...
                  
                  String s = "abc";
                  foo(s);
                  In this case, String is clearly more specific than Object.

                  However, in other cases, it's less intuitive. In this particular case, I would not have known which one would be called without looking it up or trying it. I don't know the reason why, and I don't care, since it's very unlikely I'll come across anything like this in the real world.

                  If you're curious as to the specific rule, read [url http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12.2]JLS 15.12.2 Compile-Time Step 2: Determine Method Signature. My guess would be that an exact number of arguments (as opposed to fitting due to varargs) carries more weight than the types matching exactly, although I'm sure the spec spells it out in greater detail.
                  • 6. Re: about java _what iwll be the output of this and why
                    819746
                    output will be int

                    because widening is higher precidence than -public static void printValue(byte...b)