Step 1:Installation and Configuration of Java in Eclipse/Netbeans- 1st Hour

1-Installation of java (jdk)

Windows User

Step 1:-

Download the latest Java software (JDK) from oracle home site (http://www.oracle.com/technetwork/java/javase/downloads/index.html) Continue reading “Step 1:Installation and Configuration of Java in Eclipse/Netbeans- 1st Hour”

Advertisements

Step 2:Java definition : 1st hour

Definition-

Java is
high level,
Object oriented,
Platform independent programming language.
It Promises WORA (write once, run anywhere)

1-High level-

071713_1135_introductio4

2-Object oriented-

structured-vs-object-oriented

3-Platform independent-

javaservletjspjdbc-7-638

Step 10:Type Casting, Boxing/InBoxing and OutBoxing -6th hour +code

Definitions

-Assigning a value of one type to a variable of another type is known as Type Casting.

Datatype hierarchy-
(Biggest datatype) double > float > long > int > char > short > byte (smallest datatype)

primitives
type-casting-different-forms
(i)Widening/Automatic type conversion/Up casting
-When value of small datatype is saved into big datatype.
i.e-
Continue reading “Step 10:Type Casting, Boxing/InBoxing and OutBoxing -6th hour +code”

Step 15:Oops- 3rd Concept:Abstraction: 10th hour + code

Definition-

Abstraction is a concept of showing only functionality and hiding procedure.

In java, only name of method is given and not the body.

-Abtraction can be applied on classes and methods using abstract keyword.

– In abstraction we focus on “what” the object does instead of “how”.

1- abstract class
– Only abstract class can have abstract methods

Continue reading “Step 15:Oops- 3rd Concept:Abstraction: 10th hour + code”

Step 16:Oops- 4th Concept:Encapsulation: 11th hour + code

Definition-

Encapsulation is a concept of binding data and code together in single unit.

-In java, data means data members, and code means methods.

– In java, in encapsulation, data members are private and access methods (setters and getters) are public.

1- Creation of encapsulation class-


package ncapsulsn;

public class ExEncapsulated {

private String name;
private int age;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

2- Usege of encapsulation class-

package ncapsulsn;

public class UseEncapsulated {

public static void main(String[] args) {
ExEncapsulated e1=new ExEncapsulated();
e1.setName("xyz");
e1.setAge(5);
ExEncapsulated e2=new ExEncapsulated();
e2.setAge(17);
System.out.println(e2.getAge());
}
}

Step 17:this and super keywords – 11th hour + code

this –

Definition- It’s a keyword which refers to current class.
this keyword is used to-
(1) Refer class variable –

package dis;

public class TisVar{
String name="java";

void show(){
System.out.println(this.name);
}
}

(2) Refer current class constructor-

package dis;

public class TisConst{

public TisConst(){
this(5);
System.out.println("a");
}

public TisConst(int age){
System.out.println("b");
}

public static void main(String[] args){
TisConst k=new TisConst();
}
}

(3) Invoke current class method-

Continue reading “Step 17:this and super keywords – 11th hour + code”

Step 20:String Handling -13th hour + code

String:– Generally string is a sequence of characters, but in java, strings are immutable objects. Immutable simply means  unmodifiable/unchangeable.

Creation of String object:-
1:- By string literal
2:- By new keyword

1:- String literal:-

String s1=”Welcome”; //1 object created
String s2=”Welcome”; //no object will be created

Description:- When a String is created, JVM checks it in string constant pool if it exists it returns reference else string object is submitted to pool.

op1

2:- By new keyword:-

String s=new String(“Welcome”);     //Creates 2 objects
String s1=new String(“Welcome”);   //1 object

Description:- new String object is created in nonpool(heap memory) and literal “Welcome” is created in string constant pool. The variable s is reference variable for object created in nonpool.

output1

Methods of String class– There are many functions which can be performed on string such as concatenation,trim,split,substring etc.

String Buffer & String Builder –

Both classes are used to create mutable strings.

Unlike Strings these are stored in heap memory.

Both classes have same methods, but each method in StringBuffer is synchronized so StringBuffer is thread safe.

StringBuilder is fast in performance.

code samples are here

1-Mostly Used String methods-


package strn;

public class ExStrings {

public static void main(String[] args) {

String nameLiteral = "kuldeep"; // 1 object creation
String nameobject = new String("kaushik"); // 2 object creation

System.out.println(nameLiteral.charAt(4));
System.out.println(nameLiteral.concat(nameobject));

String[] arr1 = nameLiteral.split("d");
System.out.println(arr1[0] + " and " + arr1[1]);

System.out.println(nameLiteral.length());

String fortrim = " deep ";
System.out.println(fortrim);
System.out.println(fortrim.trim());

System.out.println(nameLiteral.equals(nameobject));

System.out.println(nameLiteral.indexOf("e"));

System.out.println(nameLiteral.isEmpty());

System.out.println(nameLiteral.length());

System.out.println(nameLiteral.substring(4));

System.out.println(nameLiteral.substring(2, 5));

System.out.println(nameLiteral.compareTo(nameobject));

System.out.println(nameLiteral.contains("ul"));

System.out.println(nameLiteral.replace("ul", "lu"));

}
}

2-Mostly Used StringBuilder/StringBuffer methods-(both classes have same methods)


package strn;

public class ExStringB {

public static void main(String[] args) {

StringBuilder sb1=new StringBuilder("java");
System.out.println(sb1.reverse());

StringBuffer sb2=new StringBuffer("core");
System.out.println(sb2.reverse());
}
}

 

Step 23:I/O package 18th+19th hour+code

Definition-

It's an API of Java which is used to read and write the data.

Stream–  is a mediator which connects our program to input and output device.
It’s a continuous flow of data which travel between source and target.

Types of streams-

1.Input Stream:Input stream is mediator between the Java program and the Input devices like keyboard, files.

2.Output Stream: Output stream is mediator between the Java program and the Output devices like Monitor, network.

Stream Classes-

Byte Stream classes: these classes works with 8 bit data.
and support ASCII code systems.

Character Stream classes:these classes work with 16 bit dara and support UNICODE code systems.

Note-  The input streams of byte stream classes are similar to the Reader streams of character stream classes
and the output streams are equivalent to Writer streams of character stream classes.

ByteStream classes-

bsc

-Character Stream Class-

csc

code samples are here

1- Fileinputstream and Fileoutputstream

package byte1;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FOSandFIS {
public static void main(String[] args) throws IOException {

FileOutputStream fileOutputStream = new FileOutputStream("C:\\kittu8.txt");
String a = "hi i am mr.java";

byte b[] = a.getBytes();

fileOutputStream.write(b);

FileInputStream fis = new FileInputStream("C:\\kittu8.txt");

byte[] b1 = new byte[fis.available()];

int i = 0;
while ((i = fis.read()) != -1) {
System.out.print((char) i);
}
}
}
2- ByteArrayOutputstream and ByteArrayInputstream

package byte1;

import java.io.*;

public class BAOSandBAIS {

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

ByteArrayOutputStream bao = new ByteArrayOutputStream();

String a = "abcdefg";

byte[] b = a.getBytes();

bao.write(b);

bao.writeTo(new FileOutputStream("C:\\kittu324.txt"));

bao.writeTo(new FileOutputStream("C:\\kittu718.txt"));

FileInputStream inputStream = new FileInputStream("C:\\kittu718.txt");

byte[] b2 = new byte[inputStream.available()];

inputStream.read(b2);

ByteArrayInputStream ais = new ByteArrayInputStream(b2);

int i;
while ((i = ais.read()) != -1) {
System.out.print((char) i);
}
}

3-DataOutputstream and DataInputstream

 package byte1;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class DOSandDIS {

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

DataOutputStream outputStream=new DataOutputStream(new FileOutputStream("D://titu7.txt"));

outputStream.writeInt(123);

DataInputStream stream=new DataInputStream(new FileInputStream("D://titu7.txt"));

System.out.println(stream.readInt());

}

}

4-BufferedOutputstream and BufferedInputstream

package byte1;

import java.io.*;

public class BFOSandBFIS {

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

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:/FileIO.txt"));

String str = "BufferedOutputStreamed Example";

byte[] b = str.getBytes();

bos.write(b);

bos.close();

BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream("D:/FileIO.txt"));

int i = 0;
while ((i = inputStream.read()) != -1) {
System.out.print((char) i);
}

}
}

