0% found this document useful (0 votes)
5 views39 pages

Java Programming Overview and Features

The document provides an overview of Java programming, detailing its history, types of applications, and key features. It covers fundamental concepts such as variables, data types, operators, arrays, and control statements, including loops and conditional statements. Additionally, it includes examples to illustrate the usage of these concepts in Java programming.

Uploaded by

nig bird
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)
5 views39 pages

Java Programming Overview and Features

The document provides an overview of Java programming, detailing its history, types of applications, and key features. It covers fundamental concepts such as variables, data types, operators, arrays, and control statements, including loops and conditional statements. Additionally, it includes examples to illustrate the usage of these concepts in Java programming.

Uploaded by

nig bird
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

Bule Hora University

College of Informatics
Department of Computer Science

CoSc3053: Advanced Programming

Chapter 1:
Overview of Java Programming

Nigussu B.
[Link]@[Link]

1
Introduction
• What is Java?
• Java is a programming language and a platform.
Java is a high level, robust, object-oriented and
secure programming language.
• Java was developed by Sun Microsystems (which is
now the subsidiary of Oracle) in the year 1995.
• James Gosling is known as the father of Java.
• Before Java, its name was Oak. Since Oak was
already a registered company, so James Gosling and
his team changed the name from Oak to Java.
2
Types of Java Applications
• There are mainly 4 types of applications that can be
created using Java programming:
1) Standalone Application
• Standalone applications are also known as desktop
applications or window-based applications. These are
traditional software that we need to install on every
machine. Examples of standalone application are
Media player, antivirus, etc. AWT and Swing are used in
Java for creating standalone applications.
2) Web Application
• An application that runs on the server side and creates
a dynamic page is called a web application. Currently,
Servlet, JSP, etc. technologies are used for creating web
applications in Java.
3
Con’d
3) Enterprise Application
• An application that is distributed in nature, such as
banking applications, etc. is called an enterprise
application. It has advantages like high-level security,
load balancing, and clustering.
• In Java, EJB is used for creating enterprise
applications.
4) Mobile Application
• An application which is created for mobile devices is
called a mobile application. Currently, Android and
Java ME are used for creating mobile applications.

4
Features of Java

5
Fig 1.A list of the most important features of the Java language
Java Variables
• A variable is a container which holds the value
while the Java program is executed. A variable is
assigned with a data type.
• In other words, it is a name of the memory
location. It is a combination of "vary + able"
which means its value can be changed.
• int data=5;//Here data is variable
• There are three types of variables in Java:
• local variable
• instance variable
• static variable
6
Cont’d
1) Local Variable
• A variable declared inside the body of the method is called local
variable. You can use this variable only within that method and the
other methods in the class aren't even aware that the variable
exists.
• A local variable cannot be defined with "static" keyword.
2) Instance Variable
• A variable declared inside the class but outside the body of the
method, is called an instance variable. It is not declared as static.
• It is called an instance variable because its value is instance-specific
and is not shared among instances.
3) Static variable
• A variable that is declared as static is called a static variable. 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.
7
Cont’d
• Example to understand the types of variables in java
public class A
{
static int m=100;//static variable
void method()
{
int n=90;//local variable
}
public static void main(String args[])
{
int data=50;//instance variable
}
}//end of class

8
Data Types in Java
• Data types specify the different sizes and values that can
be stored in the variable. There are two types of data
types in Java:
1. Primitive data types: The primitive data types include
boolean, char, byte, short, int, long, float and double.
2. Non-primitive data types: The non-primitive data types
include Classes, Interfaces, and Arrays, etc.

9
Operators in Java
• There are many types of operators in Java which are
given below:
• Unary Operator,
• Arithmetic Operator,
• Shift Operator,
• Relational Operator,
• Bitwise Operator,
• Logical Operator,
• Ternary Operator and
• Assignment Operator.
10
Cont’d
• Java Unary Operator Example: ++ and --
public class OperatorExample{
public static void main(String args[]){
int x=10;
[Link](x++);//10 (11) Output
10
12
[Link](++x);//12 12
[Link](x--);//12 (11) 10

[Link](--x);//10
}}

11
Cont’d
• Java AND Operator Example: Logical && and Bitwise &
• The logical && operator doesn't check the second condition if the first
condition is false. It checks the second condition only if the first one is
true.
• The bitwise & operator always checks both conditions whether first
condition is true or false.

public class OperatorExample{


public static void main(String args[]){
int a=10;
int b=5;
int c=20;
[Link](a<b&&a<c);//false && true = false
[Link](a<b&a<c);//false & true = false
}}

12
Cont’d
• Java Ternary Operator Example
public class OperatorExample{
public static void main(String args[]){
int a=2;
int b=5;
int min=(a<b)?a:b;
[Link](min);
}} Output
2

13
Cont’d
• Java Assignment Operator Example
public class OperatorExample{
public static void main(String args[]){
int a=10;
int b=20;
a+=4;//a=a+4 (a=10+4) Output
b-=4;//b=b-4 (b=20-4) 14
16
[Link](a);
[Link](b);
}}

14
Java Arrays
• Java array is an object which contains elements of a similar data type.
• Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd
element is stored on 1st index and so on.

15
Cont’d
• Syntax to Declare an Array in Java
• dataType[] arr; (or)
• dataType []arr; (or)
• dataType arr[];
• Example of Java Array
Output
• class Testarray{
10
• public static void main(String args[]){ 20
• int a[]=new int[5];//declaration and instantiation 70
• a[0]=10;//initialization 40
• a[1]=20; 50
• a[2]=70;
• a[3]=40;
• a[4]=50;
• //traversing array
• for(int i=0;i<[Link];i++)//length is the property of array
• [Link](a[i]);
• }} 16
Multidimensional Array in Java
• In such case, data is stored in row and column based index (also
known as matrix form).
• Syntax to Declare Multidimensional Array in Java
• dataType[][] arrayRefVar; (or)
• dataType [][]arrayRefVar; (or)
• dataType arrayRefVar[][];
• Example to instantiate Multidimensional Array in Java
• int[][] arr=new int[3][3];//3 row and 3 column
Example to initialize Multidimensional Array in Java
arr[0][0]=1; arr[1][2]=6;
arr[0][1]=2; arr[2][0]=7;
arr[0][2]=3; arr[2][0]=7;
arr[1][0]=4; arr[2][1]=8;
arr[1][1]=5; arr[2][2]=9;

17
Cont’d
• Example of Multidimensional Java Array
• //Java Program to illustrate the use of multidimensional array
• class Testarray3{
• public static void main(String args[]){
• //declaring and initializing 2D array
• int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
• //printing 2D array
• for(int i=0;i<3;i++){
• for(int j=0;j<3;j++){
• [Link](arr[i][j]+" ");
• }
• [Link]();
• }
• }}

18
Java Control Statements
• Java provides statements that can be used to control the flow
of Java code. Such statements are called control flow
statements. It is one of the fundamental features of Java,
which provides a smooth flow of program.
• Java provides three types of control flow statements.
1. Decision Making statements
o if statements
o switch statement
2. Loop statements
o do while loop
o while loop
o for loop
o for-each loop
3. Jump statements
o break statement
o continue statement

19
If Statement
• In Java, the "if" statement is used to evaluate a condition. The
control of the program is diverted depending upon the specific
condition.
• The condition of the If statement gives a Boolean value, either
true or false. In Java, there are four types of if-statements given
below.
• Simple if statement
• if-else statement
• if-else-if statement
• Nested if-statement

20
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 of if statement is given below.
• if(condition) {
• statement 1; //executes when condition is true
• }

21
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:
• if(condition) {
• statement 1; //executes when condition is true
• }
• else{
• statement 2; //executes when condition is false
• }

22
if-else-if statement
• The if-else-if statement contains the if-statement followed by
multiple else-if statements.
• In other words, we can say that it is the chain of if-else statements
that create a decision tree where the program may enter in the
block of code where the condition is true. We can also define an
else statement at the end of the chain.

