2016-03-31 23 views
19

Sto usando il codice:Perché `System.out.println (null);` restituisce "Il metodo println (char []) è ambiguo per il tipo errore PrintStream"?

System.out.println(null); 

sta mostrando l'errore:

The method println(char[]) is ambiguous for the type PrintStream 

Perché non null rappresenta Object?

+2

'System.out.println ((Object) null);' –

+1

Possibile duplicato di [impossibile passare null ad execute(); metodo di AsyncTask in Android 4.0] (http://stackoverflow.com/questions/10679739/unable-to-pass-null-to-execute-method-of-asynctask-in-android-4-0) – Raedwald

+1

Vedere anche http : //stackoverflow.com/questions/13033037/how-is-an-overloaded-method-chosen-when-a-parameter-is-the-literal-null-value – Raedwald

risposta

25

Ci sono 3 metodi println in PrintStream che accettano un tipo di riferimento - println(char x[]), println(String x), println(Object x).

Quando si passa null, tutti e 3 sono applicabili. Le regole di overloading del metodo preferiscono il metodo con i tipi di argomenti più specifici, quindi println(Object x) non è selezionato.

Poi il compilatore non può scegliere fra i primi due - println(char x[]) & println(String x) - poiché String non è più specifico rispetto char[] e viceversa.

Se si desidera selezionare un metodo specifico, eseguire il cast del valore nullo nel tipo richiesto.

Ad esempio:

System.out.println((String)null); 
+1

Conosco e capisco questo concetto. Ma per completezza della risposta, il JLS da qualche parte specifica perché, ad esempio, 'char []' e 'String' sono ugualmente specifici? Dopo tutto, 'char' è una primitiva e' String' è un oggetto. – Magnilex

+11

@Magnilex char è una primitiva, ma char [] è un oggetto. char [] non è una sottoclasse di String e String non è una sottoclasse di char []. Pertanto nessuno dei due è più specifico. – Eran

+3

@KannanThangadurai nota che puoi scrivere 'char x []' come 'char [] x' - molti (ad es. [Guida di stile di Google] (https: // google.github.io/styleguide/javaguide.html#s4.8.3.2-array-declarations)) preferiscono quest'ultima forma perché il tipo di 'x' è' char [] ', non' char'. Mantenere il tipo insieme rende più facile la lettura. –

7

Se si chiama System.println(null) ci sono diversi metodi di candidati (con char [], String e Object argomento) e il compilatore non può decidere quale prendere.

Correzione
Aggiungere un cast esplicito a null.

System.out.println((Object)null); 

Oppure utilizzare null object pattern.


Why does't null represent Object?

Da JLS 4.1 The Kinds of Types and Values

There is also a special null type, the type of the expression null, which has no name.
Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type.
The null reference is the only possible value of an expression of null type. The null reference can always be cast to any reference type.
The null reference can always be assigned or cast to any reference type

In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type

Così il tipo di null è nonObject, anche se può essere convertito in (lettura come assegnato a) un Object.

Problemi correlati