SlideShare uma empresa Scribd logo
1 de 30
Java Tutorial
Object-Oriented Programming

Rajan Gupta
Singsys Pte. Ltd.
Different Programming Paradigms
Functional/procedural programming:
program is a list of instructions to the computer
Object-oriented programming
program is composed of a collection objects that
communicate with each other
Main Concepts
 Object

Class
Inheritance
Encapsulation
Objects
identity – unique identification of an object
attributes – data/state
services – methods/operations
− supported by the object
− within objects responsibility to provide these
services to other clients
Inheritance
Class hierarchy
Generalization and Specialization
− subclass inherits attributes and services from its
superclass
− subclass may add new attributes and services
− subclass may reuse the code in the superclass
− subclasses provide specialized behaviors (overriding
and dynamic binding)
− partially define and implement common behaviors
(abstract)
Encapsulation
 Separation between internal state of the object and its external
aspects

 How ?

− control access to members of the class
− interface “type”
Why Java ?
Portable
Easy to learn
[ Designed to be used on the Internet ]
Platform Dependent

myprog.c
C source code

gcc

myprog.exe
machine code
OS/Hardware

Platform Independent

myprog.java

javac

myprog.class
bytecode

Java source code
JVM
OS/Hardware
Primitive types
•
•
•
•
•
•
•
•

int
4 bytes
short 2 bytes
Behaviors is
long
8 bytes
exactly as
byte
1 byte
in C++
float
4 bytes
double 8 bytes
char
Unicode encoding (2 bytes)
Note:
boolean {true,false}
Primitive type
always begin
with lower-case
Wrappers
Java provides Objects which
wrap primitive types and supply
methods.
Example:
Integer n = new Integer(“4”);
int m = n.intValue();

Read more about Integer in JDK Documentation
Hello World
Hello.java

class Hello {
public static void main(String[] args) {
System.out.println(“Hello World !!!”);
}
}

C:javac Hello.java
C:java Hello

