Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Java Professional
Certificate
Day 1- Bridge Session
© Net Serpents LLC, USA
1
Java - An Introduction
§
Java - The new programming language from Sun Microsystems
§
Java -Allows anyone to publish a web page with Java code in it
§
Java - CPU Independent language
§
Oak -The predecessor of Java
Basic Features and
Concepts
© Net Serpents LLC, USA
Java Features
• Now owned by Oracle
Basic Features and
Concepts
• Object Oriented
• Portable
• Architecture Neutral
• Distributed
• Multi-threaded
• Robust, Secure/Safe
• Interpreted
• High Performance
© Net Serpents LLC, USA
Basic Features and
Concepts
Java Virtual Machine
§
JVM is an abstract machine.
§
Provides special environment to execute Java byte code .
§
a software residing on hardware and operating system.
Compiler
Source Code
Byte Code
Hello.java
Hello.class
Compiler
Interpreter
Hello World
Output
JVM
© Net Serpents LLC, USA
Java Technology Architecture
Basic Features and
Concepts
Java Source Code
Java Compiler
Java Classes
Runtime Support
Java Runtime
Environment
Standard Library
Classes
Java Virtual Machine
Java Plug-in
Browser
Operating System
Hardware
© Net Serpents LLC, USA
Java Development tools
§
java : Java Interpreter - runs the application.
§
javac : Java compiler - converts source code to byte code.
§
javadoc : creates HTML documents from java code used for creating documentation of the
program.
§
jdb : Java debugger , helps to fix errors in a program.
§
javah: It produces header files.
§
jar : A tool to make zipped files that are also Known as jar files(Java Archive Files)
§
Appletviewer : Helps to view and run applets.
Basic Features and
Concepts
© Net Serpents LLC, USA
Installation Demo
Eclipse
Learn how to install and use Eclipse
© Net Serpents LLC, USA
What is Eclipse?
§
It’s an IDE(Integrated Development Environment)
§
A software development kit (SDK) that includes the Java development tools.
§
A Java-based application that requires a Java runtime environment (JRE) OR
Eclipse
Java development kit(JDK) in order to run.
© Net Serpents LLC, USA
Basic Features and
Concepts
Create a program
§
Using Notepad or another text editor, create a small Java file with the following text:
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello, World!");
}
}
§
Save this file as HelloWorld.java at a desired location.
main is an essential method that must
be present to run a java program
© Net Serpents LLC, USA
Classes & Objects
OOP Concepts
§
A class is a blueprint or prototype from which objects are created.
§
Each object is said to be an ‘instance’ of a particular class.
Class
Example
public class Employee
{
String name;
String department;
bonus() {
…
...
}
}
© Net Serpents LLC, USA
Objects
OOP Concepts
§
Structuresthat contain both data and methods
§
Employee class used to create two objects : emp1, emp2 below
class
Employee
Object
Object
emp1
Data for emp1
Method
(behavior) for
emp1
emp1.name
emp1.department
emp1.bonus()
emp2
emp2.name
emp2.department
emp2.bonus()
Data for emp2 object
Method (behavior)
for emp2
© Net Serpents LLC, USA
Four OOP Principles
OOP Concepts
Data Abstraction
Encapsulation
Inheritance
Polymorphism
© Net Serpents LLC, USA
Data Abstraction
§
Act of representing essential features without including the background details or explanations
§
Eg., a sort() method within a class presents sorted data to the outside world and hides the
implementation details (algorithm) that may change over time
OOP Concepts
© Net Serpents LLC, USA
Encapsulation
§
Most striking feature of a class.
§
Wrapping up of data and methods into a single unit (called class).
§
It makes objects to be treated like “black boxes”.
OOP Concepts
© Net Serpents LLC, USA
Inheritance
§
A process by which object of one class acquires the properties of another class (like a son child
inheriting traits of mother)
§
Parent class is called “Superclass”.
§
Child class is known as “subclass”.
OOP Concepts
© Net Serpents LLC, USA
Inheritance
OOP Concepts
Employee
Name, Emp_id, etc
Super Class
printinfo()
tax()
etc
regular
Name, Emp_id, etc
paid_leaves
annual_bonus ()
contractor
Name, Emp_id, etc
contracted_hours
compute_salary()
etc
Sub Classes
daily_wage
Name, Emp_id, etc
daily_rate
compute_wage()
© Net Serpents LLC, USA
Polymorphism
§
Ability to take same name but more than one form.
§
allows objects to share different internal structure but same external structure.
§
It is extensively used in implementing inheritance.
§
Single function name can be used to handle different types and different number of
arguments.
OOP Concepts
© Net Serpents LLC, USA
Polymorphism
OOP Concepts
Employee
printInfo()
regular
printInfo(salary)
contractor
printInfo(hours)
daily_wage
printInfo(rate)
© Net Serpents LLC, USA
Java Program Structure
Documentation Section
Package Statement
Program Structure
Suggested
Optional
Import Statements
Optional
Interface Statements
Class Definitions
Main Method class
{
Main Method Definition
}
Optional
Essential
Essential
© Net Serpents LLC, USA
Java Tokens
§
Tokens
The smallest individual unit in a program is known as token.
© Net Serpents LLC, USA
Data Types
Data Types
§
Data types specify the size and type of values to be stored.
§
Java language is rich in data types.
Data types in Java
Primitive
(built-in)
Non-Primitive
(Derived)
© Net Serpents LLC, USA
Primitive Data Types
Data Types
Primitive
Numeric
Integers
Non-Numeric
Floatingpoint
Character
Boolean
© Net Serpents LLC, USA
Variables
§
It is an identifier.
§
Basic unit of storage.
§
It defines scope and a lifetime.
§
Variable names may consist :
Variables
Ø alphabets
Ø digits,
Ø underscore (_)
Ø dollar sign($)
© Net Serpents LLC, USA
Type Conversion
§
Variables
Process of converting one primitive type to another.
Type
conversion
Implicit
Conversion
Explicit
Conversion
© Net Serpents LLC, USA
Implicit Conversion
§
Assigning one type of data to another type is Automatic Conversion .
§
Conversion done by Java complier.
§
Two conditions:
Variables
1. Two types are compatible.
2. The destination type is larger than source type.
§
Also called Widening conversion.
Assigning a smaller
type to larger type
is called widening.
© Net Serpents LLC, USA
Examples
§
Variables
Implicit Conversion
int a = 5;
double b = a;
§
int
double
(4 Bytes)
(8 bytes)
Explicit Conversion
long a =500;
int b = (int) a;
long
(8 Bytes)
int
(4 bytes)
© Net Serpents LLC, USA
Example 1
Variables
Class Example1
{
public static void main(String args[])
{
System.out.println("Variables created ");
int i= 1234;
long l= 12345678;
float f1 =3.1242f;
System.out.println( " i : " + i);
System.out.println( " l : " + l);
System.out.println( " f1 : " + f1);
System.out.println(" ");
System.out.println( " Type Conversion");
float m=(float)l;
int f2=(int)f1;
System.out.println( " (float)l : " + m);
System.out.println( " (int)f1 : " + f2);
}
}
© Net Serpents LLC, USA
Output
Variables
Variables created
i : 1234
l : 12345678
f1 : 3.1242
Type Conversion
(float)l : 1.2345678E7
(int)f1 : 3
© Net Serpents LLC, USA
Control Statements
Control Statements
Programming languages use control statements.
§
Handles the flow of execution.
§
To advance and branch based on state of a program.
Control flow
§
Selection Statements
Allows to choose different paths of execution based
upon the outcome of expression.
•
•
If
Switch
Iteration Statements
Repeats one or more statements during program
execution.
•
•
•
While
Do-while
For
Allows program to execute in a non-linear manner.
•
•
•
Break
Continue
Retun
Jump Statements
© Net Serpents LLC, USA
Classes
and
Methods
Class Fundamentals
Attributes
Behavior
§
Data that differentiates one object from another
§
Things that class of objects can do
§
Defined by:
§
Eg.
Instance variables
§
§
change attributes of an object
§
aka object variables
§
receive information from other objects
§
define attributes of one particular object
§
send messages to other objects
Class Variables
§
§
define attributes of entire class
§
one value for all objects
© Net Serpents LLC, USA
Classes
and
Methods
General form of a class
class Class_name
{
type variable1;
type variable2;
Instance variables
type methodname1(parameter-list)
{
body of the method
}
}
type methodname2(parameter-list)
{
body of the method
}
methods
Members
© Net Serpents LLC, USA
Classes
and
Methods
A Simple Class
class Employee
{
int empid;
String name;
String dept;
}
Class name
Instance variables
© Net Serpents LLC, USA
Classes
and
Objects
Declaring Objects
§
1.
Two-step process:
Declare a variable of class type.
Employee emp;
2.
Acquire an actual physical copy of the object and assign it to that variable.
emp = new Employee();
§
Combining above steps:
Employee emp = new Employee();
© Net Serpents LLC, USA
Classes
and
Objects
new Operator
§
Dynamically allocates memory for an object.
Statement
Employee emp;
Effect
null
emp
empid
emp = new Employee();
emp
name
dept
Employee Object
© Net Serpents LLC, USA
Classes
and
Objects
Assigning Object Reference
§
Employee emp1 = new Employee ;
§
Employee emp2 = emp1;
emp1
empid
name
dept
emp2
Employee Object
© Net Serpents LLC, USA
Classes
and
Objects
Creating Object: Example 2
class Employee
{
int empid;
String name;
String dept;
}
void display( )
{
System.out.println("Employee Id:" + empid);
System.out.println("Name : " + name);
System.out.println("Department : " + dept);
}
A dot operator links the
name of an object with
instance variable.
public class Employee_trial
{
public static void main(String[] args)
{
Employee emp1= new Employee();
emp1.empid = 001;
emp1.name = "John";
emp1.dept = "Production";
}
}
emp1.display( );
© Net Serpents LLC, USA
Methods:
Example 3
Classes
and
Objects
class Employee
{
int empid;
String name;
String dept;
void display( )
{
System.out.println(“Employee Id: “ + empid);
System.out.println(“Name : “ + name);
System.out.println(“Department : “ + dept);
}
Employee emp2= new Employee();
emp2.empid = 002;
emp2.name = “Deep";
emp2.dept = “IT";
emp2.display( );
}
© Net Serpents LLC, USA
Types
Constructors
Constructors
Parameterized
Receive Parameters
Non Parameterized
Cannot receive any
Parameters
© Net Serpents LLC, USA
Parameterized Constructor Example 4
class Employee
{
int empid;
String name;
String dept;
public Employee(int i , String n , String d)
{
empid = i;
name = n;
dept = d;
}
Constructors
Constructor is a method with the
same name as the class name. Sets
values automatically for a new
object
public static void main(String[] args)
{
Employee e1=new Employee(001 , "John" , "Production");
Employee e2=new Employee(); //Invalid
}
}
© Net Serpents LLC, USA
Non-parameterized Constructor
§
Also called default Constructor.
§
In a class with no explicit constructor ,compiler
Constructors
1. Supplies a default constructor.
2. Initializes the data members with any dummy value.
© Net Serpents LLC, USA
Example 5
Constructors
public class Employee
{
int empid;
String name;
String dept;
}
public Employee()
{ empid =0;
name =" ";
dept =" ";
}
// Constructor
public class Employee_trial
{
public static void main(String[] args)
{
Employee e1=new Employee();
}
}
© Net Serpents LLC, USA
Inheritance basics
§
§
Inheritance
The syntax of defining a derived/ subclass:
Example
class <sub class-name> extends <super class-name>
{
body of class
}
class salaried_Employee extends Employee
{
body of class
}
© Net Serpents LLC, USA
Member Access
§
Inheritance
Access control of inherited members:
Access Specifier
Description
private
Members are accessible only inside their own class
and nowhere else
public
Accessible in all the sub classes and class in the
same package
protected
Accessible in their own class and subclasses
regardless whether subclasses exist in the same
package or other package.
default
Accessible in the class with in the same package
© Net Serpents LLC, USA
Example
public class Employee
{
int empid;
String name;
String dept;
public Employee()
{
empid=0;
name="null";
dept="null";
}
public Employee(int i, String n, String d)
{
empid=i;
name=n;
dept=d;
}
void show( )
{
System.out.println("Employee Id: " + empid);
System.out.println("Name : " + name);
System.out.println("Department : " + dept);
} }
Inheritance
© Net Serpents LLC, USA
44
Example- Inherited class
Inheritance
public class Salaried_Employee extends Employee
{ int no_of_days;
Salaried_Employee(int i, String n, String d, int num)
{
empid=i;
name=n;
dept=d;
no_of_days=num;
}
}
© Net Serpents LLC, USA
Packages
§
Containers for class.
§
Keep the class name space compartmentalized.
§
Provides naming and visibility control mechanism.
§
Stored in a hierarchical manner.
§
Explicitly imported into new class definitions.
Packages
java.lang is the default package.
© Net Serpents LLC, USA
46
Definition
Packages
§
Include a package command as the first statement in a Java Source file.
§
General form:
package Mypackage;
§
Above statement defines a name space in which classes are stored.
§
All the standard java classes included with Java are stored in a package called java.
Name of the
package
© Net Serpents LLC, USA
Example
§
Packages
Creating two packages:
package Mypack;
public class first
{
public static void main(String[] args) {
System.out.println("Welcome to Package");
} }
package Newpackage;
public class Second {
public void msg()
{
System.out.println("Hello World");
} }
© Net Serpents LLC, USA
48
Accessing Packages
Packages
import package.*;
import package.classname;
Fully qualified name
© Net Serpents LLC, USA
Interfaces: Introduction
§
Similar to a class in Java.
§
Lack instance variables.
§
Methods are declared without any body.
§
An interface contains behaviours that a class implements.
Interfaces
interface A
{
void show();
}
© Net Serpents LLC, USA
50
Contd..
Interfaces
§
Once defined, any number of classes can implement one interface.
§
One class can implement any number of interfaces.
§
Java implements “ One interface, multiple methods” –Polymorphism.
class B implements A
{
void show()
{
//method body
}
}
© Net Serpents LLC, USA
51
Interfaces
§
No default implementation of any method specified within any interface.
§
Variables can be declared inside of interface declarations with final and static keyword.
§
All the methods and variables are implicitly public.
§
Class that includes an interface must implement all of the methods but not variables.
§
Variables must be initialized within an interface.
Interfaces
© Net Serpents LLC, USA
52
Interface Features
§
An interface can contain any number of methods.
§
An interface is written in a file with a .java extension, with the name of the interface matching
the name of the file.
§
The byte code of an interface appears in a .class file.
§
Interfaces appear in packages, and their corresponding byte code file must be in a directory
structure that matches the package name.
Interfaces
© Net Serpents LLC, USA
53
Interface
§
You cannot instantiate an interface.
§
An interface does not contain any constructors.
§
All of the methods in an interface are abstract.
§
An interface cannot contain instance fields. The only fields that can appear in an interface
must be declared both static and final.
§
An interface is not extended by a class; it is implemented by a class.
§
An interface can extend multiple interfaces.
Interfaces
© Net Serpents LLC, USA
54
Defining
Interfaces
General form of an interface –
§
Access-specifier interface name
{
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1=value;
type final-varname2=value;
}
Case1 : No access specifier
§
§
Then default access and the interface is available to other members of the package.
Case2 : public access
§
§
Interface can be used by any other code.
© Net Serpents LLC, USA
55
Example
§
Interfaces
Defining an interface
interface Callback
{
void call(int param);
}
© Net Serpents LLC, USA
56
Implementation
§
Include the implements clause in the class definition.
§
General form:
Interfaces
class classname implements interface1[,interface2]]
{
//class body
}
§
In case of more than one interface ,the interfaces are separated with a comma.
© Net Serpents LLC, USA
Example
Interfaces
Declaring a class implementing Callback interface.
§
class Client implements Callback
{
public void call(int p)
{
System.out.println(“Callback called with “ + p);
}
}
© Net Serpents LLC, USA