5-PrintStream

package byte1;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.io.PrintWriter;

public class ExamplePrintStream {

public static void main(String[] args) throws FileNotFoundException {
PrintStream stream = new PrintStream("D://fg.txt");
stream.print("hey");

PrintWriter writer = new PrintWriter("d://gf.txt");
writer.print("bye");
writer.close();
}

}

 

Serialization and De-Serialization in java-

Serialization- It’s a process of converting an object into file.

De-Serialization- It’s a process of converting an file into object.

Serializable interface –

It’s a marker interface.

An empty interface without any methods is known as marker interface. It’s used to indicate to perform any special action.

 

6-ObjectOutputStream and ObjectInputStream (Serialization and De-Serialization)

(i)ObjectOutputStream (Serialization) –

package byte1;

import java.io.*;

public class PersonSER implements Serializable {

private int age;

private String name;

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public static void main(String[] args) throws FileNotFoundException, IOException {

PersonSER emp = new PersonSER();
emp.setName("amit");
emp.setAge(10);

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d://cf10.txt"));
oos.writeObject(emp);

}

}
(ii)ObjectInputStream De-Serialization

package byte1;

import java.io.*;

public class PersonDSER {

public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {

ObjectInputStream is = new ObjectInputStream(new FileInputStream("d://cf10.txt"));

Object pl = is.readObject();

PersonSER tr1 = (PersonSER) pl;

System.out.println(tr1.getName());
System.out.println(tr1.getAge());

}
}