( compilation creates Hello.class )
(Execution on the local JVM)
Arrays
• Array is an object
• Array size is fixed
Animal[] arr; // nothing yet …
arr = new Animal[4]; // only array of pointers
for(int i=0 ; i < arr.length ; i++) {
arr[i] = new Animal();
// now we have a complete array
Arrays - Multidimensional
 In C++ a
Animal arr[2][2]

Is:
• In Java
Animal[][] arr=
new Animal[2][2]
What is the type of
the object here ?
Static data
Member data - Same data is used for all the
instances (objects) of some Class. Assignment performed
on the first access to
the
Class.
Only one instance of
‘x’
exists in memoryb
a

Class A {
public int y = 0;
public static int x_ =
1;
};
A a = new A();
A b = new A();
System.out.println(b.x_);
a.x_ = 5;
System.out.println(b.x_);
A.x_ = 10;
System.out.println(b.x_);

Output:
1
5
10

0
y

0
y

1
A.x_
Static
• Member function
– Static member function can access only static
members
– Static member function can be called without an
instance.
Class TeaPot {
private static int numOfTP =
0;
private Color myColor_;
public TeaPot(Color c) {
myColor_ = c;
numOfTP++;
}
public static int
howManyTeaPots()
{ return numOfTP; }
// error :
public static Color getColor()
{ return myColor_; }

}
String is an Object
• Constant strings as in C, does not exist
• The function call foo(“Hello”) creates a String
object, containing “Hello”, and passes reference
to it to foo.
• There is no point in writing :
String s = new String(“Hello”);

• The String object is a constant. It can’t be
changed using a reference to it.
Flow control
Basically, it is exactly like c/c++.
do/while
if/else

If(x==4)
{
//
act1
} else {
//
act2
}

int i=5;
do {
// act1
i--;
} while(i!=0);
for

int j;
for(int i=0;i<=9;i++)
{
j+=i;
}

switch

char
c=IN.getChar();
switch(c) {
case ‘a’:
case ‘b’:
// act1
break;
default:
// act2
}
Access Control
• public member (function/data)
– Can be called/modified from outside.

• protected
– Can be called/modified from derived classes

• private
– Can be called/modified only from the current class

• default ( if no access modifier stated )
– Usually referred to as “Friendly”.
– Can be called/modified/instantiated from the same package.
Inheritance
Base

Derived

class Base {
Base(){}
Base(int i) {}
protected void foo() {…}
}
class Derived extends Base {
Derived() {}
protected void foo() {…}
Derived(int i) {
super(i);
…
super.foo();
}
}

As opposed to C++, it is possible to inherit only from ONE class.
Pros avoids many potential problems and bugs.
Cons might cause code replication
Inheritance (2)
• In Java, all methods are virtual :
class Base {
void foo() {
System.out.println(“Base”);
}
}
class Derived extends Base {
void foo() {
System.out.println(“Derived”);
}
}
public class Test {
public static void main(String[] args) {
Base b = new Derived();
b.foo(); // Derived.foo() will be activated
}
}
Abstract
• abstract member function, means that the function
does not have an implementation.
• abstract class, is class that can not be instantiated.
AbstractTest.java:6: class AbstractTest is an abstract
class.
It can't be instantiated.
new AbstractTest();
^
1 error
NOTE:
An abstract class is not required to have an abstract method in it.
But any class that has an abstract method in it or that does
not provide an implementation for any abstract methods declared
in its superclasses must be declared as an abstract class.
Interface
Interfaces are useful for the following:
• Capturing similarities among unrelated classes
without artificially forcing a class relationship.
• Declaring methods that one or more classes are
expected to implement.
• Revealing an object's programming interface
without revealing its class.
Interface
interface IChef {
void cook(Food
food);
}
interface BabyKicker {
void kickTheBaby(Baby);
}

interface SouthParkCharacter {
void curse();
}

class Chef implements IChef, SouthParkCharacter {
// overridden methods MUST be public
// can you tell why ?
public void curse() { … }
public void cook(Food f) { … }
}
* access rights (Java forbids reducing of access rights)
Collections
• Collection/container
– object that groups multiple elements
– used to store, retrieve, manipulate, communicate
aggregate data
• Iterator - object used for traversing a collection and
selectively remove elements
• Generics – implementation is parametric in the type of
elements
Java Collection Framework
• Goal: Implement reusable data-structures and functionality
• Collection interfaces - manipulate collections
independently of representation details
• Collection implementations - reusable data structures
List<String> list = new ArrayList<String>(c);

• Algorithms - reusable functionality
– computations on objects that implement collection interfaces
– e.g., searching, sorting
– polymorphic: the same method can be used on many different
implementations of the appropriate collection interface
Collection Interface
• Basic Operations
–
–
–
–
–
–

int size();
boolean isEmpty();
boolean contains(Object element);
boolean add(E element);
boolean remove(Object element);
Iterator iterator();

• Bulk Operations
–
–
–
–
–

boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c);
boolean removeAll(Collection<?> c);
boolean retainAll(Collection<?> c);
void clear();

• Array Operations

– Object[] toArray(); <T> T[] toArray(T[] a); }
Final
• final member data
Constant member
• final member function
The method can’t be
overridden.
• final class
‘Base’ is final, thus it
can’t be extended