23
Cont’d
• Consider the following example.
• public class Student {
• public static void main(String[] args) {
• String city = “Dilla";
• if(city == “Bule Hora") {
• [Link]("city is Bule Hora");
• }else if (city == “Adola") {
• [Link]("city is Adola");
• }else if(city == “Bore") {
• [Link]("city is Bore");
• }else {
• [Link](city);
• }
• }
• }
24
Switch Statement
• In Java, Switch statements are similar to if-else-if statements.
• The switch statement contains multiple blocks of code called cases
and a single case is executed based on the variable which is being
switched.
• The switch statement is easier to use instead of if-else-if
statements. It also enhances the readability of the program

25
Cont’d
• Consider the following example to understand the flow of the switch statement.
• public class Student {
• public static void main(String[] args) {
• int num = 2;
• switch (num){
• case 0:
• [Link]("number is 0");
• break;
• case 1:
• [Link]("number is 1");
• break;
• default:
• [Link](num);
• }
• }
• } 26
Loop Statements
• In programming, sometimes we need to execute the block of code
repeatedly while some condition evaluates to true.

• However, loop statements are used to execute the set of instructions in a


repeated order. The execution of the set of instructions depends upon a
particular condition.

• In Java, we have three types of loops that execute similarly. However,


there are differences in their syntax and condition checking time.

✓ for loop

✓ while loop

✓ do-while loop
27
Java for loop
• In Java, for loop is similar to C and C++. It enables us to initialize the
loop variable, check the condition, and increment/decrement in a
single line of code. We use the for loop only when we exactly know
the number of times, we want to execute the block of code.

• Syntax
• for(initialization, condition, increment/decrement) {
• //block of statements
• }

28
Cont’d
• The flow chart for the for-loop is given below.

29
Cont’d
• Consider the following example to understand the proper functioning of the for
loop in java.
• public class Calculate {
• public static void main(String[] args) {
• int sum = 0;
• for(int j = 1; j<=10; j++) {
• sum = sum + j;
• }
• [Link]("The sum of first 10 natural numbers is " + sum);
• }
• }

Output:
The sum of first 10 natural numbers is 55

30
Java while loop
• The while loop is also used to iterate over the number of
statements multiple times. However,

• Unlike for loop, the initialization and increment/decrement doesn't


take place inside the loop statement in while loop.

• The syntax of the while loop is given below.

• while(condition){

• //looping statements

• }

31
Cont’d
• The flow chart for the while loop is given in the following image.

32
Cont’d
• Consider the following example.
• public class Calculation {
• public static void main(String[] args) {
• int i = 2;
• [Link]("Printing the list of first 10 even numbers \n");

• while(i<=10) { Output:
• [Link](i); Printing the list of first 10 even numbers
2
• i = i + 2; 4
6
• } 8
• } 10

• }

33
Java do-while loop
• The do-while loop checks the condition at the end of
the loop after executing the loop statements.

• When the number of iteration is not known and we


have to execute the loop at least once, we can use do-
while loop.
• The syntax of the do-while loop is given below.
• do
• {
• //statements
• } while (condition);
34
Cont’d
• The flow chart of the do-while loop is given in
the following image

35
Cont’d
• Example (do-while loop in Java)
• public class Calculation {
• public static void main(String[] args) {
• int i = 10;
• do {
• [Link](i);
• i = i + 2;
• }while(i<=10);
• }
• }

36
Java Nested for Loop
• If we have a for loop inside the another loop, it is known as
nested for loop. The inner loop executes completely whenever
outer loop executes.
• public class NestedForExample {
• public static void main(String[] args) {
• for(int i=1;i<=3;i++){
• for(int j=1;j<=3;j++){
• [Link](i+" "+j);
• }
• }
• }
• }

37
Java break statement
• public class BreakExample {
• public static void main(String[] args) {
• for(int i = 0; i<= 10; i++) {
• [Link](i);
• if(i==6) {
• break;
• }
• }
• }
• }

38
Java continue statement
• public class ContinueExample {
• public static void main(String[] args) {
• for(int i = 0; i<= 5; i++) {
• if(i==2) {
• continue;
• }
• [Link](i);
• }
• }
• } 39

You might also like