• 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

Method Overloading with default param values

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Why doesnt this method overloading work ?


I was trying to assign default values
(like public static String foo(byte a = 5, byte b = 10))
only to realize tha Java doesnt support default assignments.

I get


JDK 6

Thanks
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure that that's exactly the error you are getting? Because that would be strange. I compiled this small test program:

And got the following errors:

Note that the compiler does not complain that it can't find foo(byte, byte) - it does complain that it can't find variants of this function that take an int parameter. That is because the literal numeric values are of type int, not byte. They are not automatically converted to byte.

You can either (1) cast the literals to byte, or (2) use int instead of byte in the method arguments.
 
Anjanesh Lekshminarayanan
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah..Thanks.

Yes - you're right, I got

I wanted to use byte to save memory since I know the values are going to be from 1-50 or so.

So I got to typecast all literals manually ?
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anjanesh Lekshminarayanan:
So I got to typecast all literals manually ?


If you explicitly want these to be interpreted as byte and not as int, yes.

There's one special case for byte in the Java language. When you declare a byte and you assign a literal value to it, you don't have to cast it.
 
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

Originally posted by Anjanesh Lekshminarayanan:

I wanted to use byte to save memory



Using byte-sized method parameters will not have any effect on memory usage. Using bytes as member variable may.
 
She's out of the country right now, toppling an unauthorized dictatorship. Please leave a message with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic