SlideShare a Scribd company logo
JAVA BASIC PART II
SOUMEN SANTRA
MCA, M.Tech, SCJP, MCP
Why World Needs Java ?
Dynamic Web pages.
Platform Independent language.
Replacement of C++.
A collection of more wings.
The new programming language from Sun Microsystems.
Allows anyone to create a web page with Java code in it.
Platform Independent language
Java developer James Gosling, Arthur Van , and others.
Oak, The root of Java.
Java is “C++ -- ++ “.
Sun : Java Features
Simple and Powerful with High Performance.
Object Oriented approach.
Portable and scalable.
Independent Architecture.
Distributed based.
Multi-threaded.
Robust, Secure/Safe, No network connectivity.
Interpreted Source code.
Dynamic programming platform.
Java : OOP
Simple and easily understandable.
Flexible.
Portable
Highly effective & efficient.
Object Oriented.
Compatibility.
High Performance.
Able for Distributed Environments.
Secure.
Java as Best Object Oriented Program
Object is the key of program.
Simple and Familiar: “C++ Lite”
No Pointers overhead to the developer.
Garbage Collector.
Dynamic Binding.
Single & Multilevel Inheritance with “Interfaces”.
Java : JVM
Unicode character.
Unlike other language compilers, Java complier generates code (byte
codes) for Universal Machine.
Java Virtual Machine (JVM): Interprets bytecodes at runtime.
Independent Architecture.
No Linker.
No Loader.
No Preprocessor.
Higher Level Portable Features for GUI : AWT, Applet, Swing.
Total Platform Independence
JAVA COMPILER
JAVA BYTE CODE
JAVA INTERPRETER
Windows 95 Macintosh Solaris Windows NT
(translator)
(same for all platforms)
(one for each different system)
Java
Write Once, Run Anywhere
One Source Code In Any Platform
Java : Architecture
Java Compiler – Name javac, Converts Java source code to bytecode.
Bytecode - Machine representation Intermediate form of Source code &
native code.
JVM-A virtual machine on any target platform interprets the bytecode.
Porting the java system to any new platform involves writing an interpreter
that supports the Java Virtual Machine.
The interpreter will figure out what the equivalent machine dependent
code to run.
Java : High Performance Distributed Computing
JVM uses bytecodes.
Small binary class files.
Just-in-time Compilers.
Multithreading.
Native Methods.
Class Loader.
Lightweight Binary Class Files.
Dynamic.
Good communication constructs.
Secure.
Java : Security
Designed as safe Language.
Strict compiler javac & JIT.
Dynamic Runtime Loading verified by JVM .
Runtime Security Manager.
12
Definition of Java ?
A programming language:
Object oriented (no friends, all functions are members of classes, no function
libraries -- just class libraries).
Simple (no pointer arithmetic, no need for programmer to deallocate
memory).
 Platform Independent.
Dynamic.
Interpreted.
13
Java Data Types
Eight basic types
4 integers (byte, short, int, short) [ example : int a; ]
2 floating point (float, double) [example : double a;]
1 character (char) [example : char a; ]
1 boolean (boolean) [example : boolean a; ]
Everything else is an object
String s;
StringBuffer, StringBuilder, StringTokenizer
14
Java : Class and Object
Declaring a class
class MyClass {
member variables;
…
member functions () ;
…
} // end class MyClass
15
Java Program
Two kinds
Applications
have main() method.
run from the OS prompt (DOS OR SHELL through classpath).
Applets
have init(), start(), stop(), paint(), update(), repaint(), destroy() method.
run from within a web page using HTML <applet> tag.
16
First Java Application
class MyClass {
public static void main(String s [ ] ) {
System.out.println(“Hello World”);
}
} // end class MyClass
17
Declaring and creating objects
Declare a reference
String s;
Create/Define an object
s = new String (“Hello”); Object
Hello
StringPool
18
Java : Arrays
Arrays are objects in Java.
It is a derive object i.e. Integer based or double etc.
A homogeneous contiguous collection of elements of specific datatype.
It’s index starts with 0.
Declaration of Array:
int x [ ] ; // 1-dimensional
int [ ] x ; // 1-dimensional
int [ ] y [ ]; // 2-dimensional
int y [ ][ ];// 2-dimensional
Syntax of Array for allocate space
x = new int [5];
y = new int [5][10];
19
Java : Arrays length
It is used to retrieve the size of an array.
int a [ ] = new int [7]; // 1-dimensional
System.out.println(a.length); //output print ‘7’
int b [ ] [ ] = new int [7] [11];
System.out.println(a.length); //output print ‘7’
System.out.println(b.length * b[0].length); //output print ‘77’
Let int [][][][] array = new int [5][10][12[20] , then …
array.length * array[3rd dimensional].length * array[][2nd dimensional].length *
array[][][1st dimensional].length is 5 x 10 x 12 x 20
20
Java : Constructors
All objects are created through constructors.
Assign Object.
They are invoked automatically.
Return class type.
Mainly public for access anywhere but also private (Singleton class).
Two types: default & parameterize.
class Matter_Weight {
int lb; int oz;
public Matter_Weight (int m, int n ) {
lb = m; oz = n;
}
}
parameterize
21
Java : this keyword
Refer to as “this” local object (object in which it is used).
use:
with an instance variable or method of “this” class.
as a function inside a constructor of “this” class.
as “this” object, when passed as local parameter.
22
Java : this use Example as variable
Refers to “this” object’s data member.
class Rectangle {
int length; int breath;
public Weight (int length, int breath ) {
this. length = length; this. breath = breath;
}
}
23
Java : this use Example as method
Refers to another method of “this” class.
class Rectangle {
public int m1 (int x) {
int a = this.m2(x);
return a;
}
public int m2(int y) {
return y*7 ;
}
}// class close
24
Java : this use Example as function
It must be used with a constructor.
class Rectangle {
int length, breath;
public Rectangle (int l, int b)
{
length = a; breath = b;
}
}
public Rectangle(int m) { this( m, 0); }
}
Constructor is also overloaded
(Java allows overloading of all
methods, including constructors).
25
Java : this use Example as function object,
when passed as parameter
Refers to the object that used to call the calling method.
class MyClass {
int a;
public static void main(String [] s )
{
(new MyClass ()).Method1();
}
public void Method1()
{
Method2(this);
}
public void Method2(MyClass obj)
{ obj.a = 75; }
}
26
Java : static keyword
It means “GLOBAL” for all objects refer to the same storage.
It applies to variables or methods throughout the Program.
use:
with an instance variable of a class.
with a method of a class.
27
Java : static keyword (with variables)
class Order {
private static int OrderCount; // shared by all objects of this class throughout the program
public static void main(String [] s ) {
Order obj1 = new Order();
obj1.updateOdCount();
}
public void updateOdCount()
{
OrderCount++;
}
}
28
Java : static keyword (without methods)
class Math {
public static double sqrt(double m) {
// calculate
return result;
}
}
class MyClass{
public static void main(String [] s ) {
double d;
d = Math.sqrt(9.34);
}
}
29
Java : Inheritance (subclassing)
class Employee {
protected String name;
protected double salary;
public void raise(double dd) {
salary += salary * dd/100;
}
public Employee ( … ) { … }
}
30
Manager acts as sub/derived-class of
Employee
class Manager extends Employee {
private double bonus;
public void setBonus(double bb) {
bonus = salary * bb/100;
}
public Manager ( … ) { … }
}
31
Java : Overriding (methods)
class Manager extends Employee {
private double bonus;
public void setSalary(double bb) { …}
public void cal(double dd) {
salary += salary * dd/100 + bonus;
}
public Manager ( … ) { … }
}
Keyword for Inheritance
32
class First {
public First() { System.out.println(“ First class “); }
}
public class Second extends First {
public Second() { System.out.println(“Second class”); }
}
public class Third extends Second {
public Third() {System.out.println(“Third class”);}
}
Java : Role of Constructors in
Inheritance
First class
Second class
Third class
Topmost class constructor is invoked first
(like us …grandparent-->parent-->child->)
33
Java : Access Modifiers
private
Same class only
public
Everywhere
protected
Same class, Same package, any subclass
(default)
Same class, Same package
34
Java : super keyword
Refers to the superclass (base class)
use:
with a variable or method (most common with a method)
as a function inside a constructor of the subclass
35
Java : super with a method
class Manager extends Employee {
private double bonus;
public void setSalary(double bb) { …}
public void cal(double dd) { //overrides cal() of Employee
super.cal(dd); // call Employee’s cal()
salary += bonus;
}
public Manager ( … ) { … }
}
36
Java : super function inside a constructor of the subclass
class Manager extends Employee {
private double bonus;
public void setSalary(double bb) { …}
public Manager ( String name, double salary, double bonus )
{
super(name, salary);
this.bonus = bonus;
}
}
37
Java : final keyword
It means “constant”.
It applies to
variables (makes a constant variable), or
methods (makes a non-overridable or final method)
or
classes (makes a class non-overridable or final means
“objects cannot be created”).
38
Java : final keyword with a variable
class Math {
public final double pi = 3.141;
public static double cal(double x) {
double x = pi * pi;
}
}
note: variable pi is made “Not Overriden”
39
Java : final keyword with a method
class Employee {
protected String name;
protected double salary;
public final void cal(double dd) {
salary += salary * dd/100;
}
public Employee ( … ) { … }
}
Cannot override final method cal() inside the Manager
class
40
Java : final keyword with a class
final class Employee {
protected String name;
protected double salary;
public void cal(double dd) {
salary += salary * dd/100;
}
public Employee ( … ) { … }
}
Not create class Manager as a subclass of class Employee
(all are equal)
41
Java : abstract classes and interfaces
abstract classes
may have both implemented and non-implemented methods.
interfaces
have only non-implemented methods.
(concrete classes or pure classes)
have all their methods implemented.
42
Java : abstract class
abstract class Figure {
public abstract double area();
public abstract double perimeter();
public abstract void print();
public void setOutColor(Color cc) {
// code to set the color
}
public void setInColor(Color cc) {
// code to set the color
}
}
KEYWORD FOR
ABSTRACT
CLASS
43
Java : interface
interface MouseClick {
public void Down();
public void Up();
public void DoubleClick();
}
class PureClick implements Mouse Click {
// all above methods implemented here
}
KEYWORD FOR
INTERFACE
CLASS
44
Java : Exceptions (error handling)
code without exceptions:
...
int x = 7, y = 0, result;
if ( y != 0) {
result = x/y;
}
else {
System.out.println(“y is zero”);
}
...
code with exceptions:
...
int x = 7, y = 0, result;
try {
result = x/y;
}
catch (ArithmeticException ex )
{
System.out.println(“y is zero”);
}
...
A nice way to handle errors in Java programs
45
Java : Exceptions (try-catch-finally block )
import java.io.*;
class Test{
public static void main(String args[]){
int x = 7, y = 0, result;
try {
result = x/y;
/// more code .. Main operations
}
catch (ArithmeticException ex1 ) {
System.out.println(“y is zero”);
}
catch (IOException ex2 ) {
System.out.println(“Can’t read Format error”);
}
finally {
System.out.println(“Closing file”);
/// code to close file, release memory.
}
}}
KEYWORD FOR
Exception CLASS
Package FOR
Exception
CLASS
46
Java : Using Throwing Exceptions
public int divide (int x, int y ) throws ArithmeticException {
if (y == 0 ) {
throw new ArithmeticException();
}
else {
return x/y ;
}
} // end divide()
KEYWORD FOR
Exception CLASS
47
Java : User Defining exceptions
public int divide (int x, int y ) throws MyException {
if (y == 0 ) {
throw new MyException();
}
else {
return a/b ;
}
} // end divide()
}
import java.io.*;
class MyException extends ArithmeticException
{
KEYWORD FOR
Exception CLASS
Package FOR
Exception
CLASS
inherit
Exception
CLASS
THANK YOU
GIVE FEEDBACK

More Related Content

PPS
Introduction to class in java
kamal kotecha
 
PPTX
Core java complete ppt(note)
arvind pandey
 
PPTX
Class introduction in java
yugandhar vadlamudi
 
PPTX
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
Sagar Verma
 
PDF
Advanced java jee material by KV Rao sir
AVINASH KUMAR
 
PPT
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Sagar Verma
 
PPTX
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Sagar Verma
 
PDF
Object Oriented Programming in PHP
Lorna Mitchell
 
Introduction to class in java
kamal kotecha
 
Core java complete ppt(note)
arvind pandey
 
Class introduction in java
yugandhar vadlamudi
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
Sagar Verma
 
Advanced java jee material by KV Rao sir
AVINASH KUMAR
 
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Sagar Verma
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Sagar Verma
 
Object Oriented Programming in PHP
Lorna Mitchell
 

What's hot (20)

PPTX
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
Sagar Verma
 
PDF
Advance java kvr -satya
Satya Johnny
 
PPT
Object and Classes in Java
backdoor
 
PDF
Java Programming - 05 access control in java
Danairat Thanabodithammachari
 
PDF
Basic java for Android Developer
Nattapong Tonprasert
 
PPTX
Java Programming and J2ME: The Basics
tosine
 
PPTX
Unit3 part3-packages and interfaces
DevaKumari Vijay
 
PPT
Core java Basics
RAMU KOLLI
 
PDF
Java OOP Programming language (Part 5) - Inheritance
OUM SAOKOSAL
 
PDF
Built in classes in java
Mahmoud Ali
 
PDF
Java OOP Programming language (Part 3) - Class and Object
OUM SAOKOSAL
 
PPTX
Python advance
Mukul Kirti Verma
 
PDF
Java Reflection
Hamid Ghorbani
 
PPT
Sonu wiziq
Sonu WIZIQ
 
PPT
Core Java- An advanced review of features
vidyamittal
 
PDF
Java inheritance
Hamid Ghorbani
 
PPTX
Unit3 inheritance
Kalai Selvi
 
PPTX
Lecture 13, 14 & 15 c# cmd let programming and scripting
Wiliam Ferraciolli
 
PPTX
Java class 3
Edureka!
 
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
Sagar Verma
 
Advance java kvr -satya
Satya Johnny
 
Object and Classes in Java
backdoor
 
Java Programming - 05 access control in java
Danairat Thanabodithammachari
 
Basic java for Android Developer
Nattapong Tonprasert
 
Java Programming and J2ME: The Basics
tosine
 
Unit3 part3-packages and interfaces
DevaKumari Vijay
 
Core java Basics
RAMU KOLLI
 
Java OOP Programming language (Part 5) - Inheritance
OUM SAOKOSAL
 
Built in classes in java
Mahmoud Ali
 
Java OOP Programming language (Part 3) - Class and Object
OUM SAOKOSAL
 
Python advance
Mukul Kirti Verma
 
Java Reflection
Hamid Ghorbani
 
Sonu wiziq
Sonu WIZIQ
 
Core Java- An advanced review of features
vidyamittal
 
Java inheritance
Hamid Ghorbani
 
Unit3 inheritance
Kalai Selvi
 
Lecture 13, 14 & 15 c# cmd let programming and scripting
Wiliam Ferraciolli
 
Java class 3
Edureka!
 
Ad

Similar to Java basic part 2 : Datatypes Keywords Features Components Security Exceptions (20)

PPT
basic_java.ppt
sujatha629799
 
PPTX
Complete PPT about the Java lokesh kept it
lokeshpappaka10
 
PPTX
Introduction to JcjfjfjfkuutyuyrsdterdfbvAVA.pptx
patilrohini0224
 
PPTX
Java programming(unit 1)
Dr. SURBHI SAROHA
 
PPTX
JAVA_VR23_OOPS THROUGH JAVA PPT UNIT-1.pptx
netaji10700
 
PDF
JAVA Class Presentation.pdf Vsjsjsnheheh
AnushreeP4
 
PPT
1- java
Krishna Sujeer
 
PPTX
java slides
RizwanTariq18
 
PPT
Core_java_ppt.ppt
SHIBDASDUTTA
 
PPT
Java basic introduction
Ideal Eyes Business College
 
PDF
Basic Java Programming
Math-Circle
 
PDF
OOPs - JAVA Quick Reference.pdf
ArthyR3
 
PPTX
1.introduction to java
Madhura Bhalerao
 
PPTX
Java Programming
Simon Ritter
 
PPTX
objectorientedprogrammingCHAPTER 2 (OOP).pptx
tutorialclassroomhit
 
PPTX
UNIT 1.pptx
EduclentMegasoftel
 
PPTX
Skillwise Elementary Java Programming
Skillwise Group
 
PPT
What is Java Technology (An introduction with comparision of .net coding)
Shaharyar khan
 
PPTX
object oriented programming unit one ppt
isiagnel2
 
basic_java.ppt
sujatha629799
 
Complete PPT about the Java lokesh kept it
lokeshpappaka10
 
Introduction to JcjfjfjfkuutyuyrsdterdfbvAVA.pptx
patilrohini0224
 
Java programming(unit 1)
Dr. SURBHI SAROHA
 
JAVA_VR23_OOPS THROUGH JAVA PPT UNIT-1.pptx
netaji10700
 
JAVA Class Presentation.pdf Vsjsjsnheheh
AnushreeP4
 
java slides
RizwanTariq18
 
Core_java_ppt.ppt
SHIBDASDUTTA
 
Java basic introduction
Ideal Eyes Business College
 
Basic Java Programming
Math-Circle
 
OOPs - JAVA Quick Reference.pdf
ArthyR3
 
1.introduction to java
Madhura Bhalerao
 
Java Programming
Simon Ritter
 
objectorientedprogrammingCHAPTER 2 (OOP).pptx
tutorialclassroomhit
 
UNIT 1.pptx
EduclentMegasoftel
 
Skillwise Elementary Java Programming
Skillwise Group
 
What is Java Technology (An introduction with comparision of .net coding)
Shaharyar khan
 
object oriented programming unit one ppt
isiagnel2
 
Ad

More from Soumen Santra (20)

PDF
Basic and advance idea of Sed and Awk script with examples
Soumen Santra
 
PPT
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Soumen Santra
 
PPTX
Cell hole identification in carcinogenic segment using Geodesic Methodology: ...
Soumen Santra
 
PPTX
PPT_PAPERID 31_SOUMEN_SANTRA - ICCET23.pptx
Soumen Santra
 
PPT
Basic networking hardware: Switch : Router : Hub : Bridge : Gateway : Bus : C...
Soumen Santra
 
DOC
Traveling salesman problem: Game Scheduling Problem Solution: Ant Colony Opti...
Soumen Santra
 
PPT
Optimization techniques: Ant Colony Optimization: Bee Colony Optimization: Tr...
Soumen Santra
 
PPT
Quick Sort
Soumen Santra
 
PPT
Merge sort
Soumen Santra
 
PPTX
A Novel Real Time Home Automation System with Google Assistance Technology
Soumen Santra
 
PPTX
Java Basic PART I
Soumen Santra
 
PPT
Threads Advance in System Administration with Linux
Soumen Santra
 
PPTX
Frequency Division Multiplexing Access (FDMA)
Soumen Santra
 
PPTX
Carrier Sense Multiple Access With Collision Detection (CSMA/CD) Details : Me...
Soumen Santra
 
PPTX
Code-Division Multiple Access (CDMA)
Soumen Santra
 
PPTX
PURE ALOHA : MEDIUM ACCESS CONTROL PROTOCOL (MAC): Definition : Types : Details
Soumen Santra
 
PPTX
Carrier-sense multiple access with collision avoidance CSMA/CA
Soumen Santra
 
PPTX
RFID (RADIO FREQUENCY IDENTIFICATION)
Soumen Santra
 
PPTX
SPACE DIVISION MULTIPLE ACCESS (SDMA) SATELLITE COMMUNICATION
Soumen Santra
 
PPT
Threads Basic : Features, Types & Implementation
Soumen Santra
 
Basic and advance idea of Sed and Awk script with examples
Soumen Santra
 
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Soumen Santra
 
Cell hole identification in carcinogenic segment using Geodesic Methodology: ...
Soumen Santra
 
PPT_PAPERID 31_SOUMEN_SANTRA - ICCET23.pptx
Soumen Santra
 
Basic networking hardware: Switch : Router : Hub : Bridge : Gateway : Bus : C...
Soumen Santra
 
Traveling salesman problem: Game Scheduling Problem Solution: Ant Colony Opti...
Soumen Santra
 
Optimization techniques: Ant Colony Optimization: Bee Colony Optimization: Tr...
Soumen Santra
 
Quick Sort
Soumen Santra
 
Merge sort
Soumen Santra
 
A Novel Real Time Home Automation System with Google Assistance Technology
Soumen Santra
 
Java Basic PART I
Soumen Santra
 
Threads Advance in System Administration with Linux
Soumen Santra
 
Frequency Division Multiplexing Access (FDMA)
Soumen Santra
 
Carrier Sense Multiple Access With Collision Detection (CSMA/CD) Details : Me...
Soumen Santra
 
Code-Division Multiple Access (CDMA)
Soumen Santra
 
PURE ALOHA : MEDIUM ACCESS CONTROL PROTOCOL (MAC): Definition : Types : Details
Soumen Santra
 
Carrier-sense multiple access with collision avoidance CSMA/CA
Soumen Santra
 
RFID (RADIO FREQUENCY IDENTIFICATION)
Soumen Santra
 
SPACE DIVISION MULTIPLE ACCESS (SDMA) SATELLITE COMMUNICATION
Soumen Santra
 
Threads Basic : Features, Types & Implementation
Soumen Santra
 

Recently uploaded (20)

PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
Software Development Company | KodekX
KodekX
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PDF
Beyond Automation: The Role of IoT Sensor Integration in Next-Gen Industries
Rejig Digital
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Software Development Company | KodekX
KodekX
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
Beyond Automation: The Role of IoT Sensor Integration in Next-Gen Industries
Rejig Digital
 

Java basic part 2 : Datatypes Keywords Features Components Security Exceptions

