Scrigroup - Documente si articole

     

HomeDocumenteUploadResurseAlte limbi doc
AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

Overloading with primitives

java



+ Font mai mare | - Font mai mic



Overloading with primitives

Primitives can be automatically promoted from a smaller type to a larger one and this can be slightly confusing in combination with overloading. The following example demonstrates what happens when a primitive is handed to an overloaded method:



//: PrimitiveOverloading.java

// Promotion of primitives and overloading

public class PrimitiveOverloading

void f1(char x)

void f1(byte x)

void f1(short x)

void f1(int x)

void f1(long x)

void f1(float x)

void f1(double x)

void f2(byte x)

void f2(short x)

void f2(int x)

void f2(long x)

void f2(float x)

void f2(double x)

void f3(short x)

void f3(int x)

void f3(long x)

void f3(float x)

void f3(double x)

void f4(int x)

void f4(long x)

void f4(float x)

void f4(double x)

void f5(long x)

void f5(float x)

void f5(double x)

void f6(float x)

void f6(double x)

void f7(double x)

void testConstVal()

void testChar()

void testByte()

void testShort()

void testInt()

void testLong()

void testFloat()

void testDouble()

public static void main(String[] args)

If you view the output of this program, you'll see that the constant value 5 is treated as an int, so if an overloaded method is available that takes an int it is used. In all other cases, if you have a data type that is smaller than the argument in the method, that data type is promoted. char produces a slightly different effect, since if it doesn't find an exact char match, it is promoted to int.

What happens if your argument is bigger than the argument expected by the overloaded method? A modification of the above program gives the answer:

//: Demotion.java

// Demotion of primitives and overloading

public class Demotion

void f1(char x)

void f1(byte x)

void f1(short x)

void f1(int x)

void f1(long x)

void f1(float x)

void f1(double x)

void f2(char x)

void f2(byte x)

void f2(short x)

void f2(int x)

void f2(long x)

void f2(float x)

void f3(char x)

void f3(byte x)

void f3(short x)

void f3(int x)

void f3(long x)

void f4(char x)

void f4(byte x)

void f4(short x)

void f4(int x)

void f5(char x)

void f5(byte x)

void f5(short x)

void f6(char x)

void f6(byte x)

void f7(char x)

void testDouble()

public static void main(String[] args)

Here, the methods take narrower primitive values. If your argument is wider then you must cast to the necessary type using the type name in parentheses. If you don't do this, the compiler will issue an error message.

You should be aware that this is a narrowing conversion, which means you might lose information during the cast. This is why the compiler forces you to do it - to flag the narrowing conversion.



Politica de confidentialitate | Termeni si conditii de utilizare



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 492
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved