Duvida sobre sobrecarga

6 respostas
marciosouzajunior

Pessoal, estou estudando uns simulados aqui estou com uma dúvida aqui:

public class Yikes {

    public static void go(Long n) {
        System.out.println("Long ");
    }

    public static void go(Short n) {
        System.out.println("Short ");
    }

    public static void go(int n) {
        System.out.println("int ");
    }

    public static void main(String[] args) {
        short y = 6;
        long z = 7;
        go(y);
        go(z);
    }
}
Porque a saída desse código é: int Long Sendo que passei um short? A chamada ao método sobrescrito nao depende totalmente do tipo de argumento passado?

6 Respostas

E

Short != short

marciosouzajunior

Mas…Short == int?

D

Siga as regras:

  1. Ampliação
  2. Autoboxing
  3. Varargs

você passou um short, ele ampliou, acho o int e executou o método
você passou um long, ele não achou ampliação, mas achou o boxing… Long…

marciosouzajunior

Ok pessoal, obrigado pelas dicas.

Flavio_machine

Valeu me ajudou tb. To estudando essa praga tb, que exitem 0,0000000000000000000000000001% de vc utilizar;

Milton_Quirino

Pessoal,

Peguei esse exercicio e complementei ele, quem quiser fazer para fixar =)

public class Yikes {  

	public static void go(Object o) {  
	        System.out.println("Objeto ");  
	}  
		
	 public static void go(Long n) {  
		        System.out.println("Long ");  
	 }  
	  
	 public static void go(long n) {  
	        System.out.println("long ");  
	 }  
	  
	 public static void go(Short n) {  
	        System.out.println("Short ");  
	} 
	 
    public static void go(short n) {  
        System.out.println("short ");  
    }  
  
    public static void go(Integer n) {  
        System.out.println("Integer ");  
    }
    
    public static void go(int n) {  
        System.out.println("int ");  
    }  
  
    public static void go(Float n) {  
        System.out.println("Float ");  
    } 

    public static void go(float n) {  
        System.out.println("float ");  
    } 
    
    public static void main(String[] args) {  

        short y = 6;  
        Short a = 2;
        long z = 7; 
        Long b = new Long("1");
        String k = "5";
        int t = Integer.parseInt(k);
        Integer x = 8;
        int c = 9;
        float h = 2.5f;
        Float i = 2.6f;
        Double d = 6d;
        byte g = 127;
        int o = 1;
        
        System.out.print(+(o++)+" - "); 
        go(a);
        System.out.print(+(o++)+" - ");
        go( y ); 
        System.out.print(+(o++)+" - "); 
        go(a);
        System.out.print(+(o++)+" - ");
        go(z);
        System.out.print(+(o++)+" - ");
        go(b);
        System.out.print(+(o++)+" - ");
        go(k);
        System.out.print(+(o++)+" - ");
        go(t);
        System.out.print(+(o++)+" - ");
        go(x);
        System.out.print(+(o++)+" - ");
        go(c);
        System.out.print(+(o++)+" - ");
        go(h);
        System.out.print(+(o++)+" - ");
        go(i);
        System.out.print(+(o++)+" - ");
        go(d);
        System.out.print(+(o++)+" - ");
        go(g);
    }  
}
Criado 15 de outubro de 2010
Ultima resposta 14 de mar. de 2012
Respostas 6
Participantes 5