2. What is JAVA?
Java is an
object-oriented programming language
developed by James Gosling at Sun
Microsystems and released in 1995.
Old Name : Oak
Green Project (7 members team)
Write Once Run Anywhere (WORA)
5. JAVA Technologies
Applets
Small Java Programs developed for web applications.
Servlets
Server side Java programs
JSP
Java Server Pages
Swing
Developing GUI components using JFC.
6.
EJB
Enterprise Java Beans (Reusable Software Components)
JDBC Java Data Base Connectivity
Struts Framework to create web applications
Hibernate (ORM) (Object Relational Mapping)
To handle databases
Spring (Framework of Frameworks)
Framework to create Java Enterprise applications
19. import java.io.*;
class Test
{
public static void main(String []ar) throws Exception
{
String name;
DataInputStream d = new DataInputStream(System.in);
System.out.println("Enter your Name");
name = d.readLine();
System.out.println(“ Your Name is:"+ name);
}
}
21. import java.io.*;
class Test
{
public static void main(String []ar) throws Exception
{
int age;
BufferedReader d = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter your Age");
age = Integer.parseInt(d.readLine());
System.out.println("Age:"+ age);
}
}
22. import java.util.Scanner;
class Test
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter your rollno");
int rno=sc.nextInt();
System.out.println("Enter your name");
String name=sc.next();
System.out.println("Rollno:"+rno);
System.out.println(" name:"+name);
sc.close();
}
24. import java.util.Scanner;
class Test
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a value");
int a=sc.nextInt();
System.out.println("Enter b value");
int b=sc.nextInt();
int c = a+b;
System.out.println(" Addition:"+c);
sc.close();
}
25. Final, finally, finalizer
• Final
1. Variable (CONSTANT)
final int TOTAL=100;
2. Method (can’t be overridden)
final int add(){}
3. Class (can’t be inherited)
final class Test{ .. }
26. Finally
Exception Handling
What is Exception? Runtime error
Eg:-
ArrayIndexoutofBounds Exception
int a = new int[10];
a[11]=123;
ClassNotFoundException
27. Throws informing the compiler about the
occurrence of an exception (eg:- Questions
without answer)
Try block & Catch block (eg:- Q & A)
Try block (Q: intimation about the occurrence of
an error to java compiler)
Catch block (A: Solution ie remedy for that
exception)
28. import java.io.*; //for DataInputStream class
class Test
{
public static void main(String []ar) throws Exception
{
int age;
DataInputStream d = new DataInputStream(System.in);
System.out.println("Enter your Age");
age = Integer.parseInt(d.readLine()); //IOException
System.out.println("Age:"+ age);
}
}
29. class Test
{
public static void main(String []ar)
{
try
{
int a=100,b=0,c;
c=a/b;
}
catch (Exception e) //ArithmeticException division by zero
{
System.out.println(e);
}
}
}
30. • try block must be followed by a single or
multiple catch block.
• finally block
Is executed once whether the exception is
raised or not.
House keeping operations like file closing &
freeing resources
31. class Test
{
public static void main(String []ar)
{
try
{
int a=100,b=0,c;
c=a/b;
}
catch (Exception e) //ArithmeticException division by zero
{
System.out.println(e);
}
finally
{
System.out.println(“finally block”);
}
}
}
33. Inheritance
• Deriving a new class from existing class.
• Old class or existing class (Super Class)
• New class or derived class (Sub Class)
Reusability
41. Single Inheritance
class A
{
static int x=10;
static int y=20;
}
class B extends A
{
public static void main(String[] args)
{
int z=30;
int res=x+y+z;
System.out.println("Result:"+res);
}
}
43. Interface
Similar to class but contains only declarations of
variables and methods.
• Methods declared in a interface should
be public and abstract
(final Vs abstract)
• variables declared should be public, static &
final
44. Interface
Tagging or Markable interface?
An interface with no variables & methods
eg:-
Serializable, Clonnable and Remote interface
45. Interface pgm
interface P
{
void print();
}
class A implements P
{
public void print()
{ System.out.println("Hello"); }
public static void main(String args[])
{
A obj = new A();
obj.print();
}
}
46. Multiple Inheritance
class A
{
static int x=10;
}
interface B
{
static int y=20;
}
class C extends A implements B
{
public static void main(String[] args)
{
System.out.println("Multiplication:"+ x*y);
}
47. Multiple Inheritance
class A
{ }
class B ________________ A
{}
interface I1
{}
class C ________________ B _________________ I
{}
Interface I2 _____________ I1
{}
x extends y
x implements y
48. Constructor & Destructor
Constructor A method which is invoked
automatically while creating objects.
Constraints:
• Constructor name should be same as class
name.
• No return value for constructor. (even void)
54. Super keyword
• super can be used to refer immediate parent
class instance variable.
• super can be used to invoke immediate
parent class method.
• super() can be used to invoke immediate
parent class constructor.
57. this keywordclass A
{
int a,b;
int add(int x, int y)
{
a=x;
b=y;
return(a+b);
}
}
class B extends A
{
public static void main(String args[])
{
A a=new A();
System.out.println("Added Value:" + a.add(10,20));
}
}
58. class A
{
int x,y;
int add(int x, int y)
{
this.x=x;
this.y=y;
return(x+y);
}
}
class B extends A
{
public static void main(String args[])
{
A a=new A();
System.out.println("Added Value:" + a.add(10,20));
}
}
59. Method Overloading
• Same method name with different signatures.
Signature?
• Number and type of arguments.
int add(int a, int b);
int add(int a, int b, int c);
double add(int a, double b);
60. //Method Overloading
class A
{
int add(int x, int y)
{
return(x+y);
}
int add(int x, int y, int z)
{
return(x+y+z);
}
double add(double x, int y)
{
return(x+y);
}
}
61. class B extends A
{
public static void main(String args[])
{
A a=new A();
System.out.println("Added Value:" + a.add(10,20));
System.out.println("Added Value:" + a.add(10,20,30));
System.out.println("Added Value:" + a.add(1.2,20));
}
}
62. Constructor Overloading
• More than one types of constructor used in a
single class.
• Method Overriding
• Same method name with same signature but in
2 different classes namely super class & sub
class.
Overloading Vs Overriding?
63. // method overriding
class A
{
void show()
{
System.out.println("I am inside super class");
}
}
class B extends A
{
void show()
{
System.out.println("I am inside sub class");
}
}
65. Abstract class
• A class which can’t be instantiated.
Ie we can’t create objects for abstract class.
How to create abstract class in Java?
1.Using abstract class.
2.Using at least one abstract method inside a class.
3.An abstract class contains both abstract & non
abstract methods.
70. • Some Built-in Packages
1) java.lang: defines primitive data types, math
operations
2) java.io: supporting input / output operations.
3) java.util: data structures like Linked List, Date / Time
operations.
4) java.applet: Contains classes for creating Applets.
5) java.awt: graphical user interfaces
6) java.net: networking operations.
71. Advantages of using packages:
• Preventing naming conflicts
• Easier maintenance
• Controlled access
72. User defined package
//create a folder mypack and save Mycls.java in mypack
package mypack;
public class Mycls
{
public void getName(String s)
{
System.out.println(s);
}
}
//compile Mycls.java
73. import mypack.Mycls;
public class Test
{
public static void main(String args[])
{
Mycls m = new Mycls();
m.getName("VIIMS");
}
}
//cd..
//compile Test.java & run
// Output:- VIIMS
74. // Naming conflicts
• import java.util.*;
• import java.sql.*;
Date today ;
//ERROR– java.util.Date or java.sql.Date?
Solution:
java.sql.Date today = new java.sql.Date();
75. APPLETS
• Small java programs developed for web
applications.
console applications Vs Applets
1. main() 1. No main()
2. No browser 2. needs browser or
appletviewer
76. Lifecycle of Java Applet
java.applet.Applet class
• public void init(): is used to initialized the Applet.
It is invoked only once.
• public void start(): is invoked after the init()
method or browser is maximized. It is used to
start the Applet.
• public void stop(): is used to stop the Applet. It is
invoked when Applet is stop or browser is
minimized.
• public void destroy(): is used to destroy the
Applet. It is invoked only once.
78. >javac First.java
Include <applet> tag in 2 ways
1.In java program itself as comment line and run
using appletviewer
>appletviewer First.java
2. As a separate .html file and run using any
browser.
79. First.java
/*
<applet code="First.class" width="300" height="300">
</applet>
*/
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet
{
public void paint(Graphics g)
{
g.drawString("welcome",150,150);
g.drawLine(20,30,20,300);
}
}
81. JDBC (Java Database Connectivity)
• Front End
• Backend
DBMS Vs RDBMS
• ODBC vs JDBC
82. SQL
1. DDL
i. Create
ii. Alter
iii. Drop
2. DML
i. Select
ii. Insert
iii. Update
iv. Delete
3. DCL
i. grant
ii. Revoke
4. TCL
i. Commit
ii. Rollback
iii. Save Point
83. Creating a Tableimport java.sql.*;
class Test
{
public static void main(String a[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =
DriverManager.getConnection("Jdbc:Odbc:stucon");
String qs = "create table student(sname text,rno number)";
PreparedStatement ps = con.prepareStatement(qs);
ps.executeUpdate();
ps.close();
con.close();
}
}
84. Select Queryimport java.sql.*;
class Test
{
public static void main(String a[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("Jdbc:Odbc:empcon");
String qs = "select * from EMPLOYEE";
PreparedStatement ps = con.prepareStatement(qs);
ResultSet rs = ps.executeQuery();
while(rs.next())
{
System.out.println("Employee No :" + rs.getString(1));
System.out.println("Employee Name:" + rs.getString(2));
System.out.println("Salary Rs :" + rs.getString(3));
}
ps.close();
con.close();
}
}