Core Java: Presented by S.Anitha Ap/Mca Viims
Core Java: Presented by S.Anitha Ap/Mca Viims
Presented By
S.Anitha
AP/MCA
VIIMS
What is JAVA?
Java is an object-oriented programming
language developed by James Gosling at Sun
Microsystems and released in 1995.
System.out.println(“Welcome”);
}
}
How to run a JAVA pgm?
>javac Test.java
o/p: Test.class
>java Test
o/p: Welcome
JDK, BDK, JSDK, JRE, JIT, Hot Java
Different name for java file and class
Sample.java
class Test
{
public static void main(String []a)
{
System.out.println(“Welcome”);
}
}
How to run a JAVA pgm?
>javac Sample.java
o/p: Test.class
>java Test
Welcome
Comment Line
1. Single Line Comment //
3.Documentation Comment
/** */
Getting Input
1. DataInputStream
2. BufferedReader
3. Scanner
import java.io.*;
class Test
{
public static void main(String []ar) throws Exception
{
int age;
System.out.println("Age:"+ age);
}
}
DataInputStram
Output
D:\jpgms>javac sample.java
Note: sample.java uses or overrides a deprecated
API.
Note: Recompile with -Xlint:deprecation for details.
D:\jpgms>java Test
Enter your Age
20
Age:20
import java.io.*;
class Test
{
public static void main(String []ar) throws Exception
{
int age;
System.out.println("Age:"+ age);
}
}
import java.util.Scanner;
class Test
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Rollno:"+rno);
System.out.println(" name:"+name);
sc.close();
}
Final, finally, finalizer
• Final
1. Variable (CONSTANT)
final int TOTAL=100;
2. Method (can’t be override)
final int add(){}
3. Class (can’t inherited)
final class Test{ .. }
Finally
Exception Handling
ClassNotFoundException
Throws informing the compiler about the
occurrence of an exception (eg:- Questions
without answer)
System.out.println("Age:"+ age);
}
}
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);
}
}
}
• try block must be followed by a single or
multiple catch block.
• finally block
Is executed once whether the exception is
raised or not.
Reusability
Types of Inheritance
1. Single Inheritance
2. Multiple Inheritance
3. Hierarchical inheritance
4.MultiLevel Inheritance
5.Hybrid inheritance
1.Single Inheritance
Father Mother
Son
Java doesn’t support Multiple
Inheritance.
?
But we can do Multiple
Inheritance by using interfaces.
3. Hierarchical Inheritance
(More than one Sub class)
Mobile OS
Parent
Child
5. Hybrid Inheritance
(combination of all types of inheritance)
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);
}
}
class Shape
{ int l,b;
}
eg:-
Serializable, Clonnable and Remote interface
Interface pgm
interface P
{
void print();
}
class A implements P
{
public void print()
{ System.out.println("Hello"); }
interface I1
{}
Constraints:
• Constructor name should be same as class
name.
• No return value for constructor. (even void)
Types of Constructor:
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
4. Dynamic Constructor
class Student
Default constructor
{
int rno;
String name;
void display()
{
System.out.println("Name:"+name);
System.out.println("RollNO:"+rno);
}
public static void main(String args[])
{
Student s1=new Student();//default constructor
s1.display();
}
}
2. Parameterized Constructor
class Student
{
int rno;
String name;
Student(int r,String s)
{
rno=r;
name=s;
}
void display()
{
System.out.println("Name:"+name);
System.out.println("RollNO:"+rno);
}
public static void main(String args[])
{
Student s1=new Student(101,"Sudha");//parameterized constructor
s1.display();
}
Copy Constructor
class Student
{
int rno;
String name;
Student(int r,String s)
{
rno=r;
name=s;
}
class B extends A
{
public static void main(String args[])
{
A a=new A();
System.out.println("Added Value:" + a.add(10,20));
}
}
Method Overloading
• Same method name with different signatures.
Signature?
• Number and type of arguments.
}
}
Constructor Overloading
• More than one types of constructor used in a
single class.
• Method Overriding
Overloading Vs Overriding?
// 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");
}
}
class Test
{
public static void main(String a[])
{
B b = new B();
b.show();
}
}
Abstract class
• A class which can’t be instantiated.
Ie we can’t create objects for abstract class.
}
}
Package
• grouping of related classes, Interfaces,
methods.
• Easier maintenance
• Controlled access
User defined package
//create a folder mypack and save Mycls.java in mypack
package mypack;
m.getName("VIIMS");
}
}
//cd..
//compile Test.java & run
// Output:- VIIMS
//static keyword
import static java.lang.System.*;
class Test
{
public static void main(String b[])
{
out.println("Welcome");
}
}
// 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();
APPLETS
• Small java programs developed for web
applications.
}
>javac First.java
Include <applet> tag in 2 ways
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);
}
}
test.html (use any browser)
<html>
<body>
<applet code="First.class" width="300"
height="300">
</applet>
</body>
</html>
JDBC (Java Database Connectivity)
• Front End
• Backend
DBMS Vs RDBMS
• ODBC vs JDBC
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
import java.sql.*;
Creating a Table
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();
}
}
import java.sql.*;
Select Query
class Test
{
public static void main(String a[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
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();
}
}
Thank You