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

A compendium of operators

java



+ Font mai mare | - Font mai mic



A compendium of operators

The following example shows which primitive data types can be used with particular operators. Basically, it is the same example repeated over and over, but using different primitive data types. The file will compile without error because the lines that would cause errors are commented out with a //!.

//: AllOps.java



// Tests all the operators on all the

// primitive data types to show which

// ones are accepted by the Java compiler.

class AllOps

void boolTest(boolean x, boolean y)

void charTest(char x, char y)

void byteTest(byte x, byte y)

void shortTest(short x, short y)

void intTest(int x, int y)

void longTest(long x, long y)

void floatTest(float x, float y)

void doubleTest(double x, double y)

} ///:~

Note that boolean is quite limited. You can assign to it the values true and false, and you can test it for truth or falsehood, but you cannot add booleans or perform any other type of operation on them.

In char, byte, and short you can see the effect of promotion with the arithmetic operators. Each arithmetic operation on any of those types results in an int result, which must be explicitly cast back to the original type (a narrowing conversion that might lose information) to assign back to that type. With int values, however, you do not need to cast, because everything is already an int. Don't be lulled into thinking everything is safe, though. If you multiply two ints that are big enough, you'll overflow the result. The following example demonstrates this:

//: Overflow.java

// Surprise! Java lets you overflow.

public class Overflow

static void prt(String s)

} ///:~

The output of this is:

big = 2147483647

bigger = -4

and you get no errors or warnings from the compiler, and no exceptions at run-time. Java is good, but it's not that good.

Compound assignments do not require casts for char, byte, or short, even though they are performing promotions that have the same results as the direct arithmetic operations. On the other hand, the lack of the cast certainly simplifies the code.

You can see that, with the exception of boolean, any primitive type can be cast to any other primitive type. Again, you must be aware of the effect of a narrowing conversion when casting to a smaller type, otherwise you might unknowingly lose information during the cast.



Politica de confidentialitate | Termeni si conditii de utilizare



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 541
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