Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Matching overloaded methods and widening primitives

 
Ranch Hand
Posts: 94
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers!
I was sweeping some of flashcard of this app when a question came up with my gasp


Which order does Java use to looking for matches in overloaded methods: autoboxing, exact match, widening primitives, varargs?


I'm afraid I didn't get the answer very well. Can you explain what does it mean?
Moreover: what's the meaning of the terms 'widening primitives'?

By the way the answer was :


  • exact match
  • widening primitives
  • autoboxing
  • varargs

  •  
    Bartender
    Posts: 1868
    81
    Android IntelliJ IDE MySQL Database Chrome Java
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hopefully I can help explain this.

    Exact Match:
    When you have a method which takes a parameter of a certain type and you supply a value to that method which is of that certain type e.g.
    In the above code, the method is expecting an int and we are providing an int so we are preforming an exact match.

    Widening Primitives:
    For this you need to know that a byte is smaller then a short and a short is smaller then an int etc.
    However you also need to know that a short covers the same range of numbers and more then byte and that int covers the same range of numbers and more of short.
    In the above code we are providing a byte value to a method which expects an int value. But because int covers all of the possible numbers in byte range this is perfectly fine.

    Autoboxing:
    Each of the primitives have object counter parts e.g. byte and Byte, short and Short, int and Integer. But the cost of the object is more then the cost of a primitive in terms of memory and CPU cycles.
    In the code block the method is expecting an Integer value, but a int primitive is supplied. Java then automatically converts the value from an int to an Integer.
    Note: The following code block generates a compile error because a byte variable can not be auto boxed into a Integer object
    The compile error is "error: incompatible types: byte cannot be converted to Integer"

    Varargs:
    Varargs are treated as an array. Varargs have use more memory and CPU cycles then primitives.
    In the above code block two values are passed, but Java combined them into an array.

    Conclusion:
    Java tries it's best to save on to use the least amount of memory and CPU cycles as possible.
    So Java tries to fit the values passed to the method to the smallest defined parameters of that method using the order provided in your first posting.
     
    Ranch Hand
    Posts: 151
    3
    Eclipse IDE C++ Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I haven't used varargs before, but aren't they just for accepting option flags when running the app from commandline? I don't understand what varargs have to do with boxing of primitives.
     
    Pete Letkeman
    Bartender
    Posts: 1868
    81
    Android IntelliJ IDE MySQL Database Chrome Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    S Fox wrote:I haven't used varargs before


    You most likely have, however you haven't really realized it given the many faces on the main method. For instances these are all the same according to Java:

    S Fox wrote:... but aren't they just for accepting option flags when running the app from commandline?


    I'm not too sure where else they are used, but anywhere you use arrays you could use varargs, providing that is the last parameter of the method and only used once.
    Given that this posting was create in the Associate Certification (OCAJP 7/8) area the context of this is for certification and not necessarily practical.

    There are bound to be questions on almost every exam/test which don't have too many perceived practical use cases.
    Not only that some (most?) of the questions on the OCA 808 and OCP 809 exams are designed to trick you and make you think.
    Plus for the OCA 808 and OCP 809 exams the you can also expect the code to have very bad formatting and variable names that you shouldn't use (but you are legally allowed to).

    Personally I tend use the array notation for type of method declaration whenever possible as I find it easier to read.

    You may need to know this for the certification tests/exams.

    S Fox wrote:I don't understand what varargs have to do with boxing of primitives.


    A command separated listed can be auto boxed into an array object.
    Let's look at some more elaborate code:
    Output:

    You can see that if you pass only two int values to the function you get the exact match method executing.
    However if you provide int value or more then two int values the other method is called.
    For the certification test/exam you could very well be asked which method is called with given the 25 lines of code in this post.
     
    Daniele Barell
    Ranch Hand
    Posts: 94
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Pete Letkeman wrote:Hopefully I can help explain ...
    Conclusion:
    Java tries it's best to save on to use the least amount of memory and CPU cycles as possible.
    So Java tries to fit the values passed to the method to the smallest defined parameters of that method using the order provided in your first posting.


    Hi Pete
    Great explanation!
    I didn't suspect that there  was a memory management problem behind it
     
    Pete Letkeman
    Bartender
    Posts: 1868
    81
    Android IntelliJ IDE MySQL Database Chrome Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Daniele Barell wrote:Great explanation!
    I didn't suspect that there  was a memory management problem behind it


    I'm glad that you found it helpful.
    At some point in time memory and CPU cycles are often a consideration for most programs.
    Java tries to help you out, but you need to know the rules, which you posted in your first post.
    Knowing a reason (there could be more) behind rules can help you remember the rules.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!