LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 06-17-2004, 06:11 AM   #1
pycoucou
Member
 
Registered: Apr 2004
Location: Edinburgh
Posts: 78

Rep: Reputation: 15
[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
 
Old 06-17-2004, 09:39 PM   #2
moeminhtun
Member
 
Registered: Dec 2002
Location: Singapore
Distribution: Fedora Core 6
Posts: 647

Rep: Reputation: 30
What u wanna do exactly?
 
Old 06-18-2004, 03:30 AM   #3
pycoucou
Member
 
Registered: Apr 2004
Location: Edinburgh
Posts: 78

Original Poster
Rep: Reputation: 15
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
 
Old 06-18-2004, 08:20 AM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 31
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.
 
Old 06-18-2004, 08:33 AM   #5
pycoucou
Member
 
Registered: Apr 2004
Location: Edinburgh
Posts: 78

Original Poster
Rep: Reputation: 15
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
 
Old 06-19-2004, 10:39 PM   #6
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Ubuntu
Posts: 208

Rep: Reputation: 31
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 );
 
Old 06-20-2004, 11:31 AM   #7
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
Yes, just have more accurate method descriptions. Or put them in different classes.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
java newbie question method headers and if statements dflan98783 Programming 6 02-21-2007 11:18 PM
Method readLine() in Java...help KissDaFeetOfSean Programming 1 09-09-2005 12:41 AM
[JAVA - JLabel] executing a method based on contents of the variable linux_ub Programming 1 08-12-2004 09:50 AM
how do you reload the 'main' method in Java? ludeKing Programming 1 05-29-2004 10:22 PM
Java help (accessing array elemonts from another class or method) Tru_Messiah Programming 6 05-14-2004 09:20 AM


All times are GMT -5. The time now is 03:58 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration