Advanced Programming Java-H-U1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

AMBO UNIVERSITY WOLISO CAMPUS

School of Technology and Informatics Department of Information Technology


Course Title: Advanced Programming in Java Course Code: ITec3058; ECTS: 5

Chapter One
Introduction to Java

Advanced Programming in Java (ITec3058),


BSc(IT) Third Year, Second Semester, 2014 E.C
By Jerusalem F.
Topics
2
❑ Introduction ❑ Keywords in Java
❑ Java devices ❑ Method in Java
❑ Java applications / Editions ❑ Function in Java
❑ Features of Java ❑ Operators in Java
❑ JDK, JRE & JVM ❑ Class in Java
❑ Java path ❑ Control statements in Java
❑ Variables in Java ❑ Object-oriented concepts in Java
❑ Data types in Java
Introduction to Java
3
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 in 1995. James Gosling is known as the father
of Java. Before Java, its name was Oak.
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.
E.g.,
class Simple{
public static void main(String args[]){
System.out.println(“Advanced Programming in Java”);
}
}
Devices run Java
4
✓ Desktop Applications such as acrobat reader, media player, antivirus, etc.
✓ Web Applications such as irctc.co.in, javatpoint.com, etc.
✓ Enterprise Applications such as banking applications.
✓ Mobile
✓ Embedded System
✓ Smart Card
✓ Robotics
✓ Games, etc.
Types of Java Applications
5
There are mainly 4 types of applications that can be created using Java programming:
1) Standalone Application
also known as desktop applications or window-based applications. These are
traditional software that we need to install on every machine.
e.g., Media player, antivirus, etc.
i.e., 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, Struts, Spring, Hibernate, JSF, etc. technologies
are used for creating web applications in Java.
6
3) Enterprise Application
An application that is distributed in nature, such as banking applications. 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.
Java Platforms / Editions
7
There are 4 platforms or editions of Java:
1) Java SE (Java Standard Edition)
It is a Java programming platform. It includes Java programming APIs such as java.lang,
java.io, java.net, java.util, java.sql, java.math etc. It includes core topics like OOPs, String,
Regex, Exception, Inner classes, Multithreading, I/O Stream, Networking, AWT, Swing,
Reflection, Collection, etc.
2) Java EE (Java Enterprise Edition):
It 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.
3) Java ME (Java Micro Edition):
is dedicated to mobile applications.
4) JavaFX:
It is used to develop rich internet applications. It uses a lightweight user interface API.
Features of Java
8
create distributed applications
E.g., EJB & RMI software is from objects (i.e., Data and Operation)

separate programs can be automatic garbage collection


executed simultaneously ease to learn, code, clean & understand

✓ no explicit pointer
✓ programs run inside virtual machine
‘close’ to native code sandbox (JVM)
(interpreted language)

write once, run anywhere


an interpreted language;
C & C++ are a compiled
languages.
provides automatic garbage collection
Can handle Exception
automatic compilation
& GIGO
doesn't require any implementation.
no implementation dependent features
JDK, JRE, and JVM
9
JVM
✓ an abstract (virtual) machine, because it doesn't physically exist.
✓ provides a runtime environment in which Java bytecode can be executed.
✓ run those programs which are written in other languages and compiled to Java bytecode.
There are three notions of the JVM: specification, implementation, and instance.
Main tasks of JVM:
✓ Loads code
✓ Verifies code
✓ Executes code
✓ Provides runtime environment
JRE
✓ extends for Java Runtime Environment & provides the runtime environment.
✓ is a set of software tools which are used for developing Java applications.
✓ is the implementation of JVM. It physically exists.
✓ contains a set of libraries + other files that JVM uses at runtime.
10
JDK
✓ extends for Java Development Kit.
✓ is a software development environment used to develop Java applications and applets.
✓ It physically exists. It contains JRE + development tools.
✓ is an implementation of Java Platforms released by Oracle Corporation:
o Standard Edition Java Platform
o Enterprise Edition Java Platform
o Micro Edition Java Platform
The JDK comprises of:
✓ a private Java Virtual Machine (JVM) and
✓ an interpreter/loader (java),
✓ a compiler (javac),
✓ an archiver (jar),
✓ a documentation generator (Javadoc), etc. to complete the development of a Java Application.
How to set path in Java
11
The path is required to be set for using tools such as javac, java, etc.
If you are saving the Java source file inside the JDK/bin directory, the path is not required
to be set because all the tools will be available in the current directory.
However, if you have your Java file outside the JDK/bin folder, it is necessary to set the
path of JDK.
There are two ways to set the path in Java:
1.Temporary
2.Permanent
Setting temporary path of JDK
12
To set the temporary path of JDK, you need to follow the following steps:
•Open the command prompt
•Copy the path of the JDK/bin directory
•Write in command prompt: set path=copied_path
Setting permanent path of JDK
13
For setting the permanent path of JDK, you need to follow these steps:
•Go to This PC properties -> advanced tab -> environment variables -> new tab of user
variable -> write path in variable name -> write path of bin folder in variable value -> ok ->
ok -> ok

