SlideShare a Scribd company logo
Lecture-Java ProgrammingTheWorldii6.pptx
Lecture
Outline
Increment/Decrement
Operators
Formatted Output
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
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
Increment and Decrement
Operators
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
Lecture-Java ProgrammingTheWorldii6.pptx
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
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
ASCII Table
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);
Escape Sequences for Special Characters
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
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.
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"
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.
Width and Precision in Format Specifier
Example
The cast operator lets you manually convert a value
Cast Operators

More Related Content

PPTX
C language
PPT
c-programming
PPTX
03 and 04 .Operators, Expressions, working with the console and conditional s...
PPTX
the refernce of programming C notes ppt.pptx
PPTX
Review of C programming language.pptx...
PPTX
Chapter1.pptx
PPTX
Fundamentals of Programming Constructs.pptx
PPSX
Esoft Metro Campus - Certificate in c / c++ programming
C language
c-programming
03 and 04 .Operators, Expressions, working with the console and conditional s...
the refernce of programming C notes ppt.pptx
Review of C programming language.pptx...
Chapter1.pptx
Fundamentals of Programming Constructs.pptx
Esoft Metro Campus - Certificate in c / c++ programming

Similar to Lecture-Java ProgrammingTheWorldii6.pptx (20)

PPT
slidenotesece246jun2012-140803084954-phpapp01 (1).ppt
PPTX
PPTX
Java Foundations: Data Types and Type Conversion
PPTX
C programming language
PPTX
Java Programming Tutorials Basic to Advanced 2
PPT
lecture2 (1).ppt variable s and operators
PDF
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
PDF
Module_1_Introduction-to-Problem-Solving.pdf
PPTX
basic C PROGRAMMING for first years .pptx
PPT
02basics
PDF
Pointers are one of the core components of the C programming language.
PDF
C programing Tutorial
PPT
Input And Output
PPTX
Ch3 Formatted Input/Output
PDF
Embedded_C_1711824726engéiiiring_with_the_best.pdf
PDF
introduction to python programming course 2
PPTX
Introduction to C Programming
PPTX
Lecture 3
PPTX
02. Primitive Data Types and Variables
slidenotesece246jun2012-140803084954-phpapp01 (1).ppt
Java Foundations: Data Types and Type Conversion
C programming language
Java Programming Tutorials Basic to Advanced 2
lecture2 (1).ppt variable s and operators
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
Module_1_Introduction-to-Problem-Solving.pdf
basic C PROGRAMMING for first years .pptx
02basics
Pointers are one of the core components of the C programming language.
C programing Tutorial
Input And Output
Ch3 Formatted Input/Output
Embedded_C_1711824726engéiiiring_with_the_best.pdf
introduction to python programming course 2
Introduction to C Programming
Lecture 3
02. Primitive Data Types and Variables
Ad

Recently uploaded (20)

PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
KodekX | Application Modernization Development
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Programs and apps: productivity, graphics, security and other tools
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
MIND Revenue Release Quarter 2 2025 Press Release
Agricultural_Statistics_at_a_Glance_2022_0.pdf
The AUB Centre for AI in Media Proposal.docx
Building Integrated photovoltaic BIPV_UPV.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
“AI and Expert System Decision Support & Business Intelligence Systems”
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
The Rise and Fall of 3GPP – Time for a Sabbatical?
Chapter 3 Spatial Domain Image Processing.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
KodekX | Application Modernization Development
Mobile App Security Testing_ A Comprehensive Guide.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Ad

Lecture-Java ProgrammingTheWorldii6.pptx

  • 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);
  • 12. Escape Sequences for Special Characters
  • 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.
  • 17. Width and Precision in Format Specifier
  • 19. The cast operator lets you manually convert a value Cast Operators