CATEGORII DOCUMENTE |
Asp | Autocad | C | Dot net | Excel | Fox pro | Html | Java |
Linux | Mathcad | Photoshop | Php | Sql | Visual studio | Windows | Xml |
There's one special usage of an operator in Java: the + operator can be used to concatenate strings, as you've already seen. It seems a natural use of the + even though it doesn't fit with the traditional way that + is used. This capability seemed like a good idea in C++, so operator overloading was added to C++ to allow the C++ programmer to add meanings to almost any operator. Unfortunately, operator overloading combined with some of the other restrictions in C++ turns out to be a fairly complicated feature for programmers to design into their classes. Although operator overloading would have been much simpler to implement in Java than it was in C++, this feature was still considered too complex, so Java programmers cannot implement their own overloaded operators as C++ programmers can.
The use of the String + has some interesting behavior. If an expression begins with a String, then all operands that follow must be Strings:
int x = 0, y = 1, z = 2;
String sString = 'x, y, z ';
System.out.println(sString + x + y + z);
Here, the Java compiler will convert x, y, and z into their String representations instead of adding them together first. However, if you say:
System.out.println(x + sString);
earlier versions of Java will signal an error. (Later versions, however, will turn x into a String.) So if you're putting together a String (using an earlier version of Java) with addition, make sure the first element is a String (or a quoted sequence of characters, which the compiler recognizes as a String).
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 844
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved