shorthand operators. shorthand operator: a shorthand operator is a shorter way to express something...
TRANSCRIPT
![Page 1: Shorthand operators. Shorthand operator: A shorthand operator is a shorter way to express something that is already available in the Java programming](https://reader036.vdocuments.us/reader036/viewer/2022082411/56649dd25503460f94ac907b/html5/thumbnails/1.jpg)
Shorthand operators
![Page 2: Shorthand operators. Shorthand operator: A shorthand operator is a shorter way to express something that is already available in the Java programming](https://reader036.vdocuments.us/reader036/viewer/2022082411/56649dd25503460f94ac907b/html5/thumbnails/2.jpg)
Shorthand operators
• Shorthand operator:
• A shorthand operator is a shorter way to express something that is already available in the Java programming language
• Shorthand operations do not add any feature to the Java programming language
(So it's not a big deal).
![Page 3: Shorthand operators. Shorthand operator: A shorthand operator is a shorter way to express something that is already available in the Java programming](https://reader036.vdocuments.us/reader036/viewer/2022082411/56649dd25503460f94ac907b/html5/thumbnails/3.jpg)
Shorthand operators +=, -=, *=, /= and *=
• A frequent construct is the following:
Java has a shorthand operator for these kinds of assignment statements
x is a variable in the program
x = x + value ; // Add value to the variable x x = x - value ; // Subtract value to the variable x x = x * value ; // Increase the variable x by value times and so on...
![Page 4: Shorthand operators. Shorthand operator: A shorthand operator is a shorter way to express something that is already available in the Java programming](https://reader036.vdocuments.us/reader036/viewer/2022082411/56649dd25503460f94ac907b/html5/thumbnails/4.jpg)
Shorthand operators +=, -=, *=, /= and *= (cont.)
• Operator assignment short hands:
Operator symbol
Name of the operator
Example Equivalent construct
+= Addition assignment
x += 4; x = x + 4;
-= Subtraction assignment
x -= 4; x = x - 4;
*= Multiplication assignment
x *= 4; x = x * 4;
/= Division assignment
x /= 4; x = x / 4;
%= Remainder assignment
x %= 4; x = x % 4;
![Page 5: Shorthand operators. Shorthand operator: A shorthand operator is a shorter way to express something that is already available in the Java programming](https://reader036.vdocuments.us/reader036/viewer/2022082411/56649dd25503460f94ac907b/html5/thumbnails/5.jpg)
Shorthand operators +=, -=, *=, /= and *= (cont.)
• Exercise: what is printed by the following program
public class Shorthand1 { public static void main(String[] args) { int x; x = 7; x += 4; System.out.println(x); x = 7; x -= 4; System.out.println(x);
![Page 6: Shorthand operators. Shorthand operator: A shorthand operator is a shorter way to express something that is already available in the Java programming](https://reader036.vdocuments.us/reader036/viewer/2022082411/56649dd25503460f94ac907b/html5/thumbnails/6.jpg)
Shorthand operators +=, -=, *=, /= and *= (cont.)
x = 7; x *= 4; System.out.println(x); x = 7; x /= 4; System.out.println(x); x = 7; x %= 4; System.out.println(x); } }
![Page 7: Shorthand operators. Shorthand operator: A shorthand operator is a shorter way to express something that is already available in the Java programming](https://reader036.vdocuments.us/reader036/viewer/2022082411/56649dd25503460f94ac907b/html5/thumbnails/7.jpg)
Shorthand operators +=, -=, *=, /= and *= (cont.)
• Example Program: (Demo above code) – Prog file:
http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/Shorthand1.java
• How to run the program:
• Right click on link and save in a scratch directory
• To compile: javac Shorthand1.java
• To run: java Shorthand1 (You will see the answers on the terminal)
![Page 8: Shorthand operators. Shorthand operator: A shorthand operator is a shorter way to express something that is already available in the Java programming](https://reader036.vdocuments.us/reader036/viewer/2022082411/56649dd25503460f94ac907b/html5/thumbnails/8.jpg)
Increment and decrement shorthand operators
• Two very commonly used assignment statements are:
• Java has shorthand operators to increment and decrement a variable by 1 (one).
1. x = x + 1;
and
2. x = x - 1;
x is a variable in the program
![Page 9: Shorthand operators. Shorthand operator: A shorthand operator is a shorter way to express something that is already available in the Java programming](https://reader036.vdocuments.us/reader036/viewer/2022082411/56649dd25503460f94ac907b/html5/thumbnails/9.jpg)
Increment and decrement shorthand operators (cont.)
• Increment and decrement operators:
Operator symbol
Name of the operator
Example Equivalent construct
++ Increment x++; x = x + 1;
-- Decrement x--; x = x - 1;
![Page 10: Shorthand operators. Shorthand operator: A shorthand operator is a shorter way to express something that is already available in the Java programming](https://reader036.vdocuments.us/reader036/viewer/2022082411/56649dd25503460f94ac907b/html5/thumbnails/10.jpg)
Increment and decrement shorthand operators (cont.)
• Exercise: what is printed by the following program
public class Shorthand2 { public static void main(String[] args) { int x; x = 7; x++; System.out.println(x); x = 7; x--; System.out.println(x); } }
![Page 11: Shorthand operators. Shorthand operator: A shorthand operator is a shorter way to express something that is already available in the Java programming](https://reader036.vdocuments.us/reader036/viewer/2022082411/56649dd25503460f94ac907b/html5/thumbnails/11.jpg)
Increment and decrement shorthand operators (cont.)
• Example Program: (Demo above code) – Prog file:
http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/Shorthand2.java
• How to run the program:
• Right click on link and save in a scratch directory
• To compile: javac Shorthand2.java
• To run: java Shorthand2 (You will see the answers on the terminal)