Java Generic

Java Generic :

A parameterized type that operates by a class or an interface is called generic. It is introduced in java-5. It facilitate to write algo which is independent from any specific data type and type safety.

Through Generic its possible to create a single class or method that automatically works with all data types such as Integer, String Float, Double etc. It provide ability to reuse existing code safely and easily.

Note : Generic works only with Object type such as Integer, Float. It can’t work with primitive type.

Example :

You can better understand Generic through below code :


package com.pkjavacode.com;

/**
*
* @author pradeep
*/
public class GenericTest {

public static void main(String args[]) {

GenericExample<Integer> gt = new GenericExample(50);
int a = gt.getObject();
System.out.println(a);
GenericExample<String> gt2 = new GenericExample("Pradeep Yadav");
String b = gt2.getObject();
System.out.println(b);
}
}

class GenericExample<T> {

T obj;

public GenericExample(T obj2) {
obj = obj2;
}

public T getObject() {
return obj;
}
}

Output :

50
Pradeep Yadav

vector collection in java


package com.pkjavacode.com;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class VectorExample {
public static void main(String args[]) {
List<String> list = new Vector<String>();
list.add("Pradeep");
list.add("Ajit");
list.add("Vijay");
Iterator<String> itr = list.iterator();
while (itr.hasNext()) {
Object element = (Object) itr.next();
System.out.println("" + element);
}
}
}

Output :
Vector elements are :
Pradeep
Ajit
Vijay

java Thread creation

What is a thread and how to create a thread in java ?

Thread :
A Thread is a part of program that is a program can contain more than one thread which have separate path of
execution in memory and can work more than one task simultaneously.
A thread can create two way in java :

  • By extending Thread class
  • By implementing Runnable interface

By extending Thread class :
In this way you will simply extends Thread class and override its run() method after that just create object of main class
in main() method and call start() method. You can better understand with below example.


package com.pkjavacode.com;

public class ThreadExample extends Thread {
public void run() {
System.out.println("Run method executing...");
}

public static void main(String arg[]) {
ThreadExample t = new ThreadExample();
t.start();

}
}

 

Output :
Run method executing…

 

By implementing Runnable interface :

In this way you will simply implements Runnable interface and override its run() method after that just create object of
main class in main() method and create Thread class object and pass main class object into Thread constructor and call
start() method. You can better understand with below example.


package com.pkjavacode.com;

public class ThreadExample implements Runnable {
public void run() {
System.out.println("Run method executing...");
}

public static void main(String arg[]) {
ThreadExample te = new ThreadExample();
Thread t=new Thread(te);
t.start();

}
}

 

Output :

Run method executing…

java custom exception

java custom exception :

To create custom exception in java just extends Exception class.

Example :


package com.pkjavacode.com;

class MyException1 extends Exception {
private String message;

public MyException1(String message) {

this.message = message;
}

public String getMessage() {
return message;
}
}

public class MyException {
public static void main(String[] a) {
try {
MyException.myTest(null);
} catch (MyException1 mae) {
System.out.println("Inside catch block: " + mae.getMessage());
}
}

static void myTest(String str) throws MyException1 {
if (str == null) {
throw new MyException1("String val is null");
}
}
}

 

Output :
Inside catch block: String val is null

java Singleton class

Create a Singleton class in java :

In java, when you will create a singleton class, you can’t create more than one instance that is only single instance will be created.

Example :


package com.pkjavacode.com;

public class SingletonDemo {

private static SingletonDemo singleinstance = new SingletonDemo();

private SingletonDemo() {

}

private static SingletonDemo getInstance() {

if (singleinstance == null) {
singleinstance = new SingletonDemo();

System.out.println("AccountCreation Class Object creatred...!!!");
} else {
System.out
.println("AccountCreation Class Object not Creatred just returned
Created one...!!!");
}
return singleinstance;
}

public void create(int no) {
System.out.println("Account Created Successfully, with Number:" + no);
}

public static void main(String[] args) {

SingletonDemo sd = SingletonDemo.getInstance();
SingletonDemo sd2 = SingletonDemo.getInstance();
sd.create(10);
sd2.create(20);
}
}

Output :

AccountCreation Class Object not Creatred just returned Created one…!!!
AccountCreation Class Object not Creatred just returned Created one…!!!
Account Created Successfully, with Number:10
Account Created Successfully, with Number:20

Coupling and Cohesion in java

Coupling :
Coupling is a degree where a class knows about another class. If class A has about class B, is what class B exposed
through its interface, then class A and B are called loosely coupled. And on other hand if class A relies on parts of class B
that are not part of class B’s interface, then the coupling between the class A and B is tighter which is not a good thing.

What is MVC in struts 2 ?

 MVC in struts 2 :

In struts Model, View and Controller(MVC) is a design pattern, which is used to develop web application in java.
Model : It is responsible for retrieving data from the database and saving the data into the database. It also responsible to implements all business logic. When a data entered by the user through view are checked in the model before saving into the database. Data access, data validation and the data saving logic are part of model component.

View: It is represents the user view of the application which is responsible for taking the input from the user, dispatching the request to the controller and then receiving response from the controller and displaying the result back to the user. html, jsp, custom tag libraries, resources files are the part of view component.

Controller: It is intermediary between Model and View components. It is also responsible for receiving the request from client through view. Once request is received from client it executes the appropriate business logic from the Model and then produce the result to the user using the View component. ActionServlet, Action, ActionSupport and struts.xml are the part of Controller.

You can better understand through below diagram :

mvc diagram