Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The compiler seems to be confusing two versions of an overloaded method. Why?

In my jsp file there is a line:

byte[] imageData = Base64.decodeBase64(request.getParameter("imageBase64"));

and eclipse complains:

The method decodeBase64(byte[]) in the type Base64 is not applicable for the arguments (String)"

It says that the method gets a String, but it expects a byte[]. But in the Base64 class there are two overloaded versions of decodeBase64; one with argument String, and one with argument byte[].

I fail to see why the compiler seems to think I am calling the byte[]version with an incorrect String argument, where it should have used the String version without any compiler error.

like image 813
Jan de Ruiter Avatar asked Nov 06 '12 13:11

Jan de Ruiter


People also ask

How does the compiler distinguish between function overloading?

The compiler distinguishes overloaded methods by their signatures—a combination of the method's name and the number, types and order of its parameters, but not its return type. If the compiler looked only at method names during compilation, the code in Fig.

Which type determine overload method which will be used at compile time?

Explanation: Overloading is determined at compile time. It is also called as compile time polymorphism.


1 Answers

Base64 class in package org.apache.commons.codec.binary has 2 decode methods

static byte[]   decodeBase64(byte[] base64Data) since beginning

static byte[]   decodeBase64(String base64String) since version 1.4.

I think you must be having jar prior to commons codec 1.4 in your classpath

Hope it helps.

like image 65
Sajan Chandran Avatar answered Nov 11 '22 01:11

Sajan Chandran