final class Base {
final int i=5;
final void foo() {
i=10;
//what will the compiler
say about this?
}
}
class Derived extends Base
{ // Error
// another foo ...
void foo() {
}

(String class is final)

}
final
Derived.java:6: Can't subclass final classes: class Base
class class Derived extends Base {
^
1 error

final class Base {
final int i=5;
final void foo() {
i=10;
}
}
class Derived extends
Base { // Error
// another foo ...
void foo() {
}
}
Exception - What is it and why do I care?
Definition: An exception is an event that occurs during the
execution of a program that disrupts the normal flow of
instructions.

• Exception is an Object
• Exception class must be descendent of
Throwable.
Thank You!

Mais conteúdo relacionado

Mais procurados (20)

Java Basics
Java BasicsJava Basics
Java Basics
 
Basics of Java
Basics of JavaBasics of Java
Basics of Java
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Core java Essentials
Core java EssentialsCore java Essentials
Core java Essentials
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java Programming
 
Introduction to java 101
Introduction to java 101Introduction to java 101
Introduction to java 101
 
Java basic
Java basicJava basic
Java basic
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Core java concepts
Core java concepts Core java concepts
Core java concepts
 
Java API, Exceptions and IO
Java API, Exceptions and IOJava API, Exceptions and IO
Java API, Exceptions and IO
 
Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+java
 
JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
 
Introduction to-programming
Introduction to-programmingIntroduction to-programming
Introduction to-programming
 
Java interview questions 1
Java interview questions 1Java interview questions 1
Java interview questions 1
 
Object oriented concepts with java
Object oriented concepts with javaObject oriented concepts with java
Object oriented concepts with java
 

Semelhante a Java Tutorial

Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.Tarunsingh198
 
JavaTutorials.ppt
JavaTutorials.pptJavaTutorials.ppt
JavaTutorials.pptKhizar40
 
Core java by a introduction sandesh sharma
Core java by a introduction sandesh sharmaCore java by a introduction sandesh sharma
Core java by a introduction sandesh sharmaSandesh Sharma
 
Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project LambdaRahman USTA
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)Gandhi Ravi
 
Object-oriented programming
Object-oriented programmingObject-oriented programming
Object-oriented programmingNeelesh Shukla
 
Chap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxChap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxchetanpatilcp783
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaCPD INDIA
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...Sagar Verma
 
Object Oriented Programming Concepts using Java
Object Oriented Programming Concepts using JavaObject Oriented Programming Concepts using Java
Object Oriented Programming Concepts using JavaGlenn Guden
 
Development of Java tools using SWT and WALA af Hans Søndergaard, ViaUC
Development of Java tools using SWT and WALA af Hans Søndergaard, ViaUCDevelopment of Java tools using SWT and WALA af Hans Søndergaard, ViaUC
Development of Java tools using SWT and WALA af Hans Søndergaard, ViaUCInfinIT - Innovationsnetværket for it
 
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions Soumen Santra
 
object oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoMobject oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoMambikavenkatesh2
 

Semelhante a Java Tutorial (20)

Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 
JavaTutorials.ppt
JavaTutorials.pptJavaTutorials.ppt
JavaTutorials.ppt
 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
 
Java Tutorials
Java Tutorials Java Tutorials
Java Tutorials
 
java training faridabad
java training faridabadjava training faridabad
java training faridabad
 
Core java by a introduction sandesh sharma
Core java by a introduction sandesh sharmaCore java by a introduction sandesh sharma
Core java by a introduction sandesh sharma
 
Cse java
Cse javaCse java
Cse java
 
Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project Lambda
 
core java
core javacore java
core java
 
C#2
C#2C#2
C#2
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)
 
Object-oriented programming
Object-oriented programmingObject-oriented programming
Object-oriented programming
 
Collections
CollectionsCollections
Collections
 
Chap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxChap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptx
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
 
Object Oriented Programming Concepts using Java
Object Oriented Programming Concepts using JavaObject Oriented Programming Concepts using Java
Object Oriented Programming Concepts using Java
 
Development of Java tools using SWT and WALA af Hans Søndergaard, ViaUC
Development of Java tools using SWT and WALA af Hans Søndergaard, ViaUCDevelopment of Java tools using SWT and WALA af Hans Søndergaard, ViaUC
Development of Java tools using SWT and WALA af Hans Søndergaard, ViaUC
 
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
 
object oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoMobject oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoM
 

Mais de Singsys Pte Ltd

Technical Seminar Series: GIT Pull Requests Best Practices
Technical Seminar Series:  GIT Pull Requests Best PracticesTechnical Seminar Series:  GIT Pull Requests Best Practices
Technical Seminar Series: GIT Pull Requests Best PracticesSingsys Pte Ltd
 
Laravel Security Standards
Laravel Security Standards Laravel Security Standards
Laravel Security Standards Singsys Pte Ltd
 
Android OS - The Journey of most popular Operating System
Android OS - The Journey of most popular Operating SystemAndroid OS - The Journey of most popular Operating System
Android OS - The Journey of most popular Operating SystemSingsys Pte Ltd
 
