[ Pobierz całość w formacie PDF ]
.Of course, they all ultimately end up using System.out.println( ).To generate numbers, the program first creates a Random object.Because no arguments arepassed during creation, Java uses the current time as a seed for the random numberChapter 3: Controlling Program Flow 97 generator.The program generates a number of different types of random numbers with theRandom object simply by calling different methods: nextInt( ), nextLong( ), nextFloat( ) ornextDouble( ).The modulus operator, when used with the result of the random number generator, limitsthe result to an upper bound of the operand minus one (99 in this case).Unary minus and plus operatorsThe unary minus (-) and unary plus (+) are the same operators as binary minus and plus.The compiler figures out which use is intended by the way you write the expression.Forinstance, the statementx = -a;has an obvious meaning.The compiler is able to figure out:x = a * -b;but the reader might get confused, so it is more clear to say:x = a * (-b);The unary minus produces the negative of the value.Unary plus provides symmetry withunary minus, although it doesn t do much.Auto increment and decrementJava, like C, is full of shortcuts.Shortcuts can make code much easier to type, and eithereasier or harder to read.Two of the nicer shortcuts are the increment and decrement operators (often referred to asthe 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 Ais an int, for example, the expression ++A is equivalent to (A = A + 1).Increment anddecrement 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-incrementand 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 theoperation is performed.As an example://: AutoInc.java// Demonstrates the ++ and -- operatorspublic class AutoInc {public static void main(String[] args) {int i = 1;prt("i : " + i);prt("++i : " + ++i); // Pre-incrementprt("i++ : " + i++); // Post-incrementprt("i : " + i);98 Thinking in Java www.BruceEckel.com prt("--i : " + --i); // Pre-decrementprt("i-- : " + i--); // Post-decrementprt("i : " + i);}static void prt(String s) {System.out.println(s);}} ///:~The output for this program is:i : 1++i : 2i++ : 2i : 3--i : 2i-- : 2i : 1You can see that for the prefix form you get the value after the operation has beenperformed, 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 plusminus minus), suggesting that Java is C++ with the unnecessary hard parts removed andtherefore a much simpler language.As you progress in this book you ll see that many partsare simpler, and yet Java isn t that much easier than C++.Relational operatorsRelational operators generate a boolean result.They evaluate the relationship between thevalues of the operands.A relational expression produces true if the relationship is true, andfalse if the relationship is untrue.The relational operators are less than (), less than or equal to (=), equivalent (==) and notequivalent (!=).Equivalence and nonequivalence works with all built-in data types, but theother comparisons won t work with type boolean.Testing object equivalenceThe relational operators == and != also work with all objects, but their meaning oftenconfuses the first-time Java programmer.Here s an example://: Equivalence.javapublic class Equivalence {public static void main(String[] args) {Integer n1 = new Integer(47);Integer n2 = new Integer(47);System.out.println(n1 == n2);System.out.println(n1 != n2);}} ///:~Chapter 3: Controlling Program Flow 99 The expression System.out.println(n1 == n2) will print out the result of the booleancomparison within it.Surely the output should be true and then false, since both Integerobjects are the same.But while the contents of the objects are the same, the handles are notthe same and the operators == and != compare object handles.So the output is actuallyfalse and then true.Naturally, this surprises people at first.What if you want to compare the actual contents of an object for equivalence? You must usethe special method equals( ) that exists for all objects (not primitives, which work fine with== and !=).Here s how it s used://: EqualsMethod.javapublic class EqualsMethod {public static void main(String[] args) {Integer n1 = new Integer(47);Integer n2 = new Integer(47);System.out.println(n1.equals(n2));}} ///:~The result will be true, as you would expect.Ah, but it s not as simple as that.If you createyour own class, like this://: EqualsMethod2.javaclass Value {int i;}public class EqualsMethod2 {public static void main(String[] args) {Value v1 = new Value();Value v2 = new Value();v1.i = v2.i = 100;System.out.println(v1 [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • szamanka888.keep.pl