Convert String to InputStream example

import java.io.ByteArrayInputStream;
import java.io.SequenceInputStream;
import java.io.IOException;

public class stringtostream {
public static void main(String args[])
{
try
{
String str1 = “This is first String”;

byte bytes1[] = str1.getBytes();

String str2 = “This is second String”;

byte bytes2[] = str2.getBytes();

//convert byte array to InputStream
ByteArrayInputStream bis1 = new ByteArrayInputStream(bytes1);

ByteArrayInputStream bis2 = new ByteArrayInputStream(bytes2);

SequenceInputStream sis = new SequenceInputStream(bis1,bis2);

System.out.println(“sis”+sis.toString());
int i;
while((i=sis.read())!=-1)
{
System.out.print((char)i);
}
}
catch(IOException io)
{
System.out.println(“exception”+io.getMessage());
}
}
}

static block

static block gets executed before constructor is invoked.
static block is executed only once, whether one or more than one object is created.
As shown in below example more than one static block can exist in a class. They get executed in the order in which they are written.
Also non static block can exist in a class. A non static block gets executed once for every object, unlike static block.

static and non static block example:

class TestMe{
	{
		System.out.println("This is simple block");
	}

	static {
		System.out.println("in static block 1 TestMe");
	}

	static {
		System.out.println("in static block 2 TestMe");
	}

	TestMe()
	{
		System.out.println("in TestMe()");
	}
}

public class StaticBlockDemo {

	static{
		System.out.println("in static block StaticBlockDemo");
	}
	public static void main(String[] args) {

		System.out.println("in main program");

		//static block is executed before constructor is invoked
		//static block is executed only once, whether one or more than one object is created
		TestMe tm = new TestMe();
		TestMe tm1 = new TestMe();
		TestMe tm2 = new TestMe();
	}

}

Output:
in static block StaticBlockDemo
in main program
in static block 1 TestMe
in static block 2 TestMe
This is simple block
in TestMe()
This is simple block
in TestMe()
This is simple block
in TestMe()

Java Multilevel inheritance

Multi level inheritance is a Derived class is Base class of another class. In below example class B is derived from A, and B is further base class of C.

One of the point to ponder in Multilevel inheritance is, what is the order in which constructors are invoked. First Base most constructor gets invoked, and then its Derived constructor, and so on. In below example, when an object of C is created, first constructor of class A gets invoked, then constructor of class B, finally class C constructor is invoked.

To understand this keyword, please refer Usage of this keyword

class A{
	int i;
	public A(int i)
	{
		this.i = i;
		System.out.println("A(int i)");
	}

	public void show()
	{
		System.out.println("Value of i is:"+i);
	}
}

class B extends A{
	int j;
	public B(int i,int j)
	{
		super(i);
		this.j = j;
		System.out.println("B(int i,int j)");
	}
	public void show()
	{
		super.show();
		System.out.println("Value of j is:"+j);
	}
}

class C extends B{
	int k;
	public C(int i,int j,int k)
	{
		super(i,j);
		this.k = k;
		System.out.println("C(int i,int j,int k)");
	}

	public void show()
	{
		super.show();
		System.out.println("Value of k is:"+k);
	}
}

public class MultiLevelInheritance {

	public static void main(String[] args) {
		C obj = new C(10,20,30);
		obj.show();
	}

}

You may also like to read: Hierarchical Inheritance

usage of this keyword

this keyword means current object. Below are different usages of this keyword in java.
1. Prefix usages of member variables, when its name is same as parameters.
Eg: this.var1/*data member*/ = var1/*parameter*/;

2. To invoke another constructor of same class, from a constructor. this() should always be first statement, and can only be invoked from constructor.
Eg: this();//invokes A1() constructor

3. this is used as current object, directly in a statement.
Eg: A1 obj = this; //assigning current object

All above this usages are shown in below example.

class A1{
private float var1, var2;

public A1()
{
System.out.println("A1() invoked");
}

public A1(float var1,float var2)
{
this();//invokes A1() constructor
this.var1/*data member*/ = var1/*parameter*/;
this.var2 = var2;
System.out.println("A1(float var1,float var2) invoked");
}

public void show()
{
A1 obj = this; //assigning current object to obj
System.out.println(" "+var1+" "+var2);
this.display();//can be invoked as display(); also
}

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

}

public class ThisDemo {
public static void main(String[] args) {
A1 obj = new A1(40,60);

obj.show();

}
}

Note that this cannot be assigned to any other object. For example below line of code generates a Compiler error, the reason is this is declared as final, and hence it cannot be changed.

You may also like to read:
Why to have a parameter as final?
Can a constructor be final?
why to use final method?

Difference between yield and sleep

yield: Causes the currently executing thread object to temporarily pause and allow other threads to execute. If no other threads occupy CPU time, then CPU time may be allocated to current thread, itself.

It is an hint to the scheduler that the current thread is willing to yield its current use of a processor. The scheduler is free to ignore this hint.

sleep: puts current executing thread to pause for certain number of milliseconds, irrespective of CPU busy with other threads or not.

Yield example

class MyThread implements Runnable{
String name; //name of Thread
Thread t;

MyThread(String threadname){
name=threadname;
t = new Thread(this,name);
System.out.println(“New thread:”+t);
t.start();
}

public void run(){
try
{
System.out.println(“Thread sleeping”);
Thread.sleep(4000);//milliseconds
for(int i=5;i>0;i–)
{
System.out.println(name+”:”+i);

}
}
catch(Exception ie){
System.out.println(name+” Interrupted”);
}

System.out.println(name+” Exiting”);
}
}

class MyThread3 implements Runnable{
String name; //name of Thread
Thread t;

MyThread3(String threadname){
name=threadname;
t = new Thread(this,name);
System.out.println(“New thread:”+t);
t.start();
}

public void run(){
System.out.println(“Yielding…”);
t.yield();
for(int i=5;i>0;i–)
{
System.out.println(name+”:”+i);

}
System.out.println(name+” Exiting”);
}
}
public class YieldDemo {

public static void main(String args[])
{
//start the threads
new MyThread3(“One”);
new MyThread(“Two”);

try
{
Thread.sleep(20000);
}catch(InterruptedException ien)
{
System.out.println(“main thread Interrupted”);
}
System.out.println(“main thread Exiting”);
}

}

Multi threading Example

package multithreading;
public class Thread1{
public static void main(String args[]) throws InterruptedException{
myThread t1 = new myThread();
myThread2 t2 = new myThread2();

t1.start();
t2.start();
}
}

//Thread 1

class myThread extends Thread{
String s = “Hi Arjun”;
public void run(){

for(int i=0; i<s.length(); i++){
char c = s.charAt(i);
System.out.println(c);
try{
Thread.sleep(2000);
}catch(InterruptedException ex){
ex.printStackTrace();
}
}
}
}

//Thread 2

class myThread2 extends Thread{
String s2 = “Hi Karan”;
public void run(){
for(int i=0; i<s2.length(); i++){
char c2 = s2.charAt(i);
System.out.println(c2);
try{
Thread.sleep(2000);
}catch(InterruptedException ex){
ex.printStackTrace();
}
}
}
}

Download Netbeans Project of Multithreading example