Figure show how to set permanent java path of JDK


Java Variables
14
✓ is a container which holds the value while the Java program is executed & assigned with a data type.
✓ is the name of a reserved area allocated in memory.
✓ is a name of the memory location (i.e., "vary + able" which means its value can be changed.
There are three types of variables in java: local, instance and static.
Local variable: declared inside the body of the method & not declared as static.
void method()
{
double pi=3.14;//local variable
}
Instance variable: declared inside the class but outside the body of the method & not declared as static.
public static void main(String args[])
{
int age=25;//instance variable
}
Static variable: any variable declared as static. E.g., static string name=“APJ”;//static variable
Method in Java
15
Method:
❑is a way to perform some task.
❑is a collection of instructions that performs a specific task, i.e., a block of code or collection
of statements or a set of code grouped together to perform a certain task or operation.
❑provides the reusability of code.
We can also easily modify code using methods.
E.g. public static void main(String args[]){
//body of method
}
There are two types of methods in Java:
❑ Predefined Method e.g., public static void main(String[] args), print(), println()
❑ User-defined Method e.g., findGreatestNum(), findIpAddr()
Java Keywords/reserved words
16
Java keywords are also known as reserved words. Keywords are particular words that act as a key to a code. These are
predefined words by Java so they cannot be used as a variable or object name or class name.
A list of Java keywords or reserved words are given below:
Keyword Description Keyword Description Keyword Description Keyword Description

abstract declares abstract class do iterates part of the implements implements an private
program interface
boolean True and False values double 64-bit import Avail classes & protected
interfaces to the code
break breaks the current flow of the else alternative options instanceof Tests instantiation public
program
byte 8-bit enum fixed set of constants int 32-bit return

case mark blocks of text extends class is derived from interface declares an interface short
class or interface.
catch catch the exceptions final constant value of var long static

char 16-bit finally block of code try super

class declare a class float 32-bit new switch

continue continues the current flow of the for to start a for loop null this
program
default specify the default block of code in if tests the conditions package throw
switch statement
Java Data Types & Operators
17
Data types
✓ specify the different sizes and values that can be stored in the variable.
There are two types of data types in Java: primitive and non-primitive.
Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.
Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.
Operators
✓ Symbols that perform simple computations, like &, |, +, * etc.
Types of operators in Java:
✓ Unary Operator: post incre(expr++, expr--) & pre incre(++expr, --expr, +expr, -expr, ~ ,!)
✓ Arithmetic Operator: *, /, %, +, -
✓ Shift Operator: <<, >>, >>>
✓ Relational Operator: <, >, <=, >=, ==(equal to), !=(not equal to)
✓ Bitwise Operator: &, ^, |
✓ Logical Operator: &&, ||
✓ Ternary Operator: ?, :
✓ Assignment Operator: =, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>=
Control statements in Java
18
Java compiler executes the code from top to bottom. The statements in the code are executed according to the
order in which they appear. However, 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
1. if statements → used to evaluate a condition. E.g., if. If-else, if-else-if, if and else-if
2. switch statement → if-else-if statements
2.Loop statements
1. do while loop--checks the condition at the end of the loop after executing the loop statements
2. while loop--iterate over the number of statements multiple times
3. for loop--enables us to initialize the loop variable, check the condition, and increment/decrement in a single line of
code
3.Jump statements--transfer the control of the program to the specific statements.
1. break statement--break the current flow of the program and transfer the control to the next statement outside a
loop or switch statement
2. continue statement--doesn't break the loop, whereas, it skips the specific part of the loop and jumps to the next
iteration of the loop immediately.
Reading assignments
18

Function in Java
Class in Java
Object in Java
Object oriented concepts in Java
Control statements in Java
Array in Java
New features of Java
Exception Handling in Java
Data structure and Algorithm

You might also like