chapter 1 - Overview of Java Programming
chapter 1 - Overview of Java Programming
CHAPTER 1
OVERVIEW OF JAVA PROGRAMMING
2
Variable
6
Access Modifier
Types of Variables
8
It cannot be local.
You can create a single copy of the static variable and share it among
all the instances of the class.
Memory allocation for static variables happens only once when the class
is loaded in the memory.
Example to understand the types of variables in java
10
1. public class A
2. {
3. static int m=100; //static variable
4. void method()
5. {
6. int n=90; //local variable
7. }
8. public static void main(String args[])
9. {
10. int data=50; //instance variable
11. }
12. } //end of class
Java Variable Example: Add Two Numbers
11
Disadvantage
Size Limit: We can store only the fixed size of elements in the
array.
It doesn't grow its size at runtime.
To solve this problem, collection framework is used in Java
which grows automatically.
Types of Arrays
15
• Multidimensional Array
We can declare, instantiate and initialize the java array together by:
int a[]={33,3,4,5};//declaration, instantiation and initialization
Let's see the simple example to print this array.
//Java Program to illustrate the use of declaration, instantiation
//and initialization of Java array in a single line
1. class Testarray1{
2. public static void main(String args[]){
3. int a[]={33,3,4,5}; //declaration, instantiation and initialization printing array
4. for(int i=0;i<a.length;i++) //length is the property of array
5. System.out.println(a[i]); Output:
6. } 33
3
7. }
4
5
For-each Loop for Java Array
18
4. dataType []arrayRefVar[];
Example to initialize Multidimensional Array in Java
22
5. int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
6. //printing 2D array
7. for(int i=0;i<3;i++){
8. for(int j=0;j<3;j++){
9. System.out.print(arr[i][j]+" ");
10. } Output:
11. System.out.println(); 1 2 3
12. } 2 4 5
13. } 4 4 5
14. }
Java Control Statements | Control Flow in Java
24
1. if statements
2. switch statement
2. Loop statements
1. do while loop
2. while loop
3. for loop
4. for-each loop
3. Jump statements
1. break statement
2. continue statement
Decision-Making statements:
26
1) If Statement:
In Java, the "if" statement is used to evaluate a condition.
1. Simple if statement
2. if-else statement
3. if-else-if ladder
4. Nested-if statement
Decision Making statements | If Statement:
27
1) Simple if statement:
It is the most basic statement among all control flow statements
in Java.
It evaluates a Boolean expression and enables the program to
enter a block of code if the expression evaluates to true.
Syntax:
1. if(condition) {
3. }
Example Student.java
28
2) if-else statement
The if-else statement is an extension to the if-statement, which
uses another block of code, i.e., else block.
The else block is executed if the condition of the if-block is
evaluated as false.
Syntax:
1. if(condition) {
3. }
4. else{
6. }
Example Student.java
30
1. public class Student {
2. public static void main(String[] args) {
3. int x = 10;
4. int y = 12;
5. if(x+y < 10) {
6. System.out.println("x + y is less than 10");
7. }
8. else {
9. System.out.println("x + y is greater than 20");
10. }
11. } Output:
x + y is greater than 20
12. }
Decision Making statements | If Statement:
31
3) if-else-if ladder:
The if-else-if statement contains the if-statement followed by multiple
else-if statements.
We can also define an else statement at the end of the chain.
Syntax:
if(condition 1) {
1. statement 1; //executes when condition 1 is true
2. }
3. else if(condition 2) {
5. }
6. else {
8. }
Example Student.java
32
1. public class Student {
2. public static void main(String[] args) {
3. String city = “Robe";
4. if(city == “Goba") {
5. System.out.println(“City is Goba");
6. }else if (city == “Addis Ababa") {
7. System.out.println(“City is Addis Ababa ");
8. }else if(city == "Adama") {
9. System.out.println(“City is Adama");
10. }else {
11. System.out.println(city);
12. } Output:
13. } Robe
14. }
Decision Making statements | If Statement:
33
4. Nested if-statement
In nested if-statements, the if statement can contain a if or if-
else statement inside another if or else-if statement.
Syntax:
1. if(condition 1) {
Switch Statement:
In Java, Switch statements are similar to if-else-if statements.
4. int sum = 0;
6. sum = sum + j;
7. }
9. }
10. } Output:
The sum of first 10 natural numbers is 55
Loop Statements
42
7. System.out.println(name);Java
C
8. }
C++
9. } Python
10. } JavaScript
Loop Statements
44
Syntax:
while(condition){
1. //looping statements
2. }
Example Calculation .java
46
Output:
6. while(i<=10) {
Printing the list of first 10 even numbers
7. System.out.println(i); 0
8. i = i + 2;
2
4
9. } 6
10. }
8
10
11. }
Loop Statements
47
5. System.out.println(i);
6. if(i==6) {
Output:
7. break; 0
8. }
1
2
9. }
3
10. } 4
11. }
5
6
Jump statements
51
• IOException,
• SQLException,
• RemoteException, etc.
Keyword Description
try The "try" keyword is used to specify a block where we should place an
exception code. It means we can't use try block alone. The try block
must be followed by either catch or finally.
catch The "catch" block is used to handle the exception. It must be preceded
by try block which means we can't use catch block alone. It can be
followed by finally block later.
finally The "finally" block is used to execute the necessary code of the
program. It is executed whether an exception is handled or not.
throw The "throw" keyword is used to throw an exception.
throws The "throws" keyword is used to declare exceptions. It specifies that
there may occur an exception in the method. It doesn't throw an
exception. It is always used with method signature.
Java Exception Handling Example
JavaExceptionExample.java
59
12. catch(ArrayIndexOutOfBoundsException e)
13. {
11. System.out.println("ArrayIndexOutOfBounds Exceptio
n occurs");
12. }
13. catch(Exception e)
14. {
15. System.out.println("Parent Exception occurs");
16. }
17. System.out.println(“Rest of the code");
18. }
19. } Output:
Arithmetic Exception occurs
Rest of the code
Java finally block
65
Case 2: When an exception occur but not handled by the catch block
Case 3: When an exception occurs and is handled by the catch block
Example TestFinallyBlock.java
68
Let's see the below example where the Java program does not throw any exception,
and the finally block is executed after the try block.
1. class TestFinallyBlock {
2. public static void main(String args[]){
3. try{
4. //below code do not throw any exception
5. int data=25/5;
6. System.out.println(data);
7. }
8. //catch won't be executed
9. catch(NullPointerException e){
10. System.out.println(e);
11. }
Example TestFinallyBlock.java
69
15. }
17. }
18. }
Output
Java throw Exception
70