Movatterモバイル変換


[0]ホーム

URL:


Documentation

The Java™ Tutorials
Language Basics
Variables
Primitive Data Types
Arrays
Summary of Variables
Questions and Exercises
Operators
Assignment, Arithmetic, and Unary Operators
Equality, Relational, and Conditional Operators
Bitwise and Bit Shift Operators
Summary of Operators
Questions and Exercises
Expressions, Statements, and Blocks
Questions and Exercises
Control Flow Statements
The if-then and if-then-else Statements
The switch Statement
The while and do-while Statements
The for Statement
Branching Statements
Summary of Control Flow Statements
Questions and Exercises
Trail: Learning the Java Language
Lesson: Language Basics
Home Page >Learning the Java Language >Language Basics
« Previous • Trail • Next »

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.

Questions and Exercises: Operators

Questions

  1. Consider the following code snippet.
    arrayOfInts[j] > arrayOfInts[j+1]
    Which operators does the code contain?
  2. Consider the following code snippet.
    int i = 10;int n = i++%5;
    1. What are the values ofi andn after the code is executed?
    2. What are the final values ofi andn if instead of using the postfix increment operator (i++), you use the prefix version (++i))?
  3. To invert the value of aboolean, which operator would you use?
  4. Which operator is used to compare two values,= or== ?
  5. Explain the following code sample:result = someCondition ? value1 : value2;

Exercises

  1. Change the following program to use compound assignments:
    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);     }}
  2. In the following program, explain why the value "6" is printed twice in a row:
    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"    }}

Check your answers

« PreviousTrailNext »

About Oracle |Contact Us |Legal Notices |Terms of Use |Your Privacy Rights

Copyright © 1995, 2024 Oracle and/or its affiliates. All rights reserved.

Previous page: Summary of Operators
Next page: Expressions, Statements, and Blocks

[8]ページ先頭

©2009-2025 Movatter.jp