Wednesday, August 22, 2012

Thread Example in Java


public class ThreadExample extends Thread{

public synchronized  void run(){
for(int i=0;i<11;i++)
System.out.print(i);
System.out.println(" ");
}
public static void main(String args[]){
Thread t1=new ThreadExample();
Thread t2=new ThreadExample();
Thread t3=new ThreadExample();

t1.start();
t2.start();
t3.start();

t1.setPriority(MAX_PRIORITY);
System.out.println("Thread 1 Priority"+t1.getPriority());

System.out.println("Thread 1 Group"+t1.getThreadGroup());

System.out.println("State of Thread2"+t2.getState());

}
}

Tuesday, August 21, 2012

Basic String Operations in Java


public class StringOperations {

public static void main(String args[]){
String str1="Hello";
String str2="Ja va";
String str3="Hello";

// String Operations

System.out.println("String1 == String3"+ str1.equals(str3));
System.out.println("String2 at Index 2:"+str2.charAt(2));
System.out.println("Compare Str2 and Str3"+str2.compareTo(str3));
System.out.println("Compare str1 and str3"+str1.compareTo(str3));
System.out.println("String2 contains Ja: "+str2.contains("Ja"));
System.out.println("String is empty ?"+str1.isEmpty());
System.out.println("Sting HashCode"+ str1.hashCode());
System.out.println("Substring of String1"+str1.substring(2, 3));
System.out.println("Remove whitespaces in String2"+str2.trim());
System.out.println("Convert to LowerCase"+str2.toLowerCase());
System.out.println("Replace a with A"+str3.replace('a','A'));
}
}

OutPut:
========
String1 == String3true
String2 at Index 2: 
Compare Str2 and Str32
Compare str1 and str30
String2 contains Ja: true
String is empty ?false
Sting HashCode69609650
Substring of String1l
Remove whitespaces in String2Ja va
Convert to LowerCaseja va
Replace a with AHello