  • 1. JAVA BASIC PART II SOUMEN SANTRA MCA, M.Tech, SCJP, MCP
  • 2. Why World Needs Java ? Dynamic Web pages. Platform Independent language. Replacement of C++. A collection of more wings. The new programming language from Sun Microsystems. Allows anyone to create a web page with Java code in it. Platform Independent language Java developer James Gosling, Arthur Van , and others. Oak, The root of Java. Java is “C++ -- ++ “.
  • 3. Sun : Java Features Simple and Powerful with High Performance. Object Oriented approach. Portable and scalable. Independent Architecture. Distributed based. Multi-threaded. Robust, Secure/Safe, No network connectivity. Interpreted Source code. Dynamic programming platform.
  • 4. Java : OOP Simple and easily understandable. Flexible. Portable Highly effective & efficient. Object Oriented. Compatibility. High Performance. Able for Distributed Environments. Secure.
  • 5. Java as Best Object Oriented Program Object is the key of program. Simple and Familiar: “C++ Lite” No Pointers overhead to the developer. Garbage Collector. Dynamic Binding. Single & Multilevel Inheritance with “Interfaces”.
  • 6. Java : JVM Unicode character. Unlike other language compilers, Java complier generates code (byte codes) for Universal Machine. Java Virtual Machine (JVM): Interprets bytecodes at runtime. Independent Architecture. No Linker. No Loader. No Preprocessor. Higher Level Portable Features for GUI : AWT, Applet, Swing.
  • 7. Total Platform Independence JAVA COMPILER JAVA BYTE CODE JAVA INTERPRETER Windows 95 Macintosh Solaris Windows NT (translator) (same for all platforms) (one for each different system)
  • 8. Java Write Once, Run Anywhere One Source Code In Any Platform
  • 9. Java : Architecture Java Compiler – Name javac, Converts Java source code to bytecode. Bytecode - Machine representation Intermediate form of Source code & native code. JVM-A virtual machine on any target platform interprets the bytecode. Porting the java system to any new platform involves writing an interpreter that supports the Java Virtual Machine. The interpreter will figure out what the equivalent machine dependent code to run.
  • 10. Java : High Performance Distributed Computing JVM uses bytecodes. Small binary class files. Just-in-time Compilers. Multithreading. Native Methods. Class Loader. Lightweight Binary Class Files. Dynamic. Good communication constructs. Secure.
  • 11. Java : Security Designed as safe Language. Strict compiler javac & JIT. Dynamic Runtime Loading verified by JVM . Runtime Security Manager.
  • 12. 12 Definition of Java ? A programming language: Object oriented (no friends, all functions are members of classes, no function libraries -- just class libraries). Simple (no pointer arithmetic, no need for programmer to deallocate memory).  Platform Independent. Dynamic. Interpreted.
  • 13. 13 Java Data Types Eight basic types 4 integers (byte, short, int, short) [ example : int a; ] 2 floating point (float, double) [example : double a;] 1 character (char) [example : char a; ] 1 boolean (boolean) [example : boolean a; ] Everything else is an object String s; StringBuffer, StringBuilder, StringTokenizer
  • 14. 14 Java : Class and Object Declaring a class class MyClass { member variables; … member functions () ; … } // end class MyClass
  • 15. 15 Java Program Two kinds Applications have main() method. run from the OS prompt (DOS OR SHELL through classpath). Applets have init(), start(), stop(), paint(), update(), repaint(), destroy() method. run from within a web page using HTML <applet> tag.
  • 16. 16 First Java Application class MyClass { public static void main(String s [ ] ) { System.out.println(“Hello World”); } } // end class MyClass
  • 17. 17 Declaring and creating objects Declare a reference String s; Create/Define an object s = new String (“Hello”); Object Hello StringPool
  • 18. 18 Java : Arrays Arrays are objects in Java. It is a derive object i.e. Integer based or double etc. A homogeneous contiguous collection of elements of specific datatype. It’s index starts with 0. Declaration of Array: int x [ ] ; // 1-dimensional int [ ] x ; // 1-dimensional int [ ] y [ ]; // 2-dimensional int y [ ][ ];// 2-dimensional Syntax of Array for allocate space x = new int [5]; y = new int [5][10];
  • 19. 19 Java : Arrays length It is used to retrieve the size of an array. int a [ ] = new int [7]; // 1-dimensional System.out.println(a.length); //output print ‘7’ int b [ ] [ ] = new int [7] [11]; System.out.println(a.length); //output print ‘7’ System.out.println(b.length * b[0].length); //output print ‘77’ Let int [][][][] array = new int [5][10][12[20] , then … array.length * array[3rd dimensional].length * array[][2nd dimensional].length * array[][][1st dimensional].length is 5 x 10 x 12 x 20
  • 20. 20 Java : Constructors All objects are created through constructors. Assign Object. They are invoked automatically. Return class type. Mainly public for access anywhere but also private (Singleton class). Two types: default & parameterize. class Matter_Weight { int lb; int oz; public Matter_Weight (int m, int n ) { lb = m; oz = n; } } parameterize
  • 21. 21 Java : this keyword Refer to as “this” local object (object in which it is used). use: with an instance variable or method of “this” class. as a function inside a constructor of “this” class. as “this” object, when passed as local parameter.
  • 22. 22 Java : this use Example as variable Refers to “this” object’s data member. class Rectangle { int length; int breath; public Weight (int length, int breath ) { this. length = length; this. breath = breath; } }
  • 23. 23 Java : this use Example as method Refers to another method of “this” class. class Rectangle { public int m1 (int x) { int a = this.m2(x); return a; } public int m2(int y) { return y*7 ; } }// class close
  • 24. 24 Java : this use Example as function It must be used with a constructor. class Rectangle { int length, breath; public Rectangle (int l, int b) { length = a; breath = b; } } public Rectangle(int m) { this( m, 0); } } Constructor is also overloaded (Java allows overloading of all methods, including constructors).
  • 25. 25 Java : this use Example as function object, when passed as parameter Refers to the object that used to call the calling method. class MyClass { int a; public static void main(String [] s ) { (new MyClass ()).Method1(); } public void Method1() { Method2(this); } public void Method2(MyClass obj) { obj.a = 75; } }
  • 26. 26 Java : static keyword It means “GLOBAL” for all objects refer to the same storage. It applies to variables or methods throughout the Program. use: with an instance variable of a class. with a method of a class.
  • 27. 27 Java : static keyword (with variables) class Order { private static int OrderCount; // shared by all objects of this class throughout the program public static void main(String [] s ) { Order obj1 = new Order(); obj1.updateOdCount(); } public void updateOdCount() { OrderCount++; } }
  • 28. 28 Java : static keyword (without methods) class Math { public static double sqrt(double m) { // calculate return result; } } class MyClass{ public static void main(String [] s ) { double d; d = Math.sqrt(9.34); } }
  • 29. 29 Java : Inheritance (subclassing) class Employee { protected String name; protected double salary; public void raise(double dd) { salary += salary * dd/100; } public Employee ( … ) { … } }
  • 30. 30 Manager acts as sub/derived-class of Employee class Manager extends Employee { private double bonus; public void setBonus(double bb) { bonus = salary * bb/100; } public Manager ( … ) { … } }
  • 31. 31 Java : Overriding (methods) class Manager extends Employee { private double bonus; public void setSalary(double bb) { …} public void cal(double dd) { salary += salary * dd/100 + bonus; } public Manager ( … ) { … } } Keyword for Inheritance
  • 32. 32 class First { public First() { System.out.println(“ First class “); } } public class Second extends First { public Second() { System.out.println(“Second class”); } } public class Third extends Second { public Third() {System.out.println(“Third class”);} } Java : Role of Constructors in Inheritance First class Second class Third class Topmost class constructor is invoked first (like us …grandparent-->parent-->child->)
  • 33. 33 Java : Access Modifiers private Same class only public Everywhere protected Same class, Same package, any subclass (default) Same class, Same package
  • 34. 34 Java : super keyword Refers to the superclass (base class) use: with a variable or method (most common with a method) as a function inside a constructor of the subclass
  • 35. 35 Java : super with a method class Manager extends Employee { private double bonus; public void setSalary(double bb) { …} public void cal(double dd) { //overrides cal() of Employee super.cal(dd); // call Employee’s cal() salary += bonus; } public Manager ( … ) { … } }
  • 36. 36 Java : super function inside a constructor of the subclass class Manager extends Employee { private double bonus; public void setSalary(double bb) { …} public Manager ( String name, double salary, double bonus ) { super(name, salary); this.bonus = bonus; } }
  • 37. 37 Java : final keyword It means “constant”. It applies to variables (makes a constant variable), or methods (makes a non-overridable or final method) or classes (makes a class non-overridable or final means “objects cannot be created”).
  • 38. 38 Java : final keyword with a variable class Math { public final double pi = 3.141; public static double cal(double x) { double x = pi * pi; } } note: variable pi is made “Not Overriden”
  • 39. 39 Java : final keyword with a method class Employee { protected String name; protected double salary; public final void cal(double dd) { salary += salary * dd/100; } public Employee ( … ) { … } } Cannot override final method cal() inside the Manager class
  • 40. 40 Java : final keyword with a class final class Employee { protected String name; protected double salary; public void cal(double dd) { salary += salary * dd/100; } public Employee ( … ) { … } } Not create class Manager as a subclass of class Employee (all are equal)
  • 41. 41 Java : abstract classes and interfaces abstract classes may have both implemented and non-implemented methods. interfaces have only non-implemented methods. (concrete classes or pure classes) have all their methods implemented.
  • 42. 42 Java : abstract class abstract class Figure { public abstract double area(); public abstract double perimeter(); public abstract void print(); public void setOutColor(Color cc) { // code to set the color } public void setInColor(Color cc) { // code to set the color } } KEYWORD FOR ABSTRACT CLASS
  • 43. 43 Java : interface interface MouseClick { public void Down(); public void Up(); public void DoubleClick(); } class PureClick implements Mouse Click { // all above methods implemented here } KEYWORD FOR INTERFACE CLASS
  • 44. 44 Java : Exceptions (error handling) code without exceptions: ... int x = 7, y = 0, result; if ( y != 0) { result = x/y; } else { System.out.println(“y is zero”); } ... code with exceptions: ... int x = 7, y = 0, result; try { result = x/y; } catch (ArithmeticException ex ) { System.out.println(“y is zero”); } ... A nice way to handle errors in Java programs
  • 45. 45 Java : Exceptions (try-catch-finally block ) import java.io.*; class Test{ public static void main(String args[]){ int x = 7, y = 0, result; try { result = x/y; /// more code .. Main operations } catch (ArithmeticException ex1 ) { System.out.println(“y is zero”); } catch (IOException ex2 ) { System.out.println(“Can’t read Format error”); } finally { System.out.println(“Closing file”); /// code to close file, release memory. } }} KEYWORD FOR Exception CLASS Package FOR Exception CLASS
  • 46. 46 Java : Using Throwing Exceptions public int divide (int x, int y ) throws ArithmeticException { if (y == 0 ) { throw new ArithmeticException(); } else { return x/y ; } } // end divide() KEYWORD FOR Exception CLASS
  • 47. 47 Java : User Defining exceptions public int divide (int x, int y ) throws MyException { if (y == 0 ) { throw new MyException(); } else { return a/b ; } } // end divide() } import java.io.*; class MyException extends ArithmeticException { KEYWORD FOR Exception CLASS Package FOR Exception CLASS inherit Exception CLASS

Editor's Notes