0% found this document useful (0 votes)
27 views17 pages

Java Programming Language Overview

Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995, known for its robustness and security. It supports various application types including standalone, web, enterprise, and mobile applications, and utilizes different variable types and data types. Java also features control flow statements, operators, arrays, and exception handling to facilitate programming.

Uploaded by

shimelisdamesa8
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)
27 views17 pages

Java Programming Language Overview

Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995, known for its robustness and security. It supports various application types including standalone, web, enterprise, and mobile applications, and utilizes different variable types and data types. Java also features control flow statements, operators, arrays, and exception handling to facilitate programming.

Uploaded by

shimelisdamesa8
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

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.
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) 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

1
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

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.

Example to understand the types of variables in java

public class A {

static int m=100;//static variable

void method() {

2
int n=90;//local variable }

public static void main(String args[]) }

int data=50;//instance variable } }//end of class

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.

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.

Java Unary Operator Example: ++ and --

public class OperatorExample{

public static void main(String args[]){

int x=10;

3
[Link](x++);//10 (11)

[Link](++x);//12

[Link](x--);//12 (11)

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

}}

Output
10
12
12
10

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

}}

Java Ternary Operator Example

public class OperatorExample{

public static void main(String args[]){

4
int a=2;

int b=5;

int min=(a<b)?a:b;

[Link](min);

}}

Output 2

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)

b-=4;//b=b-4 (b=20-4)

[Link](a);

[Link](b);

}}

Output 14,16

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.

5
Syntax to Declare an Array in Java

• dataType[] arr; (or)

• dataType []arr; (or)

• dataType arr[];

Example of Java Array

• class Testarray{

• public static void main(String args[]){

• int a[]=new int[5];//declaration and instantiation

• a[0]=10;//initialization

• a[1]=20;

• 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]);

• }}

Output 10,20,70,40,50

Multidimensional Array in Java

6
• 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

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

7
3. Jump statements

o break statement

o continue statement

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

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

• }

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) {

8
• statement 1; //executes when condition is true

• }

• else{

• statement 2; //executes when condition is false

• }

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. Syntax:

if(condition1){

//code to be executed if condition1 is true

}else if(condition2){

//code to be executed if condition2 is true

else if(condition3){

//code to be executed if condition3 is true

….…..

else{

//code to be executed if all the conditions are false

Consider the following example:

• public class Student {

9
• 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);

• } } }

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. Syntax:

switch(expression) {

case value1:

//code to be executed;

break; //optional

case value2:

//code to be executed;

break; //optional

default:

10
Code to be executed if all cases are not matched.

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

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

• }

The flow chart for the for-loop is given below:

The flow chart for the for-loop is given below:

11
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); } }

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 }

The flow chart for the while loop is given in the following image.

e flow chart for the while loop is given in the following image.

Consider the following example.

• public class Calculation {

• public static void main(String[] args) {

12
• int i = 2;

• [Link]("Printing the list of first 10 even numbers \n");

• while(i<=10) {

• [Link](i);

• i = i + 2;

• }}}

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);

The flow chart of the do-while loop is given in the following image

do

true condition

false

terminate

13
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); } } } }

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;

• } } } }

Java continue statement

public class ContinueExample {

• public static void main(String[] args) {

• for(int i = 0; i<= 5; i++) {

• if(i==2) {

• continue;

14
• }

• [Link](i); } } }

Annotate

Exceptions

An exception is an object that describes an unusual or erroneous situation. Exceptions


are thrown by a program, and may be caught and handled by another part of the
program.

n Errors:

n Errors are fairly rare and usually fatal. They are caused by bugs in the
Java VM, or by the program running out of memory or other resources.

n Errors are usually not handled by programs.

n Exceptions:

n Exceptions can be caused by bugs in the program or improper data


supplied to the program

n Exceptions should be handled, or caught

n An exception is either checked or unchecked

n Checked exceptions must be caught or declared as thrown.

n You are not required to handle unchecked exceptions.

n An error event that disrupts the program flow and may cause a program to fail.

n Some examples:

n Performing illegal arithmetic

n Illegal arguments to methods

15
n Accessing an out-of-bounds array element

n Hardware failures

n Writing to a read-only file

Exception Class Hierarchy

All exceptions are instances of classes that are subclasses of Exception

n ArithmeticException: You tried an illegal type of arithmetic operation, such as


dividing an integer by zero.

n ArrayIndexOutOfBoundsException: when array index is past the last array


index

n ClassNotFoundException: a necessary class couldn’t be found.

n FileNotFoundException: when you try to open a file for reading/writing but the
file does not exist.

n IllegalArgumentException: you passed an incorrect argument to a method.

n IOException: A method that performs I/O encountered an unrecoverable I/O


error.

16
n InputMismatchException: the console input doesn’t match the data type
expected by a method of the Scanner class.

n NegativeArraySizeException: array created with a negative size.

n NullPointerException: occurs when your program creates an object reference,


but has not yet created an object and assigned it to the reference. Attempting
to use such a null reference causes a NullPointerException to be thrown.

n : invalid conversion of a string to a numeric format.

n StringIndexOutOfBounds: attempt to index outside the bounds of a string.

Keywords for Java Exceptions

n throws: - Describes the exceptions which can be raised by a method.

n throw: - Raises an exception to the first available handler in the call stack,
unwinding the stack along the way.

n try: - Marks the start of a block associated with a set of exception handlers.

n catch: - If the block enclosed by the try generates an exception of this type,
control moves here; watch out for implicit subsumption.

n finally: - Always called when the try block concludes, and after any necessary
catch handler is complete.

17

You might also like