Coding convesion:
name of class should start with capital letter data
PartTimeEmployee payPerHr
parttime_employee
When to use what? Jumpable
abs class vs interface
Animal Human
jump()
Monkey stayOnTree()
bite() Kid
Monkey
jump()
Kid school()
...
Strings are immutable (once it created there state can not be change)
stack heap
How String stored in java?
String s1=new String("lee"); s2
or
String s2="lee"; s3
String s3="lee"
method area
s3=s3+"foo"
"lee"
Bad prog practice
"leefoo"
String a="a+"b"+"c"+"d";
string pool
"a"
"ab"
"abc"
"abcd"
String data2=new StringBuilder().append("a").append("b").append("c").append("d").toString();
stack heap
data2
"abcd"
method area
"abcd"
string pool
exception handing: Checked and uncheked ex? GPP(good programming practice)
Checked ex Compile time ex All Ex happend at run time?
exception
Unchecked ex run time ex
try
catch
throw
throws
"God give me the problem
finally
but give me strenght to handle that"
"robust"
code Compiler
Examples: divide method try {
int x, y;
Scanner scanner=new Scanner(System.in);
System.out.println("PE 2 nos");
x=scanner.nextInt();
y=scanner.nextInt();
int z=x/y;
System.out.println("Z:"+z);
System.out.println("end");
}
catch(ArithmeticException e) {
System.out.println("dont do divide by zero");
}
catch(InputMismatchException e) {
System.out.println("input must be ints");
}
Scanner scanner = null;
try {
int x, y;
scanner = new Scanner(System.in); DRY
System.out.println("PE 2 nos");
x = scanner.nextInt();
y = scanner.nextInt();
int z = x / y;
System.out.println("Z:" + z);
System.out.println("end");
scanner.close();
} catch (ArithmeticException e) {
System.out.println("dont do divide by zero");
}
catch (InputMismatchException e) {
System.out.println("input must be ints");
}
User define exception: Need of user define ex?
throw vs throws bankapp
try
catch Account SA
throw
throws withdraw 1000 NotSufficientFundException
finally deposit OverFundEcxception
account creation process AccountCreationException
public Account(int id, String name, double balance) throws AccountCreationException{
if(balance<1000) {
throw new AccountCreationException("account can not be created min bal must be 1000 usd");
}
this.id = id;
this.name = name;
this.balance = balance;
}
class AccountCreationException extends Exception{
private static final long serialVersionUID = 1825616456008488982L;
public AccountCreationException(String message) {
super(message);
}
}
//NotSufficientFundException
class NotSufficientFundException extends Exception{
private static final long serialVersionUID = 1825616456008488982L;
public NotSufficientFundException(String message) {
super(message);
}
}
//OverFundEcxception
class OverFundEcxception extends RuntimeException{
private static final long serialVersionUID = 1825616456008488982L;
public OverFundEcxception(String message) {
super(message);
}
}
class Account{
private int id;
private String name;
private double balance;
public Account(int id, String name, double balance) throws AccountCreationException{
if(balance<1000) {
throw new AccountCreationException("account can not be created min bal must be 1000 usd");
}
this.id = id;
this.name = name;
this.balance = balance;
}
public void withdraw(double amount)throws NotSufficientFundException {
double temp=balance-amount;
if(temp<1000) {
throw new NotSufficientFundException("transaction is not possible min bal should be 1000 usd");
}
else
balance=temp;
}
//1L
public void depsit(double amount)throws OverFundEcxception {
double temp=balance+amount;
if(temp>=10_00_00) {
throw new OverFundEcxception("transaction is not possible max bal should be less then 10_00_00 usd");
}
else
balance=temp;
}
public void print() {
System.out.println("id: "+ id +" name: "+ name +" balance: "+balance);
}
}
public class A_UserDefineEx {
public static void main(String[] args) {
Account account;
try {
account = new Account(1, "pol", 900);
account.depsit(5000);
account.withdraw(94500);
account.print();
} catch (AccountCreationException e) {
System.out.println(e.getMessage());
}catch(OverFundEcxception e) {
System.out.println(e.getMessage());
} catch (NotSufficientFundException e) {
System.out.println(e.getMessage());
}
System.out.println("code end");
BikeStore
BikeStoreApplication BikeStoreCreationExcption
min bike should be 10
BikeStoreDemo
sell(int qty) main
BikeNotAvailableException
procure(int qty) StoreFullExcpetion : 20
Emp[] emps=new Emp[5];
stack heap 0 1 2 3 4
emps
new Emp(1, "foo")
method area
Object (Mother class)
class Emp {
private int id;
private String name; Emp Student Account
public Emp(int id, String name) {
this.id = id;
this.name = name;
}
why Object class?
public void print() {
print();
System.out.println("id: " + id + " name: " + name);
} public class java.lang.Object {
} public java.lang.Object();
public final native java.lang.Class<?> getClass();
public class D_SomeCommonEx { print() public native int hashCode();
print() public boolean equals(java.lang.Object);
public static void main(String[] args) { protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException;
Emp emp1=new Emp(1, "foo"); print() public java.lang.String toString();
emp1.print(); public final native void notify();
} public final native void notifyAll();
print()
public final void wait() throws java.lang.InterruptedException;
public final void wait(long) throws java.lang.InterruptedException;
public final void wait(long, int) throws java.lang.InterruptedException;
protected void finalize() throws java.lang.Throwable;
}
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
com.session2.ob_class.Emp@50040f0c
new Emp(1, "john");
Emp e1=new Emp(1, "john");
e1
Emp e2=new Emp(1, "john");
e2 new Emp(1, "john");
//compare there content
if(e1==e2) {
System.out.println("both are same");
}else
System.out.println("not same");
it check
address of object
Good coder optimal code
public boolean equals(Object obj) {
return (this == obj); Emp e1=new Emp(1, "john");
} Emp e2=new Emp(1, "john");
e1 new Emp(1, "john")
@Override
public boolean equals(Object obj) {
if (this == obj) e2
return true;
new Emp(1, "john")
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Emp other = (Emp) obj;
return id == other.id && Objects.equals(name, other.name);
}
if(e1.equals(e2)) {
System.out.println("both are same");
}else
System.out.println("not same");
public static void main(String[] args) {
List<Integer> list1=new ArrayList<>();
List<Integer> list2=new LinkedList<>(); 100000
doTiming(list1);
//time taken: 2111 ms for ArrayList
//time taken: 11 ms for LinkedList
private static void doTiming(List<Integer> list) {
for(int i=0;i<1E5; i++) { 0 9
list.add(i);
}
long start=System.currentTimeMillis();
for(int i=0;i<1E5; i++) {
list.add(0,i);
}
long end=System.currentTimeMillis();
System.out.println("time taken: "+(end-start)+" ms");
}
Set: set dont allow duplicate data
HashSet
LinkedHashSet
TreeSet