Professional Documents
Culture Documents
Design Pattern
Mayur V. Shrirame
(mayurshrirame@gmail.com)
• Reusability
• eatBurger(void);
• eatBurger(withCoke);
• eatBurger(withSnacks);
Simple
Object Oriented language
Distributed
Interpreted
Robust & Secure
Portable
Multi-threaded
Garbage Collection
JDK
Sample Sample
.java javac.exe .class
JRE
OS
Sample JIT
.class Compiler
class SampleData
1) public keyword is used so that every
{ class can access main() function.
public static void main(String[] args) 2) static keyword is used because we are
{ not accessing it with object reference.
3) String[] args is an array of string used for
command line arguments.
System.out.println (“Java language is easy”);
}
1) System is a predefined class name.
2) out is a static reference variable of System
class.
} 3) println() is a function to print desired
output.
Predefined Keywords
Narrowing
Widening
float
long
• Error is removed.
float a=3.2f • But data is lost.
int b=(int)a; • ‘b’ value will be 3. int
short
float f=3.5; ------ //error
float f=3.5f; ----- //no error byte
Command Line Arguments
You must
public class Dog follow my
{ orders….
("The age of " + d1.breed +" is " + d1.age); 3) Stack is faster for accessing local variables.
}
4) Variables stored onto heap are accessible to whole class
}
whereas variables stored onto stack are accessible to
respective method only.
Static Keyword
Variable Method
Class A
Class B Class C
Class B
2
Output
4
Simple Inheritance
class Employee{
float salary=40000;
}
• Method overloading only occurs in same class. • Method overriding occurs in different class.
• A method is overloaded when it has same • A method is said to be overridden when it has
method name and same return type but same name, same parameter list and same return
different parameters. type.
class Vehicle
{
void run()
{
System.out.println("Vehicle is running");}
}
void run()
{
System.out.println("Bike is running safely");
}
public static void main(String args[])
{
Bike2 obj = new Bike2();
obj.run();
}
}
Source: Google Images
final Keyword
class Child extends Parent
{
class Parent void area(int l, int b)
{ {
final int length=4; System.out.println("Area of rectangle is: "+(l*b));
final void area(int l, int b) }
{ public static void main(String[] args)
System.out.println("Area of rectangle is: "+(l*b)); {
} Parent rectangle=new Child();
} rectangle.area(4,5);
Rectangle.length=5;
}
}
3
4
Upcasting & Downcasting
instanceof Operator
class Dog
{
public static void main(String[] args)
{
Dog d1 = new Dog();
if ( d1 instanceof Dog)
{ • In java, instanceof operator is used to check
System.out.println("Yes it’s working"); the type of an object at runtime.
}
} • instanceof operator returns Boolean value, if
} an object reference is of specified type then it
returns true otherwise false.
Output:-
Yes it’s working
class Try
{
public void add()
{ Downcasting
int c=0;
System.out.println(c);
}
}
public class Try1 extends Try
{
public static void main(String[] args)
{
Try1 t1 = (Try1) new Try();
t1.add();
}
}
C:\Users\Mayur\Documents\Java>java Try1
Exception in thread "main" java.lang.ClassCastException: Try cannot be
cast to Try1
at Try1.main(Try1.java:15)
Access Modifiers
Package 1 Package 2
Rule of Thumb:-
1) Use public if the field is to be visible everywhere.
2) Use protected if the field is to be visible everywhere in the current package and also
subclasses in other packages.
3) Use default if the field is to be visible everywhere inside package only.
4) Use private if the field is not to be visible anywhere except in its own class.
1 3
4
Arrays
datatype[] variable_name;
datatype []variable_name; Declaring an array
datatype variable_name[];
class Student{
public static void main(String[] args)
{
int []studentArray = new int[5]; // declaring and allocating memory to array
studentArray[0]=12;
studentArray[1]=34;
studentArray[2]=45;
studentArray[3]=56;
studentArray[4]=67;
class Test
{
public static void main(String args[])
{
int arr[] = {3, 1, 2, 5, 4};
sum(arr);
}
Output:-
C:\Users\Mayur\Documents\Java>javac Try.java
Try1 t2 = new Try();
t2.add(); 2 C:\Users\Mayur\Documents\Java>java Try
t2.multiply(4,5); ADD Method called
} 20 2
}
String and StringBuffer class
char charArray[] = new char[6];
charArray[0]= ‘F’;
charArray[1]= ‘R’;
charArray[2]= ‘I’;
charArray[3]= ‘E’;
charArray[4]= ‘N’;
charArray[5]= ‘D’;
• To declare:-
String stringName;
stringName = new String (“Happy Friendship Day”);
Creating String Object
• Using a string literal
String str1= “Hello”;
• Each time you create a String literal, the JVM checks the string pool
first.
• If the string literal already exists in the pool, a reference to the pool
instance is returned. If string does not exist in the pool, a new string
object is created, and is placed in the pool.
• When we create a new string object using string literal, that string
literal is added to the string pool, if it is not present there already.
Bit Mesra
str1
String object
Heap
class BitMesra
{
public static void main(String[] args)
{
String a = new String("Hello");
String c ="Hello";
System.out.println(a.equals(c));
System.out.println(a==c);
}
}
Output:-
true
false
Source: Studytonight.com
class BitMesra
{
public static void main(String[] args)
{
String a = new String("Hello");
a.concat("bit");
a = a.concat("bit");
System.out.println(a);
}
}
Source: Balaguruswamy
StringBuffer class
• Why StringBuffer class is better than String class ?
• S1.setCharAt(n, ‘x’);
• S1.append(s2);
• S1.insert(n, s2);
• S1.setLength(n);
class BitMesra
{ C:\Users\Mayur\Documents\Java>javac
public static void main(String[] args) BitMesra.java
{ BitMesra.java:8: error: cannot find symbol
String a = new String("Hello"); c.insert(1,b);
String b=new String("Bit"); ^
String c = new String(“Hello”); symbol: method insert(int,String)
StringBuffer c =new StringBuffer("Hello"); location: variable c of type String
c.insert(1,b); 1 error
System.out.println(c);
}
} C:\Users\Mayur\Documents\Java>javac BitMesra.java
C:\Users\Mayur\Documents\Java>java BitMesra
HBitello
String class
Programmer
StringBuffer class
Thread-safe
Interfaces
1. Interface is a pure abstract class. They are syntactically similar to classes, but
you cannot create instance of an Interface and their methods are declared
without any body.
3. Syntax:-
interface interface-name { }
Example of Interface:-
interface Moveable
{
int AVERAGE-SPEED=40;
void move();
}
What compiler sees ?
• All variables declared inside interface are implicitly public static final
variables(constants).
• All methods declared inside Java Interfaces are implicitly public and
abstract, even if you don't use public or abstract keyword.
interface result
{
int maxmarks=100;
int minmarks=40;
void pass(int n);
}
class BitMesra implements result C:\Users\Mayur\Documents\Java>javac BitMesra.java
{
public void pass(int n) C:\Users\Mayur\Documents\Java>java BitMesra
{ Pappu pass ho gaya
if (n>=40 && n<=100)
{
System.out.println("Pappu pass ho gaya");
}
}
public static void main(String[] args)
C:\Users\Mayur\Documents\Java>javac BitMesra.java
{
BitMesra.java:19: error: cannot assign a value to final variable
BitMesra cse = new BitMesra(); minmarks
cse.pass(45); cse.minmarks=30;
cse.minmarks=30; // Error ^
} 1 error
}
Multiple Inheritance is achieved using interface
interface result class BitMesra implements result, hostel
{ {
int maxmarks=100; public void pass(int n)
{ if (n>=40 && n<=100)
int minmarks=40;
{
void pass(int n); System.out.println("Pappu pass ho gaya");
} }
interface hostel }
{
int[] hostel_no={7,8}; public void allotted(char gender)
void allotted(char gender); {
if (gender= ='M' || gender= ='m')
}
{
System.out.println("Allotted Hostel is : "+hostel_no[0]);
}
if (gender= ='F' || gender= ='f')
{
System.out.println("Allotted Hostel is : "+hostel_no[1]);
}
C:\Users\Mayur\Documents\Java>javac BitMesra.java }
public static void main(String[] args)
C:\Users\Mayur\Documents\Java>java BitMesra {
Pappu pass ho gaya BitMesra cse = new BitMesra();
Allotted Hostel is : 7 cse.pass(45);
Allotted Hostel is : 8 BitMesra imsc = new BitMesra();
imsc.allotted('M');
cse.allotted('f');
}
}
Difference between abstract class and interface
Passing Objects to Methods