0% found this document useful (0 votes)
7 views16 pages

Lecture 4 (Chapter 2)

Uploaded by

Omar Hesham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views16 pages

Lecture 4 (Chapter 2)

Uploaded by

Omar Hesham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Programming 1

Lecture 4
Chapter 2:
Introduction to Java Programming

1
Increment and decrement:

2
Modify-and-assign:

3
Increment- Decrement
Pre increment - Post increment

post
pre

Pre decrement - Post decrement


Example:
Summary of Variables:

7
Trace:

8
Type casting:

9
Examples:

10
Example:
Example:

All integers

Casting ( change the data type)

casting
char data type

• char : A primitive data type representing single characters of


text (e.g., 'a', 'b', '@', ' ', etc.).

public static void main(String[] args) {


char a = 's';
System.out.println ("student" + a);
}

Output:

students
char vs. int
• Each char is mapped to an integer value internally
• Called an ASCII value

'A' is 65 'B' is 66 ' ' is 32


'a' is 97 'b' is 98 '*' is 42

• Mixing char and int causes automatic conversion to int.


'a' + 10 is 107, 'A' + 'A' is 130

• To convert an int into the equivalent char, type-cast it.


(char) ('a' + 2) is 'c'
Example
public static void main(String[] args)
{ Be Careful:
int x = 1; char x = 67; //correct since it
char letter1 = 'a'; takes the value as the ASCII
char letter2 = (char) (letter1 + 4); code 67

System.out.println(letter2); int y = 67;


char x = y; // incorrect since
x = 'a' + 3; here it takes it as integer y
System.out.println(x);
}

Output:
e
100
16

You might also like