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

Auto increment and decrement

java



+ Font mai mare | - Font mai mic



Auto increment and decrement

Java, like C, is full of shortcuts. Shortcuts can make code much easier to type, and either easier or harder to read.

Two of the nicer shortcuts are the increment and decrement operators (often referred to as the auto-increment and auto-decrement operators). The decrement operator is -- and means "decrease by one unit." The increment operator is ++ and means "increase by one unit." If A is an int, for example, the expression ++A is equivalent to (A = A + 1). Increment and decrement operators produce the value of the variable as a result.



There are two versions of each type of operator, often called the prefix and postfix versions. Pre-increment means the ++ operator appears before the variable or expression, and post-increment means the ++ operator appears after the variable or expression. Similarly, pre-decrement means the ‑‑ operator appears before the variable or expression, and post-decrement means the ‑‑ operator appears after the variable or expression. For pre-increment and pre-decrement, (i.e., ++A or ‑‑A), the operation is performed and the value is produced. For post-increment and post-decrement (i.e. A++ or A‑‑), the value is produced, then the operation is performed. As an example:

//: AutoInc.java

// Demonstrates the ++ and -- operators

public class AutoInc

static void prt(String s)

} ///:~

The output for this program is:

i : 1

++i : 2

i++ : 2

i : 3

--i : 2

i-- : 2

i : 1

You can see that for the prefix form you get the value after the operation has been performed, but with the postfix form you get the value before the operation is performed. These are the only operators (other than those involving assignment) that have side effects. (That is, they change the operand rather than using just its value.)

The increment operator is one explanation for the name C++, implying "one step beyond C." In an early Java speech, Bill Joy (one of the creators), said that "Java=C++-- C plus plus minus minus), suggesting that Java is C++ with the unnecessary hard parts removed and therefore a much simpler language. As you progress in this book you'll see that many parts are simpler, and yet Java isn't that much easier than C++.



Politica de confidentialitate | Termeni si conditii de utilizare



DISTRIBUIE DOCUMENTUL

Comentarii


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