The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.
SeeDev.java for updated tutorials taking advantage of the latest releases.
SeeJava Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
SeeJDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.
arrayOfInts[j] > arrayOfInts[j+1]
>
,+
int i = 10;int n = i++%5;
i
andn
after the code is executed?i
is 11, andn
is 0.i
andn
if instead of using the postfix increment operator (i++
), you use the prefix version (++i)
)?i
is 11, andn
is 1.boolean
, which operator would you use?=
or==
?==
operator is used for comparison, and=
is used for assignment.result = someCondition ? value1 : value2;
someCondition
istrue
, assign the value ofvalue1
toresult
. Otherwise, assign the value ofvalue2
toresult
."class ArithmeticDemo { public static void main (String[] args){ int result = 1 + 2; // result is now 3 System.out.println(result); result = result - 1; // result is now 2 System.out.println(result); result = result * 2; // result is now 4 System.out.println(result); result = result / 2; // result is now 2 System.out.println(result); result = result + 8; // result is now 10 result = result % 7; // result is now 3 System.out.println(result); }}
Here is one solution:
class ArithmeticDemo { public static void main (String[] args){ int result = 3; System.out.println(result); result -= 1; // result is now 2 System.out.println(result); result *= 2; // result is now 4 System.out.println(result); result /= 2; // result is now 2 System.out.println(result); result += 8; // result is now 10 result %= 7; // result is now 3 System.out.println(result); }}
class PrePostDemo { public static void main(String[] args){ int i = 3; i++; System.out.println(i); // "4" ++i; System.out.println(i); // "5" System.out.println(++i); // "6" System.out.println(i++); // "6" System.out.println(i); // "7" }}
System.out.println(++i);
evaluates to 6, because the prefix version of++
evaluates to the incremented value. The next line,System.out.println(i++);
evaluates to the current value (6), then increments by one. So "7" doesn't get printed until the next line.About Oracle |Contact Us |Legal Notices |Terms of Use |Your Privacy Rights
Copyright © 1995, 2024 Oracle and/or its affiliates. All rights reserved.