Ch2. Java Programming
Ch2. Java Programming
Identifiers
Identifiers
Are tokens that represent name of variables, methods, classes, etc.
Examples of identifiers are: Hello, main, System, println.
2
Identifiers (cont…)
Naming convention
Identifiers must begin with a letter (A to Z or a to z), currency character
($), underscore character (_)
Identifiers can have any combination of characters after the first
character.
A keyword cannot be used as an identifier like class, public, void
Examples of legal identifier: age, $salary, _count
Examples of illegal identifier: 123abc, -salary
3
Identifiers (cont…)
Coding Guidelines
1. For names of classes, capitalize the first letter of the class name.
- ThisIsAnExampleOfClassName
2. For names of methods and variables, the first letter of the word should
start with a small letter.
- thisIsAnExampleOfMethodName
4
Identifiers (cont…)
Coding Guidelines
3. In case of multi-word identifiers, use capital letters to indicate the start of
the word except the first word.
- charArray, fileNumber, ClassName
4. Avoid using underscores at the start such as _read or _write
5
Keywords
Java Keywords
Keywords are predefined identifiers reserved by Java for a specific
purpose.
You cannot use keywords as names for your variables, classes, methods
…etc
6
Keywords (cont…)
7
Java Program
Components
Literals
Java Literals
Literals are tokens that do not change or are constant.
The different types of literals in Java are:
- Integer Literals
- Floating-Point Literals
- Boolean Literals
- Character Literals
- String Literals 9
Literals (cont…)
10
Literals (cont…)
12
Literals (cont…)
13
Literals (cont…)
14
Literals (cont…)
15
Literals (cont…)
16
Literals (cont…)
17
Primitive Data Types
18
Primitive Data Types (cont...)
Logical-boolean
A boolean data type represents two states: true and false
An example is:
boolean result = true;
The example shown above, declares a variable named result as boolean
type and assigns it a value of true.
19
Primitive Data Types (cont...)
Textual-char
A character data type (char), represents a single Unicode character
It must have its literal enclosed in single qoutes (‘ ‘);
For example,
To represent special characters like ‘ (single qoutes) or “ (double qoutes),
use the escape character \. For example,
‘\’’ //for single qoutes
‘\”’ //for double qoutes
20
Primitive Data Types (cont...)
Textual-char
String is a class and not a data type.
String represents a data type that contains multiple characters.
It has its literal enclosed in double qoutes (“ ”)
For example:
String message = “Hello World”;
21
Primitive Data Types (cont...)
22
Primitive Data Types (cont...)
23
Primitive Data Types (cont...)
24
Primitive Data Types (cont...)
25
Primitive Data Types (cont...)
26
Primitive Data Types (cont...)
27
Variables
Variable
A variable is an item of data used to store the state of objects.
A variable has a:
- data type
The data type indicates the type of value that the variable can hold
- name
The variable name must follow rules of identifiers
28
Variables (cont...)
29
Variables (cont...)
1 public class VariableSamples {
2 public static void main( String[] args ){
3 //declare a data type with variable name
4 // result and boolean data type
5 boolean result;
Coding Guidelines
1. It always good to initialize you variables as you declare
them.
2. Use descriptive name for your variables. Like for example, if
you want to have a variable that contains a grade for a
student, name it as, grade and not just some random letters
you choose.
31
Variables (cont...)
Coding Guidelines
3. Declare one variable per line of code. For example, the
variable declarations,
double exam = 0;
double quiz = 10;
double grade = 0;
Is preferred over the declaration,
double exam = 0, quiz = 10, grade = 0;
32
Outputting Variable
33
Outputting Variable (cont...)
1 public class OutputVariable {
2 public static void main( String[] args ){
3 int value = 10;
4 char x;
5 x = ‘A’;
6 System.out.println( value );
7 System.out.println( “The value of x=“ + x );
8 }
9 }
The program will output the following text on screen:
10
The value of x=A 34
Outputting Variable (cont...)
System.out.println() vs System.out.print()
System.out.println()
- Appends a newline at the end of the data output
System.out.print()
- Does not append newline at the end of the data output
35
Outputting Variable (cont...)
System.out.print()
System.out.print(“Hello”);
System.out.print(“World!”);
Output:
HelloWorld!
36
Outputting Variable (cont...)
System.out.print()
System.out.println(“Hello”);
System.out.println(“World!”);
Output:
Hello
World!
37
Operators
Arithmetic Operator
39
Operators (cont...)
Arithmetic Operator
1. public class ArithmeticDemo {
2. public static void main(String[] args){
3. //a few numbers
4. int i = 37;
5. int j = 42;
6. double x = 27.475;
7. double y = 7.22;
8. System.out.println("Variable values...");
9. System.out.println(" i = " + i);
10. System.out.println(" j = " + j);
11. System.out.println(" x = " + x);
12. System.out.println(" y = " + y); 40
Operators (cont...)
Arithmetic Operator
13. System.out.println("Adding...");
14. System.out.println(" i + j = " + (i + j));
15. System.out.println(" x + y = " + (x + y));
16. //subtracting numbers
17. System.out.println("Subtracting...");
18. System.out.println(" i - j = " + (i – j));
19. System.out.println(" x - y = " + (x – y));
20. //multiplying numbers
21. System.out.println("Multiplying...");
22. System.out.println(" i * j = " + (i * j));
23. System.out.println(" x * y = " + (x * y));
41
Operators (cont...)
Arithmetic Operator
24. //dividing numbers
25. System.out.println("Dividing...");
26. System.out.println(" i / j = " + (i / j));
27. System.out.println(" x / y = " + (x / y));
28. //computing the remainder
29. System.out.println("Computing the remainder...");
30. System.out.println(" i % j = " + (i % j));
31. System.out.println(" x % y = " + (x % y));
42
Operators (cont...)
Arithmetic Operator
32. //mixing types
33. System.out.println("Mixing types...");
34. System.out.println(" j + y = " + (j + y));
35. System.out.println(" i * x = " + (i * x));
36. }
37.}
43
Operators (cont...)
Arithmetic Operator
Variable values... Dividing...
i = 37 i / j = 0
j = 42 x / y = 3.8054
x = 27.475
Computing the remainder...
y = 7.22
i % j = 37
Adding...
x % y = 5.815
i + j = 79
Mixing types...
x + y = 34.695
j + y = 49.22
Subtracting...
i * x = 1016.58
i - j = -5
x - y = 20.255
Multiplying...
i * j = 1554
x * y = 198.37 44
Operators (cont...)
Arithmetic Operator
Note:
When an integer and a floating-point number are used as
operands to a single arithmetic operation, the result is a
floating point. The integer is implicitly converted to a floating-
point number before the operation takes place.
45
Operators (cont...)
47
Operators (cont...)
48
Operators (cont...)
49
Operators (cont...)
Relational Operators
Relational operators compare two values and determines the
relationship between those values.
The output of evaluation are the boolean values true or false
51
Operators (cont...)
Relational Operators
52
Operators (cont...)
Relational Operators
1. public class RelationalDemo{
2. public static void main(String[] args){
3. //a few numbers
4. int i = 37;
5. int j = 42;
6. int k = 42;
7. System.out.println("Variable values...");
8. System.out.println(" i = " +i);
9. System.out.println(" j = " +j);
10. System.out.println(" k = " +k);
53
Operators (cont...)
Relational Operators
11. //greater than
12. System.out.println("Greater than...");
13. System.out.println(" i > j = "+(i>j));//false
14. System.out.println(" j > i = "+(j>i));//true
15. System.out.println(" k > j = "+(k>j));//false
16. //greater than or equal to
17. System.out.println("Greater than or equal to...");
18. System.out.println(" i >= j = "+(i>=j));//false
19. System.out.println(" j >= i = "+(j>=i));//true
20. System.out.println(" k >= j = "+(k>=j));//true
54
Operators (cont...)
Relational Operators
21. //less than
22. System.out.println("Less than...");
23. System.out.println(" i < j = "+(i<j));//true
24. System.out.println(" j < i = "+(j<i));//false
25. System.out.println(" k < j = "+(k<j));//false
26. //less than or equal to
27. System.out.println("Less than or equal to...");
28. System.out.println(" i <= j = "+(i<=j));//true
29. System.out.println(" j <= i = "+(j<=i));//false
30. System.out.println(" k <= j = "+(k<=j));//true
55
Operators (cont...)
Relational Operators
31. //equal to
32. System.out.println("Equal to...");
33. System.out.println(" i == j = " + (i==j));//false
34. System.out.println(" k == j = " + (k==j));//true
35. //not equal to
36. System.out.println("Not equal to...");
37. System.out.println(" i != j = " + (i!=j));//true
38. System.out.println(" k != j = " + (k!=j));//false
39. }
40. }
56
Operators (cont...)
Logical Operators
Logical operators have one or two boolean operands that yield a
boolean result
There are six logical operators:
- && (logical AND) - ^ (boolean logical exclusive OR)
- & (boolean logical AND) - ! (logical NOT)
- || (logical OR)
- | (boolean logical inclusive OR)
58
Operators (cont...)
Logical Operators
The basic operation for a logical operation is
x1 op x2
where,
x1, x2 –can be Boolean expressions, variables or constants
op – is either &&, &, ||, | or ^ operator
The truth tables that will be shown next, summarize the result of each
operation for a possible combinations of x1 and x2.
59
Operators (cont...)
60
Operators (cont...)
62
Operators (cont...)
1 public class TestAND {
2 public static void main( String[] args ){
3 int i = 0;
4 int j = 10;
5 boolean test= false;
6 //demonstrate &&
7 test = (i > 10) && (j++ > 9);
8 System.out.println(i);
9 System.out.println(j);
10 System.out.println(test);
11 //demonstrate &
12 test = (i > 10) & (j++ > 9);
13 System.out.println(i);
14 System.out.println(j);
15 System.out.println(test);
16 } 63
17 }
Operators (cont...)
65
Operators (cont...)
66
Operators (cont...)
1 public class TestOR {
2 public static void main( String[] args ){
3 int i = 0;
4 int j = 10;
5 boolean test= false;
6 //demonstrate ||
7 test = (i < 10) || (j++ > 9);
8 System.out.println(i);
9 System.out.println(j);
10 System.out.println(test);
11 //demonstrate |
12 test = (i < 10) | (j++ > 9);
13 System.out.println(i);
14 System.out.println(j);
15 System.out.println(test);
16 }
67
17 }
Operators (cont...)
69
Operators (cont...)
70
Operators (cont...)
71
Operators (cont...)
72
Operators (cont...)
73
Operators (cont...)
74
Operators (cont...)
! (logical NOT)
The logical NOT takes in one argument, wherein that argument
can be an expression, variable or constant
Here is the truth table for !,
75
Operators (cont...)
Conditional Operator ( ?: )
The conditional operator ?:
- Is a ternary operator
This means that it takes in three arguments that together form a
conditional expression
Conditional Operator ( ?: )
The conditional operator ?:
- Result
if exp1 is true, exp2 is the value returned
if it is false, then exp3 is returned
78
Operators (cont...)
Conditional Operator ( ?: )
80
Operators (cont...)
Operator Precedence
81
Operators (cont...)
Operator Precedence
For example, multiplication have higher precedence than
addition. In the expression 2+3*4, the multiplication is done
before the addition, producing a result of 14.
So if we want to prioritize addition over multiplication we can
use ( ) parenthesis because parenthesis have higher precedence
than multiplication. In the expression (2+3)*4, the addition is
done before the addition, producing a result of 20.
82
Operators (cont...)
Operator Precedence
Given a complicated expression,
6%2*5+4/2+88-10
we can rewrite the expression and place some parenthesis
base on operator precedence.
((6%2)*5)+(4/2)+88-10
83
Operators (cont...)
Coding Guidelines
To avoid confusion in evaluating mathematical operations, keep
your expressions simple and use parenthesis.
84