Professional Documents
Culture Documents
Wolaita Sodo University School of Informatics: Department of Computer Science
Wolaita Sodo University School of Informatics: Department of Computer Science
School of Informatics
Department of Computer Science
Arrays
It is widely used for developing Java applications in laptops, data centers,
game consoles, scientific supercomputers, cell phones, etc.
History of Java Programming Language
4
Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the
year 1995. James Gosling, Canadian computer scientist (born 19 May 1955) is a best
known as the founder and lead designer behind the Java programming language
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.
Platform: Any hardware or software environment in which a program runs, is known as
a platform. Since Java has a runtime environment (JRE) and API, it is called a platform
Later, in 2009, Oracle Corporation acquired Sun Microsystems and took ownership of
three key Sun software assets: Java, MySQL, and Solaris.
James Gosling developed the Java platform at Sun Microsystems, and the Oracle
Corporation later acquired it.
Java Programming
5
3) Enterprise Application
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.
Java Programming
8
It is an enterprise platform that is mainly used to develop web and enterprise applications. It
is built on top of the Java SE platform. It includes topics like Servlet, JSP, Web Services,
EJB, JPA, etc.
4) JavaFX
It is used to develop rich internet applications. It uses a lightweight user interface API.
Java Programming
10
The Java Programming Language is a high-level language. Its Syntax is similar to C and
C++, but it removes many of the complex , confusing features of C and C++.
The Java Programming Language includes the feature of automatic storage management by
using a garbage collector.
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
The Java programming language source code is compiled into the bytecode instruction set
which can be run inside the Java Virtual Machine (JVM) process.
Java Programming …
11
JVM (Java Virtual Machine) is an abstract machine that enables your computer to run a
Java program.
Java Application
In the Java Language, all of the source code is written in plain text files with the .java
extension. The java source code files are then compiled into .class extension files by the
command javac
A .class file contains bytecode which is a platform independent instruction set. The java
command then runs the application
Java application translation from source code to byte code procedure.
12 Java Programming …
Oracle has two products that implement Java Platform Standard Edition, Java SE
development Kit and Java SE Runtime Environment
JRE (Java Runtime Environment) is a software package that provides Java class
libraries, Java Virtual Machine (JVM), and other components that are required to run
Java applications.
JDK (Java Development Kit) is a
software development kit required to
develop applications in Java. When you
download JDK, JRE is also downloaded
with it.
In addition to JRE, JDK also contains a
number of development tools (compilers,
JavaDoc, Java Debugger, etc).
Java Programming …
13 Java Application Example
Step 1: create a sample source code file Test.java
The Java Virtual Machine gives runtime support to the application and can make the
application independent from different hardware systems.
16 Data types and variables
Data types specify the different sizes and values that can be stored in the variable. There are
two types of data types in Java:
Java has primitive and non-primitive data types. Primitive data types are a special group of
data types that do not use the keyword new when initialized.
There are eight primitive data types that are used to store data during a program's operation.
17 Data types and variables…
Java creates them as automatic variables that are not references, which are stored in
memory with the name of the variable.
To display Variables
The println() method is often used to display variables. To combine both text and a
variable, use the + character:
All elements must be of the same type BUT there is no restriction on which type this is.
Length of the array is set when the array is declared, so the size is fixed.
arrays can be used to hold a collection of int values; or a collection of char values;
BUT they cannot be used to hold a mixture of int and char values.
Declaring arrays
24
Need to state
the size of the array ;
the type of each individual array element .
The array type and size are then put together with a special new operator that allocates
memory.
For example
Declaring arrays
25
Example 2
This is the only instance in which all the elements of an array can be assigned
explicitly by listing out the elements in a single assignment statement.
Accessing array elements
Array can be used like any other variable of the given type in Java.
The assignment operator can be used to enter a value.
You must specify which element to place the value in.
For example: Allowing the user of the program to enter the value of the first temperature:
Java provides a rich set of operators to manipulate variables. We can divide all the Java
operators into the following groups:
Arithmetic operators: are used in mathematical expressions in the same way that they are
used in algebra. These are addition, Subtraction, multiplication, division, modulus,
increment and decrement operators.
Operators in Java
29 The following program is a simple example which demonstrates the arithmetic operators.
Operators in Java
30
output
Operators in Java
31 Relational operators
Java has six relational operators used to test primitive or literal numerical values.
Logic Operators
Java has three logic operators used to combine
Boolean expressions into complex tests.
Operators in Java
32
If expression is false, statement is not executed and the program continues
at rest_of_program
Decision and Repetition statement
35 If-Else Statement If-Else Statement Example
if (expression) {
statement1;
}
else{
statement2;
}
next_statement;
if-else chains can be sometimes be rewritten as a “switch” statement. switches are usually
simpler and faster
Decision and Repetition statement
37 switch Statement switch Statement Example
The switch statement enables you to test
several cases generated by a given expression.
switch (expression) {
case value1:
statement1;
case value2:
statement2;
default:
default_statement;
}
Every statement after the true case is executed
The expression must evaluate to a char, byte,
short or int, Character, Byte, Short, Integer,
String, enum but not float, or double or long
The break statement tells the computer to exit the switch statement
38 Decision and Repetition/Itration statement
Iteration is the form of program control that allows us to repeat a section of code.
This form of control is often also referred to as repetition.
The programming structure that is used to control this repetition is often called a loop.
There are three types of loops in Java:
for loop;
while loop;
do…while loop.
The ‘for’ loop
If we wish to repeat a section of code a fixed number of times we would use Java's for
loop.
The for loop is usually used in conjunction with a counter to keep track of how many
times we have been through the loop so far:
The ‘for’ loop
39 for (init_expr; loop_condition; increment_expr) {
statement;
}
The control of the for loop appear in parentheses and is made up of three parts:
1. The first part, the init_expression, sets the initial conditions for the loop and is executed
before the loop starts.
2. Loop executes so long as the loop_condition is true and exits otherwise.
3. The third part of the control information, the increment_expr, is usually used to
increment the loop counter. This is executed at the end of each loop iteration.
Example
output
Decision and Repetition statement
40
while loop: while loop statement in Java programming language repeatedly executes a
target statement as long as a given condition is true.
while Statement syntax Here, statement(s) may be a single statement or a block of
statements.
The condition may be any expression, and true is any non
zero value. When executing, if the boolean_expression result
is true, then the actions inside the loop will be executed. This
will continue as long as the expression result is true.
When the condition becomes false, program control passes
to the line immediately following the loop
Decision and Repetition statement
41 while Statement Example
output
Decision and Repetition statement
42
do...while: A do...while loop is similar to a while loop, except that a do...while loop is
guaranteed to execute at least one time.
do…while Statement syntax Notice that the Boolean expression appears at the end of
the loop, so the statements in the loop execute once before
the Boolean is tested.
If the Boolean expression is true, the control jumps back up
to do statement, and the statements in the loop execute
again.
This process repeats until the Boolean expression is false.
43 Decision and Repetition statement
do…while Statement Example
output
44 Exception handling overview
What is an Exception? is a problem that arises during the execution of a program that disrupts
the program flow and may cause a program to fail or terminates abnormally, which is not
recommended, therefore, these exceptions are to be handled
Some examples are:.
Accessing an out-of-bounds array element
Hardware failures
When an exception is dealt with, we say the exception was handled or caught
Unchecked Exceptions: is an exception that occurs at the time of execution. These are also called
as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of
an API. Runtime exceptions are numerous and ignored at the time of compilation.
Checked Exceptions: an exception that occurs at the compile time, these are also called as compile
time exceptions. These exceptions cannot simply be ignored at the time of compilation, the
programmer should take care of (handle) these exceptions.
try {
// code that might throw exception
}
catch ([Type of Exception] e) {
// what to do if exception is thrown
}
50 Example
The following is an array declared with 2 elements. Then the code tries to access the 3rd element of the array which
throws an exception.