Professional Documents
Culture Documents
--------------------
Electronic Commerce -> Business thru Internet
Three Types
------------------
1. Business to Business (B2B) (Intel Corporation <-> HCL)
2. Business to Customer (B2C) (Chennaisilks.com)
3. Customer to Customer. (C2C) (Bazee.com) (Secondsales)
Web Technology
------------------------
Designing -> SGML, HTML, DHTML, Frontpage, Dreamweaver
Client side scripting -> Javascript, VBScript, JScript
Server Side scripting -> Servlets, ASP.NET, JSP
Storage DBMS(Database Management System)-> Oracle, SQL - Server, Access
RDBMS
B2B
Markup Language
---------------------
1. Formatting Content
2. Describing Content
disadvantage
--------------
Execution speed is slow because of using bytecode
Architecture
------------------
Presentation layer -> Input and Output forms
Business logic -> Validations
Data layer -> Storing Data
Java Programming
--------------------------
Application (Console)
Control Statements
Arrays and Strings
Class Concepts
Inheritance
Interface and package
Exception Handling
Streams
Thread
Networking
JDBC
Applet
Graphics
AWT(Abstract Window Toolkit) Components (Heavy Weight Components)
Menu
Frame
Panel
JFC/Swing --> Java Foundation Class (Light Weight Components -> ie, the components
are developed by using Java)
d:\>set path=%path%;c:\jdk1.3\bin;
d:\>path
d:\>notepad first.java
sample program
-------------------
class first {
public static void main(String args[]) {
System.out.println("My First Java Program");
}
}
Note:
-----
Java program must be within a class
java class name and the filename may be same
System - Class
out -Object of printStream
println - Method of printstream
System.out.println("Welcome to Java Programming");
System.out.print("Hello World");
Datatypes
---------
primitive datatypes
-------------------
char
float
long
double
int
boolean
Integer
-------
Type Size Range
---- ----- ------
byte 1 byte -128 to +127
short 2 bytes -32,768 to +32,767
int 4 bytes -2,147,483,648 to +2,147,483,647
long 8 bytes -9223372036854775808 to
+9223372036854775807
character
----------
char 2 bytes unicode characters. (Universal Code)
(ASCII+Non Ascii)
a,b,c,d some special symbols
boolean
--------
boolean 1 byte true or false
float
-----
float 4 bytes - 3.4e^38 to + 3.4e^38
double 8 bytes -1.7e^308 to +1.7e^308
Refence datatype:
String s1="Adroit";
userdefined datatypes
class
arrays
enums
variables
-----------
which holds some values
Instance variables are used to define attributes or the state of particular object.
class variables are global to a class and to all the instances of the class
Declaring variables
--------------------
datatype var_name;
Naming Conventions
----------------------------
var_name must be starting with character
special symbols are not allowed
white spaces are not allowed
only underscore allowed.
variable must be unique
int a;
int emp_no;
int emp no; //error
int e1;
int 1e; //error
char s$; //error
eg:
---
int a;
short b;
float f;
char c; (Variables)
String str; (Instance)
Eg:
----
class VarDecl
{
static void pr(String s)
{
System.out.println(s);
}
public static void main(String args[])
{
short a = 10;
float b = 3.14f;
char c = 'a';
boolean d = true;
String str = "Hello";
// VarDecl v = new VarDecl();
Operators
--------------
Arithmetic + - * / %
Relational >, <, >=, <=, ==, !=
Logical &&, ||, !
Increment and Decrement ++,--
Conditional ?:
Assignment =
Bitwise &, |, ^, <<, >>
Arithmetic Assignment +=, -=, *=, /=, %=
Bitwise
--------------
& And
-----------
A B C
0 0 0
0 1 0
1 0 0
1 1 1
a = 5 0101
b = 4 0100
0100 = 4
| OR
------
A B C
0 0 0
0 1 1
1 0 1
1 1 1
a = 5 0101
b = 4 0100
c = a | b 0101 = 5
^ X - OR
--------------
A B C
0 0 0
0 1 1
1 0 1
1 1 0
a = 5 0101
b = 4 0100
c = a ^ b 0001 = 1
Programming Constructs
---------------------------------
types
-------------
1. Conditional
Conditional
----------------
if statements
switch case
while loop while(true) while(1)
do while
for for(;;)
break;
continue;
if statement
---------------
if (condition)
{
statements
}
if..else
---------
if (condition)
{
true statements
}
else
{
false statements
}
if..else if
--------------
if (condition)
{
true statements
} else if(condition)
{
true statements
}
else
{
false statements;
}
nested if
-----------
if (condition) {
if (condition) {
true statements
} else
{
false statements
}
}
else
{
false statements
}
Looping
-----------
while loop
-------------
while (condition)
{
statements
}
do while
----------
do
{
statements
}while(condition);
for loop
-----------
for(initialization;condition; inc or dec)
{
statements
}
switch case
---------------
switch(expression)
{
case 1:
statements;
break;
case n:
statements;
break;
default:
statements;
}
Integer.parseInt()
Float.parseFloat()
Double.parseDouble()
Eg:
----
class CmdLine
{
public static void main(String args[])
{
if (args.length < 2)
{
System.out.println("Invalid Command Line Arguments");
System.exit(0);
}
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
Eg:
-----
class CmdLine1
{
public static void main(String args[])
{
for(int i=0;i<args.length;i++)
{
System.out.println(args[i]);
}
}
}
Factorial
Fibonacci Series
Armstrong Number
Sum of Number (123 = 1+2+3 = 6)
OOPS Concept
--------------------
Object Oriented Programming System (Simula 67)
Five Characteristics
-------------------------
1. Classes and Objects
2. Overloading
3. Inheritance
4. Polymorphism
5. Data Abstraction and Data Encapsulation.
Object (or) Instance -> To access the class member variable and functions.
Syntax:
----------
class class_name
{
access_specifier datatype var_name;
access_specifier return_type function_name(arguments);
}
ins.memberfunction();
Access Specifier
-------------------
private -> With in a class
public -> Class, Subclass and main function(thru object)
protected -> Class and Subclass
no modifier -> similar to public ( Differed in package concept).
int add(int,int);
class rect
{
int len,bre;
Eg:
-----
class rectangle
{
private int len,bre;
4. Function overloading
-------------------------------
Function name similar but passing arguments are different.
Eg:
-----
class exam
{
private int m1,m2,total;
void calculate()
{
total = m1 + m2;
}
void disp()
{
System.out.println("Mark1 : " + m1 + "\t Mark2 " + m2 + "\t Total "
+ total);
}
5. Constructor
6. Finalizer
7. Static methods and variables.
8. Final keyword
9. Abstract Class.
Static Methods
-------------------
* If a method is declared as static, without creating object the method is
accessed.
* static methods are accessed only static variables
* Normal methods are access both static and normal variables.
Eg:
----
class Rect
{
int length;
int breadth;
int area;
Rect()
{
length = 0;
breadth = 0;
count++;
}
void calc()
{
area = length * breadth;
}
void display()
{
System.out.println("Length : " + length);
System.out.println("Breadth : " + breadth);
System.out.println("Area : " + area);
}
}
class Rect1
{
public static void main(String args[])
{
System.out.println("No of object : " + Rect.count);
Rect r1 = new Rect(10,20);
r1.calc();
System.out.println("No of object : " + Rect.count);
Rect r2 = new Rect();
r2.calc();
System.out.println("No of object : " + Rect.count);
r1.display();
r2.display();
}
}
Eg:
----
class Rectangle
{
int length;
int breadth;
private static int count;
Rectangle(int a,int b)
{
length = a;
breadth = b;
count++;
}
Rectangle1()
{
length = 0;
breadth = 0;
count++;
}
void display()
{
System.out.println("Length : " + length);
System.out.println("Breadth : " + breadth);
}
this keyword
----------------
refers current object.
Eg:
-----
class point
{
private int x,y;
Eg:
-----
class load
{
String firstname;
String lastname;
int age;
String profession;
void print()
{
System.out.println(firstname + " " + lastname + " " + age + " " +
profession);
}
Wrapper Classes
--------------------------
Integer.parseInt()
Float.parseFloat()
Double.parseDouble()
Byte.parseByte();
Constructor
----------------
To initialize the object.
Garbage Collector -> Collect or Destroy the unused object.
syntax:
-----------
class class_name
{
class_name()
{
statements;
}
}
eg
----
import java.io.*;
class Const1
{
private int sno;
private String sname;
Const1()
{
System.out.println("Constructor Called");
sno = 0;
sname = "";
}
void getdetails() throws IOException
{
DataInputStream br = new DataInputStream(System.in));
String line;
System.out.print("Enter Sno : ");
line = br.readLine();
sno = Integer.parseInt(line);
System.out.print("Enter Sname : ");
sname = br.readLine();
}
void putdetails()
{
System.out.println("Sno : " + sno);
System.out.println("Sname : " + sname);
}
Overloading Constructor
---------------------------------
Constructor name are similar, but the passing arguments are different.
Default Constructor
----------------------------
classname() {}
Copy Constructor
------------------------
An object is initialized with another object.
exam(exam e1)
{
exam e1(10,20),e2(e1);
eg
----
import java.io.*;
class students
{
private int sno;
private String sname;
private int mark1,mark2,total;
void putstud()
{
System.out.println("Sno : " +
sno);
System.out.println("Sname : "
+ sname);
System.out.println("Mark1 : "
+ mark1);
System.out.println("Mark2 : "
+ mark2);
System.out.println("Total : " + total);
}
eg
----
class marks
{
private int mark1,mark2,total;
marks() {}
marks(int m1,int m2)
{
mark1 = m1;
mark2 = m2;
}
marks(marks m1)
{
mark1 = m1.mark1;
mark2 = m1.mark2;
}
void calc()
{
total = mark1 + mark2;
}
void putdetails()
{
System.out.println("Mark1 : " + mark1);
System.out.println("Mark2 : " + mark2);
System.out.println("Total : " + total);
}
public static void main(String args[])
{
marks m1 = new marks(90,80);
marks m2 = new marks(m1);
marks m3 = new marks();
m1.calc();
m2.calc();
m1.putdetails();
m2.putdetails();
m3.putdetails();
}
}
Finalizers
------------
To destroy the unused object.
Garbage Collector -> The garbage collector will destroy the unused object
automatically. There is no need for finalizers
Arrays
-----------
Collection of like Data Types
two types
------------
1. single dimension
2. multi dimension
single dimension
-------------------------
Syntax:
------------
datatype var[] = new datatype[size];
Eg:
-----
int a[] = new int[10];
Initialization
------------------
int a[5] = {1,2,3,4,5};
a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 5
Eg:
-----
import java.io.*;
class Arr1
{
public static void main(String args[]) throws Exception
{
int a[] = new int[10];
int i,n;
System.out.print("Enter the value of a n : ");
DataInputStream din = new DataInputStream(System.in);
n = Integer.parseInt(din.readLine());
for(i=0;i<n;i++)
{
System.out.print("Enter a[" + i + "] : ");
a[i] = Integer.parseInt(din.readLine());
}
System.out.println("The Given Array values");
for(i=0;i<n;i++)
System.out.println(a[i]);
}
}
Eg:
-----
import java.io.*;
class Arr1
{
public static void main(String args[]) throws Exception
{
int a[] = new int[10];
int i,n,j,temp;
System.out.print("Enter the value of a n : ");
DataInputStream din = new DataInputStream(System.in);
n = Integer.parseInt(din.readLine());
for(i=0;i<n;i++)
{
System.out.print("Enter a[" + i + "] : ");
a[i] = Integer.parseInt(din.readLine());
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.println("The Sorted Array values");
for(i=0;i<n;i++)
System.out.println(a[i]);
}
}
MultiDimension
-----------------------
* Java doesn't support multidimensional array.
* However an array of Array is used.
int a[] = new int[3];
a[0] = new int[3]; (a(0,0),(0,1),(0,2))
a[1] = new int[3]; (a(1,0),(1,1),(1,2))
a[2] = new int[3] (a(2,0),(2,1),(2,2))
Syntax:
---------
datatype var[][] = new datatype[row][col];
Eg:
----
int a[][] = new int[3][3];
Eg:
-----
import java.io.*;
class Matrix1
{
public static void main(String args[]) throws Exception
{
int a[][] = new int[5][5];
int i,j,row,col;
DataInputStream din = new DataInputStream(System.in);
System.out.print("Enter row and column value : ");
row = Integer.parseInt(din.readLine());
col = Integer.parseInt(din.readLine());
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
System.out.print("Enter a[" + i + "][" + j + "] : ");
a[i][j] = Integer.parseInt(din.readLine());
}
}
String Class
---------------
Collection of characters
String constructors
-------------------
default constructor
-------------------
1. String s=new String()
char cs[]={'a','b','c'};
2. String s = new String(cs);
char cs[]={'a','b','c','d','e','f'};
String s=new String(cs,2,3); cde
Hash Table
-----------
A hash table stores information using a special calculation on the object to be
stored. A hash code is produced as a result of a calculation. The hash code is used
to choose the location in which to store the object. When the information needs to
retrieved, the same calculations is performed, the hash code is determined and a
lookup of that location in the table results in the value that was stored there
previously.
String Arithmetic
----------------
'+' - Concatenation
String toString() - convert into String
+= Operator will also work for strings.
Str1+=Str2; (str1=str1+str2)
str1.equals(str2) = false
str1.equalsIgnoreCase(str2) = true
startsWith() - Boolean result indicates if the string starts with the argument
String s1="hello"
s1.startsWith("hE");=false
s1.startsWith("h"); = true
endsWith() - Boolean result indicates if the string ends with the argument
String s1="Hello";
s1.endsWith("o"); - True
s1.endsWith("h");- false
replace() - Returns a new String object with the replacements made. uses the old
string if no matches found.
replace('Oldchar','newchar')
String s1="hello"
s1.replace('l','w') = hewwo
String s2 = "hello";
s2.toUpperCase();
trim() - Returns a new string object with the Whitespace removed from each end.
Eg:
----
class Arith {
String fname="Aswath";
String lname="Narayanan";
void show() {
System.out.println("The fullname is " + fname + " " + lname);
}
public static void main(String args[]) {
Arith a1=new Arith();
a1.show();
}
}
Eg:
----
class hash {
public static void main(String args[]) {
String s1="world";
String s2="Hello";
System.out.println("The hash code for " + s1 + " is " + s1.hashCode());
System.out.println("The hash code for " + s2 + " is " + s2.hashCode());
}
}
Eg:
----
class equaldemo {
public static void main(String args[]) {
String s1="Hello";
String s2="Hello";
String s3="Good bye";
String s4="HELLO";
Eg:
-----
class StringMeth
{
public static void main(String args[])
{
String str1 = "Hello";
String str2 = "Hello";
String str3 = "This is a sample string for indexOf method";
String str4 = " World ";
System.out.println("Length : " + str1.length());
System.out.println("charAt(4) : " + str1.charAt(4));
System.out.println("To a Character Array : ");
char c[] = new char[str1.length() + 3];
c[0] = 'a';
c[1] = 'b';
c[2] = 'c';
str1.getChars(0,str1.length(),c,3);
for(int i=0;i<c.length;i++)
System.out.print(c[i] + " ");
System.out.println();
StringBuffer
-------------
StringBuffer is a peerclass of string that provides much common use
functionality of strings. String represents fixed-length-character sequences.
Stringbuffer represents varied length character sequences. StringBuffer may have
characters and substrings inserted in the middle or appended at the end. The
compiler automatically creates a stringbuffer to evaluate certain expressions, in
particular when the overloaded operators + and += operator are used with the string
objects.
Constructor
--------------------
StringBuffer sb = new StringBuffer();
String s;
StringBuffer sb = new StringBuffer(s);
StringBuffer Methods
-----------------------
toString() - Creates a string from this stringbuffer or convert to string
boolean ensureCapacity() - makes the stringbuffer hold atleast the desired no. of
spaces.
getChars() - copy chars into an external array. There isno getBytes() as in string
append() - The argument is converted to a string and appended at the end of the
current buffer
insert() - The second arg is converted to a string and inserted into the current
buffer beginning at the offset.
Eg:
-----
class StringBuf
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello World");
System.out.println(sb + ".capacity() = " + sb.capacity());
System.out.println(sb + ".length() = " + sb.length());
System.out.println(sb + ".insert(5,aaa) = " + sb.insert(5,"aaa"));
System.out.println(sb + ".capacity() = " + sb.capacity());
System.out.println(sb + ".length() = " + sb.length());
System.out.println(sb + ".append(bbbb) = " + sb.append("bbbb"));
System.out.println(sb + ".reverse() = " + sb.reverse());
}
}
Inheritance
--------------
* Reusability of code
* Extensibility of code.
Types of inheritance
--------------------------------
1. Single
2. Multiple -> Java Does'nt support
3. Multilevel
4. Heirarchical
5. Hybrid -> Doesn't Support
Single
----------
Base
|
Derived
Multiple
------------
Base1 Base2
| |
---------------------------
|
Derived1
Multilevel
--------------
Base1
|
Derived1
|
Derived2
Heirarchical
----------------
Base
|
-----------------------------------------
| |
Derived1 Derived2
| |
-------------------------- -------------------------------
| | | |
Derived11 Derived12 Derived21 Derived22
Hybrid
----------
Combination of Multiple and Multilevel
Base
|
Derived1 Base2
| |
| |
-------------------------------
|
Derived2
Single Inheritance
----------------------------
Base
|
Derived
Syntax:
-----------
class base
{
statements;
}
class derived extends base
{
statements;
}
Note:
---------
* Private members are not inherited
* public, protected and no modifier members are inherited
* Constructor and finalizer are not inherited.
Eg:
-----
class employee
{
private int eno;
private String ename;
Eg:
------
class emp1
{
private int eno;
private String ename;
Syntax:
--------------
class base
{
statements;
}
class derived1 extends base
{
statements;
}
class derived2 extends derived1
{
statements;
}
Eg:
-----
class students
{
private int sno;
private String sname;
Eg:
-----
//multilevel with constructor
class students1
{
private int sno;
private String sname;
Abstract Class
----------------
Set of complete and Incomplete methods.
Eg:
----
abstract class baseclass
{
public void basedisp() //complete method
{
System.out.println("Base class Display method");
}
public abstract void calc(int param); //incomplete method
}
2. class and method is declared using abstract 2. interface keyword is used for
keyword declaration
Syntax
---------
access_specifier interface interface_name {
return_type method-name1(parameter list);
return_type method-name2(parameter list);
type final-varname1=value;
type final-varname2=value;
.....
return_type method-nameN(parameter list);
type final-varnameN=value;
}
Implementing interface
-----------------------
access class class_name[extends superclass]
[implements interface[interface...]] {
//class body
}
Eg:
-----
Callback.java
----------------------
interface Callback
{
public void call(int param);
}
Client.java
----------------
class Client implements Callback
{
public void call(int p)
{
System.out.println("Interface Method = " + p);
}
public void nonIfaceMeth()
{
System.out.println("Non Interface method is also used");
}
Eg:
-----
AnotherClient.java
----------------------------
class AnotherClient implements Callback
{
public void call(int p)
{
System.out.println("Square Value is : " + (p*p));
}
Eg:
-----
Stack-> Last In First Out
IntStack.java
--------------------
interface IntStack
{
void push(int item);
int pop();
}
FixedStack.java
-------------------------
class FixedStack implements IntStack {
private int stack[];
private int tos;
FixedStack(int size)
{
stack=new int[size];
tos=-1;
}
System.out.println("stack in mystack1");
for(int i=0;i<6;i++)
System.out.println(mystack1.pop());
System.out.println("stack in mystack2");
for(int i=0;i<9;i++)
System.out.println(mystack2.pop());
}
}
Packages
-------------
Collection of Classes and interfaces placed under a folder.
Predefined Packages
-----------------------
import java.io.*
import java.util.Date
import java.util.Vector
import java.sql.*
import java.swing.*
import java.awt.*;
Userdefined packages
--------------------------
package package_name;
class
{
}
javac -d . filename.java
Same Package
Sub Class No Yes Yes Yes
Same Package
Non Subclass No Yes No Yes
Different Package
Subclass No Yes Yes No
Different Package
Non Subclass No Yes No No
Protection.java
----------------------
package p1;
public Protection()
{
System.out.println("Base Constructor");
System.out.println("n = " + n);
System.out.println("n_pri = " + n_pri);
System.out.println("n_pub = " + n_pub);
System.out.println("n_pro = " + n_pro);
}
}
javac -d . Protection.java
Derived.java
-------------------
package p1;
javac -d . Derived.java
SamePackage.java
-----------------------
package p1;
javac -d . SamePackage.java
Demo.java
---------------
import p1.Protection;
import p1.Derived;
import p1.SamePackage;
class Demo
{
public static void main(String args[])
{
Protection ob1 = new Protection();
Derived ob2 = new Derived();
SamePackage ob3 = new SamePackage();
}
}
javac Demo.java
Protection2.java
----------------------
package p2;
javac -d . Protection2.java
OtherPackage.java
----------------------
package p2;
javac -d . OtherPackage.java
Demo1.java
------------------
import p2.Protection2;
import p2.OtherPackage;
class Demo1
{
public static void main(String args[])
{
Protection2 ob1 = new Protection2();
OtherPackage ob2 = new OtherPackage();
}
}
javac Demo1.java
Advantages
---------------------
1. Class Files are placed in a single directory
2. The packages are used in anywhere.