How to do Memory Optimizations in Android
How to do Memory Optimizations in AndroidHow to do Memory Optimizations in Android
How to do Memory Optimizations in AndroidSingsys Pte Ltd
 
iOS Application Battery Optimization Techniques
iOS Application Battery Optimization TechniquesiOS Application Battery Optimization Techniques
iOS Application Battery Optimization TechniquesSingsys Pte Ltd
 
Android Battery optimization Android Apps
Android Battery optimization Android AppsAndroid Battery optimization Android Apps
Android Battery optimization Android AppsSingsys Pte Ltd
 
How to Create WordPress Website in Easy Steps
How to Create WordPress Website in Easy StepsHow to Create WordPress Website in Easy Steps
How to Create WordPress Website in Easy StepsSingsys Pte Ltd
 
Introduction to facebook sdk
Introduction to facebook sdkIntroduction to facebook sdk
Introduction to facebook sdkSingsys Pte Ltd
 
Html5 tutorial for beginners
Html5 tutorial for beginnersHtml5 tutorial for beginners
Html5 tutorial for beginnersSingsys Pte Ltd
 
Beginners css tutorial for web designers
Beginners css tutorial for web designersBeginners css tutorial for web designers
Beginners css tutorial for web designersSingsys Pte Ltd
 
Joomla 3 installation and management guide
Joomla 3 installation and management guideJoomla 3 installation and management guide
Joomla 3 installation and management guideSingsys Pte Ltd
 
Joomla Introduction & Installation Tutorial
Joomla Introduction & Installation TutorialJoomla Introduction & Installation Tutorial
Joomla Introduction & Installation TutorialSingsys Pte Ltd
 
Technical seo tips for web developers
Technical seo tips for web developersTechnical seo tips for web developers
Technical seo tips for web developersSingsys Pte Ltd
 
WordPress Website Design and Development
WordPress Website Design and DevelopmentWordPress Website Design and Development
WordPress Website Design and DevelopmentSingsys Pte Ltd
 
Points for Design and Development of SEO friendly websites
Points for Design and Development of SEO friendly websitesPoints for Design and Development of SEO friendly websites
Points for Design and Development of SEO friendly websitesSingsys Pte Ltd
 

Mais de Singsys Pte Ltd (20)

Technical Seminar Series: GIT Pull Requests Best Practices
Technical Seminar Series:  GIT Pull Requests Best PracticesTechnical Seminar Series:  GIT Pull Requests Best Practices
Technical Seminar Series: GIT Pull Requests Best Practices
 
Laravel Security Standards
Laravel Security Standards Laravel Security Standards
Laravel Security Standards
 
Android OS - The Journey of most popular Operating System
Android OS - The Journey of most popular Operating SystemAndroid OS - The Journey of most popular Operating System
Android OS - The Journey of most popular Operating System
 
How to do Memory Optimizations in Android
How to do Memory Optimizations in AndroidHow to do Memory Optimizations in Android
How to do Memory Optimizations in Android
 
iOS Application Battery Optimization Techniques
iOS Application Battery Optimization TechniquesiOS Application Battery Optimization Techniques
iOS Application Battery Optimization Techniques
 
Android Battery optimization Android Apps
Android Battery optimization Android AppsAndroid Battery optimization Android Apps
Android Battery optimization Android Apps
 
How to Create WordPress Website in Easy Steps
How to Create WordPress Website in Easy StepsHow to Create WordPress Website in Easy Steps
How to Create WordPress Website in Easy Steps
 
Basics of-linux
Basics of-linuxBasics of-linux
Basics of-linux
 
SoLoMo
SoLoMoSoLoMo
SoLoMo
 
Introduction to facebook sdk
Introduction to facebook sdkIntroduction to facebook sdk
Introduction to facebook sdk
 
Html5 tutorial for beginners
Html5 tutorial for beginnersHtml5 tutorial for beginners
Html5 tutorial for beginners
 
Beginners css tutorial for web designers
Beginners css tutorial for web designersBeginners css tutorial for web designers
Beginners css tutorial for web designers
 