1-FileReader and FileWriter


package byte1;

import java.io.*;

public class FWandFR {

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

FileWriter fw = new FileWriter("D:\\titu78.txt");
String a = "hi i am java";

fw.write(a);

fw.flush();

FileReader fr = new FileReader("D:\\titu78.txt");

int i = 0;
while ((i = fr.read()) != -1) {
System.out.print((char) i);
}

}

}

2- CharArrayReader and CharArrayWriter


package byte1;

import java.io.*;

public class CAWandCAR {

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

CharArrayWriter caw = new CharArrayWriter();

String msg = "i am java guy";

caw.write(msg);

FileWriter writer = new FileWriter("D://gyh.txt");
FileWriter writer1 = new FileWriter("D://hgk.txt");

caw.writeTo(writer);
caw.writeTo(writer1);

writer.flush();
writer1.flush();
caw.flush();

String j = "fhgfjgfjhgfgh";
char[] c = j.toCharArray();

CharArrayReader car = new CharArrayReader(c);
int i = 0;
while ((i = car.read()) != -1) {
System.out.print((char) i);
}
}

}

3-BufferedReader and BufferedWriter


package byte1;

import java.io.*;

public class BFWandBFR {

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

BufferedWriter bfw = new BufferedWriter(new FileWriter("D:\\titu190.txt"));

String msg = "i am awesome!";
bfw.write(msg);
bfw.flush();

BufferedReader bfr = new BufferedReader(new FileReader("D:\\titu19q0.txt"));

String msg1 = bfr.readLine();
System.out.println(msg1);
}

}

4-  PrintWriter


package byte1;

import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class ExPrintwriter {

public static void main(String[] args) throws FileNotFoundException {
PrintWriter p = new PrintWriter("D://fg.txt");
p.println("i am java lover");
}

}

Step 25:Reflection API -22th hour + code

Definition-

Generally reflection means -Ability of a software to analyze itself.
In java, reflection api allows to analyze and modify runtime behavior of class, at runtime.

Let’s take a class Abc to analyze with reflection api


package reflexn;

public class Abc {

private int age=12;
public String name="asd";

public Abc() {
System.out.println("asdfgh");
}

private Abc(String a){
System.out.println("zxcmnb");
}

public void ds(){
System.out.println("dfg");
}

private void sd(){
System.out.println("asd");
}
}

package- java.lang.reflect
Continue reading “Step 25:Reflection API -22th hour + code”

Step 26:Design Patterns -23th hour + code

1-Creational Patterns
These design patterns provides way to create objects while hiding the creation logic, rather than instantiating objects directly using new operator. This gives program more flexibility in deciding which objects need to be created for a given use case.

Abstract Factory Sets of methods to make various objects.
Builder Make and return one object various ways.
Factory Method Methods to make and return components of one object various ways.
Prototype Make new objects by cloning the objects which you set as prototypes.
Singleton A class distributes the only instance of itself.

Continue reading “Step 26:Design Patterns -23th hour + code”