Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-17-2004, 06:11 AM
|
#1
|
Member
Registered: Apr 2004
Location: Edinburgh
Posts: 78
Rep:
|
[java] Method signature precision
I used to believe that the java method signature was basically
- return type
- name of the method
- parameters
But the following error:
Matrix.java:851: convertArray(float[][]) is already defined in Matrix
public static double[][] convertArray (float[][] array) {
indicates that finally, it's rather name of the method + parameters!
Have you a tip to overgo this issue to use convertArray without thinking of the type of data I'm handling...
Cheers,
PY
|
|
|
06-17-2004, 09:39 PM
|
#2
|
Member
Registered: Dec 2002
Location: Singapore
Distribution: Fedora Core 6
Posts: 647
Rep:
|
What u wanna do exactly?
|
|
|
06-18-2004, 03:30 AM
|
#3
|
Member
Registered: Apr 2004
Location: Edinburgh
Posts: 78
Original Poster
Rep:
|
Well... is there a way to create a convertArray method that typecast each elements (ie float[] -> double[], float[] ->int[] +rounding for this one, etc...)
I want to be able to define:
public double[][] convertArray(float[][] array)
public int[][] convertArray(float[][] array)
But it seems, according to javac, it's not possible... Has someone an idea on that? ie is it possible or I'll have to use a method with another name (float2int)...
Cheers,
PY
|
|
|
06-18-2004, 08:20 AM
|
#4
|
Member
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895
Rep:
|
Yes, that's right. You cannot overload functions to have the same name/parameters, but different return types. For instance, if you took your 2 methods there, and simply tried to call them w/o storing the return types like so:
convertArray(someFloatArray);
How would the compiler know which overload to call?
Anyway, a solution to your problem could be just to give the functions different names, such as convertArrayToDouble and convertArrayToInt.
|
|
|
06-18-2004, 08:33 AM
|
#5
|
Member
Registered: Apr 2004
Location: Edinburgh
Posts: 78
Original Poster
Rep:
|
Quote:
Originally posted by deiussum
Yes, that's right. You cannot overload functions to have the same name/parameters, but different return types. For instance, if you took your 2 methods there, and simply tried to call them w/o storing the return types like so:
convertArray(someFloatArray);
How would the compiler know which overload to call?
Anyway, a solution to your problem could be just to give the functions different names, such as convertArrayToDouble and convertArrayToInt.
|
I agree with you... I believed that i could overload this function as soon as the return type was different, it seems that not... Thanks for this precision. I'm disappointed that my first guess was wrong 
|
|
|
06-19-2004, 10:39 PM
|
#6
|
Member
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint
Posts: 216
Rep:
|
Overloading resolution only cares about the method name and argument types, not the return type. There is no getting around "...thinking of the data type [you're] handling." Perhaps you could have methods like:
public double[][] convertArrayToDouble( float[][] f );
public double[][] convertArrayToDouble( int[][] i );
public int[][] convertArrayToInt( float[][] f );
|
|
|
06-20-2004, 11:31 AM
|
#7
|
Member
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288
Rep:
|
Yes, just have more accurate method descriptions. Or put them in different classes.
|
|
|
All times are GMT -5. The time now is 10:35 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|