Joomla 3 installation and management guide
Joomla 3 installation and management guideJoomla 3 installation and management guide
Joomla 3 installation and management guide
 
Joomla Introduction & Installation Tutorial
Joomla Introduction & Installation TutorialJoomla Introduction & Installation Tutorial
Joomla Introduction & Installation Tutorial
 
Basic of web design
Basic of web designBasic of web design
Basic of web design
 
Embedded Technology
Embedded TechnologyEmbedded Technology
Embedded Technology
 
Technical seo tips for web developers
Technical seo tips for web developersTechnical seo tips for web developers
Technical seo tips for web developers
 
WordPress Website Design and Development
WordPress Website Design and DevelopmentWordPress Website Design and Development
WordPress Website Design and Development
 
Being a designer
Being a designerBeing a designer
Being a designer
 
Points for Design and Development of SEO friendly websites
Points for Design and Development of SEO friendly websitesPoints for Design and Development of SEO friendly websites
Points for Design and Development of SEO friendly websites
 

Último

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 

Último (20)

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

Java Tutorial

  • 2. Different Programming Paradigms Functional/procedural programming: program is a list of instructions to the computer Object-oriented programming program is composed of a collection objects that communicate with each other
  • 4. Objects identity – unique identification of an object attributes – data/state services – methods/operations − supported by the object − within objects responsibility to provide these services to other clients
  • 5. Inheritance Class hierarchy Generalization and Specialization − subclass inherits attributes and services from its superclass − subclass may add new attributes and services − subclass may reuse the code in the superclass − subclasses provide specialized behaviors (overriding and dynamic binding) − partially define and implement common behaviors (abstract)
  • 6. Encapsulation  Separation between internal state of the object and its external aspects  How ? − control access to members of the class − interface “type”
  • 7. Why Java ? Portable Easy to learn [ Designed to be used on the Internet ]
  • 8. Platform Dependent myprog.c C source code gcc myprog.exe machine code OS/Hardware Platform Independent myprog.java javac myprog.class bytecode Java source code JVM OS/Hardware
  • 9. Primitive types • • • • • • • • int 4 bytes short 2 bytes Behaviors is long 8 bytes exactly as byte 1 byte in C++ float 4 bytes double 8 bytes char Unicode encoding (2 bytes) Note: boolean {true,false} Primitive type always begin with lower-case
  • 10. Wrappers Java provides Objects which wrap primitive types and supply methods. Example: Integer n = new Integer(“4”); int m = n.intValue(); Read more about Integer in JDK Documentation
  • 11. Hello World Hello.java class Hello { public static void main(String[] args) { System.out.println(“Hello World !!!”); } } C:javac Hello.java C:java Hello ( compilation creates Hello.class ) (Execution on the local JVM)
  • 12. Arrays • Array is an object • Array size is fixed Animal[] arr; // nothing yet … arr = new Animal[4]; // only array of pointers for(int i=0 ; i < arr.length ; i++) { arr[i] = new Animal(); // now we have a complete array
  • 13. Arrays - Multidimensional  In C++ a Animal arr[2][2] Is: • In Java Animal[][] arr= new Animal[2][2] What is the type of the object here ?
  • 14. Static data Member data - Same data is used for all the instances (objects) of some Class. Assignment performed on the first access to the Class. Only one instance of ‘x’ exists in memoryb a Class A { public int y = 0; public static int x_ = 1; }; A a = new A(); A b = new A(); System.out.println(b.x_); a.x_ = 5; System.out.println(b.x_); A.x_ = 10; System.out.println(b.x_); Output: 1 5 10 0 y 0 y 1 A.x_
  • 15. Static • Member function – Static member function can access only static members – Static member function can be called without an instance. Class TeaPot { private static int numOfTP = 0; private Color myColor_; public TeaPot(Color c) { myColor_ = c; numOfTP++; } public static int howManyTeaPots() { return numOfTP; } // error : public static Color getColor() { return myColor_; } }
  • 16. String is an Object • Constant strings as in C, does not exist • The function call foo(“Hello”) creates a String object, containing “Hello”, and passes reference to it to foo. • There is no point in writing : String s = new String(“Hello”); • The String object is a constant. It can’t be changed using a reference to it.
  • 17. Flow control Basically, it is exactly like c/c++. do/while if/else If(x==4) { // act1 } else { // act2 } int i=5; do { // act1 i--; } while(i!=0); for int j; for(int i=0;i<=9;i++) { j+=i; } switch char c=IN.getChar(); switch(c) { case ‘a’: case ‘b’: // act1 break; default: // act2 }
  • 18. Access Control • public member (function/data) – Can be called/modified from outside. • protected – Can be called/modified from derived classes • private – Can be called/modified only from the current class • default ( if no access modifier stated ) – Usually referred to as “Friendly”. – Can be called/modified/instantiated from the same package.
  • 19. Inheritance Base Derived class Base { Base(){} Base(int i) {} protected void foo() {…} } class Derived extends Base { Derived() {} protected void foo() {…} Derived(int i) { super(i); … super.foo(); } } As opposed to C++, it is possible to inherit only from ONE class. Pros avoids many potential problems and bugs. Cons might cause code replication
  • 20. Inheritance (2) • In Java, all methods are virtual : class Base { void foo() { System.out.println(“Base”); } } class Derived extends Base { void foo() { System.out.println(“Derived”); } } public class Test { public static void main(String[] args) { Base b = new Derived(); b.foo(); // Derived.foo() will be activated } }
  • 21. Abstract • abstract member function, means that the function does not have an implementation. • abstract class, is class that can not be instantiated. AbstractTest.java:6: class AbstractTest is an abstract class. It can't be instantiated. new AbstractTest(); ^ 1 error NOTE: An abstract class is not required to have an abstract method in it. But any class that has an abstract method in it or that does not provide an implementation for any abstract methods declared in its superclasses must be declared as an abstract class.
  • 22. Interface Interfaces are useful for the following: • Capturing similarities among unrelated classes without artificially forcing a class relationship. • Declaring methods that one or more classes are expected to implement. • Revealing an object's programming interface without revealing its class.
  • 23. Interface interface IChef { void cook(Food food); } interface BabyKicker { void kickTheBaby(Baby); } interface SouthParkCharacter { void curse(); } class Chef implements IChef, SouthParkCharacter { // overridden methods MUST be public // can you tell why ? public void curse() { … } public void cook(Food f) { … } } * access rights (Java forbids reducing of access rights)
  • 24. Collections • Collection/container – object that groups multiple elements – used to store, retrieve, manipulate, communicate aggregate data • Iterator - object used for traversing a collection and selectively remove elements • Generics – implementation is parametric in the type of elements
  • 25. Java Collection Framework • Goal: Implement reusable data-structures and functionality • Collection interfaces - manipulate collections independently of representation details • Collection implementations - reusable data structures List<String> list = new ArrayList<String>(c); • Algorithms - reusable functionality – computations on objects that implement collection interfaces – e.g., searching, sorting – polymorphic: the same method can be used on many different implementations of the appropriate collection interface
  • 26. Collection Interface • Basic Operations – – – – – – int size(); boolean isEmpty(); boolean contains(Object element); boolean add(E element); boolean remove(Object element); Iterator iterator(); • Bulk Operations – – – – – boolean containsAll(Collection<?> c); boolean addAll(Collection<? extends E> c); boolean removeAll(Collection<?> c); boolean retainAll(Collection<?> c); void clear(); • Array Operations – Object[] toArray(); <T> T[] toArray(T[] a); }
  • 27. Final • final member data Constant member • final member function The method can’t be overridden. • final class ‘Base’ is final, thus it can’t be extended final class Base { final int i=5; final void foo() { i=10; //what will the compiler say about this? } } class Derived extends Base { // Error // another foo ... void foo() { } (String class is final) }
  • 28. final Derived.java:6: Can't subclass final classes: class Base class class Derived extends Base { ^ 1 error final class Base { final int i=5; final void foo() { i=10; } } class Derived extends Base { // Error // another foo ... void foo() { } }
  • 29. Exception - What is it and why do I care? Definition: An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. • Exception is an Object • Exception class must be descendent of Throwable.