3. Increment and
Decrement Operators
•The increment operator (++) and
decrement operator (– –) are for
incrementing and decrementing a
variable by 1. int i = 3, j = 3;
i++; // i becomes 4
J--; // j becomes 2
Example – PostIncrement /
Postdecrement
int i = 3, j = 3;
++i; // i becomes 4
--j; // j becomes 2
Example – PreIncrement /
Predecrement
4. int i = 10;
int newNum = 10 * i++; int newNum = 10 * i;
i = i + 1;
Same effect as
int i = 10;
int newNum = 10 * (++i); i = i + 1;
int newNum = 10 * i;
Same effect as
Increment and Decrement Operators
6. Output of the following Java program?
1. public class IncrementDecrementTest {
2. public static void main(String[] args) {
3. int i = 5;
4. int result = ++i + i-- + i++ + --i;
5.
6. System.out.println("Result: " + result);
7. System.out.println("Final value of i: " + i);
8. }
9. }
A) Result: 22, Final value of i: 5
B) Result: 21, Final value of i: 5
C) Result: 22, Final value of i: 6
D) Result: 20, Final value of i: 4
8. Character Data Type
• Computers use binary numbers internally
• Mapping a character to its binary
representation is called encoding
• How characters are encoded is defined by
an encoding scheme
• Java supports Unicode (16 bit character
encoding and 65,536 characters are
possible)
• A 16-bit Unicode takes two bytes
– Preceded by u, expressed in four
hexadecimal digits
– i.e. ranges from u0000 To uFFFF
9. ASCII Code for Commonly Used Characters
Characters Code Value in Decimal Unicode Value
'0' to '9' 48 to 57 u0030 to u0039
'A' to 'Z' 65 to 90 u0041 to u005A
'a' to 'z' 97 to 122 u0061 to u007A
• Unicode includes ASCII code with u0000 to u007F
corresponding to the 128 ASCII characters
11. Character Data Type
char letter = 'A'; (ASCII)
char numChar = '4'; (ASCII)
char letter = 'u0041'; (Unicode)
char numChar = 'u0034'; (Unicode)
Four hexadecimal digits.
NOTE: The increment and decrement operators can also be
used on char variables to get the next or preceding Unicode
character. For example, the following statements display
character b.
char ch = 'a';
System.out.println(++ch);
13. Casting between char and Numeric Types
int i = 'a'; // Same as int i = (int)'a';
char c = 97; // Same as char c = (char)97;
char ch = (char)65.25; // Decimal 65 is assigned to ch
System.out.println(ch); // ch is character A
• All numeric operators can be applied to char operands
14. Formatting Console Output
• Syntax
System.out.printf(format, item1, item2,..., itemk);
• Where format is a string that may consist of substrings and
format specifier.
• A format specifier specifies how an item should be displayed.
• An item may be a numeric value, character, boolean value, or a
string.
– Items must match the format specifier in order, in number, and in exact
type
• A simple format specifier consists of a percent sign (%) followed
by a conversion code e.g. (%d ).
You can use the System.out.printf method to display formatted output on the
console.
15. Frequently-Used Specifiers
Specifier Output Example
%b a boolean value true or false
%c a character 'a'
%d a decimal integer 200
%f a floating-point number 45.460000
%e a number in standard scientific notation 4.556000e+01
%s a string "Java is cool"
16. Formatting Console Output
int count = 5;
double amount = 45.56;
System.out.printf("count is %d and amount is %f", count, amount);
display count is 5 and amount is 45.560000
items
• %4.2f is a format specifier:
• % indicates format conversion.
• 4 represents the minimum width.
• .2f ensures that the output is rounded to 2 decimal places.
• The floating-point result 16.444674 is rounded to 16.44 to match the .2f format.