0% found this document useful (0 votes)
24 views6 pages

4batch 12 13

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views6 pages

4batch 12 13

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

**************************----Java 12 and 13 Batch ----

*********************

(Immutable)
1.String Class :- The Group of Characters
-------------

char[] arr = {'a','b','c','d','e',};

1.String Literals/class-- SCP --- Create obj Default by JVM


2.Using New Keyword--- Heap-- own create obj here

Ex:-

package jangPackage;

public class firstClass {

public static void main(String[] args) {

// char[] arr = {'a','b','c','d','e',};

//String s=String.valueOf(arr);
//System.out.println(s);

// String s1 = "Vikas";
// String s2 = "Chaudhari";
// String s3 = "Vikas";
// System.out.println(s1+" "+s2+" "+s3);

String ss= new String("Vikas Chaudhari");


System.out.println(ss);
}

Ex:

package jangPackage;

public class firstClass {

public static void main(String[] args) {

String s1 = "Vitas";
String s2 = "Chaudhari ";
String s3= "";
System.out.println(s1+" "+s2);
System.out.println(s1.length());
System.out.println(s1.toUpperCase());
System.out.println(s1.toLowerCase());
System.out.println(s1.replace('t', 'k'));
System.out.println(s2);
System.out.println(s2.trim());
System.out.println(s2.charAt(4));
System.out.println(s1.concat(s2));
System.out.println(s3.isEmpty()); // Boolean
System.out.println(s3.isBlank());
System.out.println(s1.startsWith(s3));
}

(Mutable)
2. StringBuffer:-

3.StringBuilder:-

Java. Lang Package -----

1. Object Class -- 11 methods


2. toString Method

Parent B
Child A

class A extends B { class A { }

# Methods In Object Class:- 11 methods


----------------------

1. toString() -- This method returns a string representation of the object.


Public String toString();

2. hashcode() -- A Unique id that is Assigned to an Object called hashCode.


public int hashCode();

HashTable
HashMap
HashSet

1) Liner Search: O(n)

2) Binary Search: O (log2 O) --- two elements compare at a time

3) hashing Algorithm:- O(1)

3) .equal():- Referance Comparision In Your Object Class Method

We can used this method to check the equality of an object.


public boolean equals(Object o);
5.finalize():-
Called by GC on an object when garbage collection determine that there
are no reference
to the object.

Protected void finalized() throw throwable

4. clone():- Creates And Returns acopy of this Object.

protected object clone () throws CloneNotSupportedException

Ex:-
package javaLangPckg;

public class Employee implements Cloneable{

int id;
String name ;

public int getId() {


return id;
}

public void setId(int id) {


this.id = id;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public Employee(int id, String name) {


super();
this.id = id;
this.name = name;
}

@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + "]";
}

public static void main(String[] args) throws CloneNotSupportedException {

Employee emp1=new Employee(12,"Vikas Chaudhari");


Employee emp2 =(Employee) emp1.clone();

System.out.println("Emp1:"+emp1.getId()+" ,"+emp1.getName());
System.out.println("Emp2:"+emp2.getId()+" ,"+emp2.getName());
}
}
6. getClass():- Return the runtime class of this object. The returned Class object
is the object
that is locked by static synchronized methods of the represented
class.

public final Class<>getClass();

7. wait()
8. wait(long ms)
9. wait(lon ms, int ns )
10.notify()
11.NotifyAll()

Ex:-

package javaLangPckg;

public class Student {

String name ;
int rollno;

public String getName() {


return name;
}
public void setName(String name) {
this.name = name;
}
public int getRollno() {
return rollno;
}
public void setRollno(int rollno) {
this.rollno = rollno;
}
public Student(String name, int rollno) {
super();
this.name = name;
this.rollno = rollno;
}
@Override
public String toString() {
return "Student [name=" + name + ", rollno=" + rollno + "]";
}

@Override
public boolean equals(Object obj) {
Student st=(Student) obj;
try {
if(name.equals(st.getName()) && rollno == st.getRollno())
return true;
else
return false;
}catch(Exception e){
return false;
}
}
public static void main(String[] args) {
Student st1=new Student("Vikas", 2);
Student st2=new Student("Vikas", 2);
boolean flag =st2.equals(st1);
System.err.println(flag);

System.out.println(st1.getClass().getName());
}

===================================================================================
=============

Singletone Class/ Design Pattern:-


================================

The Class having Only Instance / Object and this


object used repetatively.

How to create Singletone Class :-

1) Private Contructor
2) Factory Method
3) private Static Variable

package com.singletone;

public class Samosa {

private static Samosa samosa;

private Samosa() {

public static Samosa getSamosa() { // factory method


//object of this class

synchronized(Samosa.class) {
if(samosa == null) {
samosa =new Samosa();
}
}

return samosa;

}
}

package com.singletone;
public class Example {

public static void main(String[] args) {

System.out.println(Samosa.getSamosa().hashCode());
System.out.println(Samosa.getSamosa().hashCode());
}

You might also like