Core Java Source Code news

You can get Core Java Sample application and code for all core java Concepts in this page.

TreeSet Example in Java

TreeSet Example in Java Collection Framework. This Java TreeSet example describes the basic operations performed on the TreeSet in Collection Framework. Program: import java.util.TreeSet; import java.util.Iterator; public class TreeSetDemo { public static void main(String[] args) { TreeSet ts = new TreeSet(); ts.add(“A”); ts.add(“E”); ts.add(“C”); ts.add(“B”); ts.add(“D”); System.out.println(“TreeSet: “+ts); Iterator it = ts.iterator(); System.out.print(“TreeSet-element: “); while(it.hasNext())… Read More »

LinkedList Example in Java

LinkedList Example in Java Collection Framework. This Java LinkedList example describes the basic operations performed on the LinkedList in Collection Framework. Program: import java.util.*; class LinkedListDemo{ public static void main(String args[]){ LinkedList l1=new LinkedList(); System.out.println(“Original Contents : ” +      l1); l1.add(“C”); l1.add(“E”); l1.add(“B”); l1.add(“D”); l1.add(“F”); l1.addLast(“Z”); l1.addFirst(“A”); l1.add(1,”A2″); System.out.println(“Original Contents after add :… Read More »

Iterator Example in Java

Iterator Example in Java Collection Framework. This Java Iterator example describes the basic operations performed on the Iterator in Collection Framework. Program: import java.util.*; class IteratorDemo{ public static void main(String args[]){ ArrayList al=new ArrayList(); System.out.println(“Initial Size : ” + al.size()); al.add(“C”); al.add(“A”); al.add(“E”); al.add(“B”); al.add(“D”); al.add(“F”); System.out.println(“Original Contents of al : “); Iterator itr=al.iterator(); while(itr.hasNext()){… Read More »

Hashset Example in Java

Hashset Example in Java Collection Framework. This Java Hashset example describes the basic operations performed on the Hashset in Collection Framework. Program: import java.util.*; class HashSetDemo{ public static void main(String[] args) { HashSet hs = new HashSet(); hs.add(“Hello”); hs.add(“How are you”); hs.add(“fine”); System.out.println(“HashSet: “+hs); hs.remove(“fine”); System.out.print(“\n HashSet after removing : “+ hs); hs.add(“Not Bad”); System.out.print(“\n… Read More »

HashMap Example in Java Collection Framework

HashMap Example in Java Collection Framework. This Java HashMap example describes the basic operations performed on the HashMap in Collection Framework. Program: import java.util.*; class HashMapDemo{ public static void main(String[] args) { HashMap hm = new HashMap(); hm.put(“Emp Name”, “EshuSoft”); hm.put(“Designation”, “Technical Manager”); hm.put(“Company”, “EshuSoft Technologies”); hm.put(“Location”, “Bangalore”); System.out.println(“Company:” + hm.get(“Company”)); System.out.println(“HashMap Values:”); System.out.println(hm); Set… Read More »

ArrayList Example in Java – How to use ArrayList in Java Program

Program: import java.util.*; public class ArrayListDemo { /** * @param args */ public static void main(String[] args) { ArrayList al = new ArrayList(); al.add(“1”); al.add(“2”); al.add(“3”); System.out.println(“ArrayList size: “+al.size()); System.out.println(“ArrayList: “+al); al.add(“4”); al.add(4, “5”); System.out.println(“Size after addition : ” + al.size()); al.remove(3); al.remove(“5”); System.out.println(“Size after removal : ” + al.size()); System.out.println(“ArrayList: “+al); for(int i=0;i<al.size();i++) System.out.println(“ArrayList-element[“+i+”]:”+al.get(i));… Read More »

Java Threads Wait Notify Example Program

Java Wait and Notify Threads Program class Que1 { int n; boolean flag = false; synchronized int get() { if(!flag) try{ wait(); }catch (InterruptedException e) { System.out.println(“Exception”); } System.out.println(“Get: ” +n); flag = false; notify(); return n; } synchronized void put(int n) { if(flag) try{ wait(); }catch (InterruptedException e) { System.out.println(“Exception”); } this.n = n;… Read More »

Java Threads Without Locks Example Program

class TwoStrings { static void print (String str1, String str2) { System.out.print(str1); try { Thread.sleep(500); } catch (InterruptedException ie) { e.printStackTrace(); } System.out.println(str2); } } class PrintStringsThread implements Runnable { String str1, str2; PrintStringsThread(String str1, String str2) { this.str1 = str1;    this.str2 = str2; new Thread(this).start(); } public void run() { TwoStrings.print(str1, str2); }… Read More »

Threads Synchronization in Java with Example

You can find here Threads Synchronization example program.This threads synchronization is based on lock Synchronize concept. class TwoStrings1 { synchronized static void print (String str1, String str2){ System.out.print(str1); try{ Thread.sleep(1000); } catch (InterruptedException ie) { e.printStackTrace(); } System.out.println(str2); } } class PrintStringsThread1 implements Runnable { String str1, str2; PrintStringsThread1(String str1, String str2) { this.str1 =… Read More »

Threads Methods in Java with Example

In this page you will get threads methods in Java with example source code. You can find infomration about Java Threads methods with example program. class Methods implements Runnable{ Thread t; String name; public Methods(String n){ name = n; System.out.println(name +” Thread started”); t = new Thread(this); t.start(); } public void run(){ try { for… Read More »

Implements Runnable Example Program in Java

You can get Java code for Java Implements Runnable in thread concept with example program. class PrintRunnable implements Runnable { public PrintRunnable(){ System.out.println(“New Thread started”); new Thread(this).start(); } public void run() { for (int i = 0; i < 50; i++) { System.out.print(i+”  “); } } } public class ImplementRunnable2 { public static void main(String… Read More »

Java Threads Implements Runnable Example Program

You can get Java code for Threads class implements runnable program. You can get the example program for Java Threads Implements Runnable. class PrintNameRunnable implements Runnable { public PrintNameRunnable(){ System.out.println(“New Thread started”); } public void run() { for (int i = 0; i < 50; i++) { System.out.print(i+”  “); } } } public class ImplementRunnable1… Read More »

Java Thread Sample Program by using Extend Thread

You can get Java code for sample thread program by using the extend thread concept in Java. class PrintThread extends Thread{ String name; public PrintThread(String msg){; name = msg; System.out.println(“Thread: “+ name); start(); } public void run(){ for (int i = 0; i < 50; i++) { System.out.print(i+”  “); } } } public class ExtendThread2… Read More »

Java Extend Thread Example Program

You can find here Extend Threads concept by extend the thread class.The sample program code for extend thread basic concept in Java Thread. public class ExtendThread1 extends Thread{ public ExtendThread2(){ System.out.println(“New Thread is started”); } public void run(){ for (int i = 0; i < 50; i++) { System.out.print(i+”  “); } } public static void… Read More »

User defined Exceptions in Java with Example – Exception Handling

You can learn about User defined Exceptions in Java with Example Program. In this tutorial you can find user defined exception concept in java Exception Handling with example. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class MyException extends Exception{ MyException(){} public String toString(){ return “User created Exception occurred “; } } public class UserExceptionDemo { public… Read More »

Multiple Try Blocks in Java with Example – Exception Handling

You can learn about Multiple Try Blocks in Java with Example.In this tutorial you can find the concept of Multiple try blocks with sample program. public class MultipleTryStmtDemo { /** * @param args */ public static void main(String[] args) { int ary[]={1,2,3}; try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println(“Enter number”); int b =… Read More »

Finally Keyword in Java Example Program – Exception Handling

You can find Finally Keyword in Java Example Program.The concept of Exception Handling Finally keyword sample code. public class FinallyDemo{ public static void main(String args[]){ try{ System.out.println(“\n\tInside main method…”); throw new ArithmeticException(); }catch(ArithmeticException e){} finally{ System.out.println(“\n\tInside finally block…”); } System.out.println(“\n\tLast statement in main…”); } } Result: Inside main method… Inside finally block… Last statement in main…

Exception Handling in Java Example Program

You can find Exception Handling in Java Examples . In this page you can learn basic concept of Exception Handling Example Program. class BasicException{ public static void main(String args[]){ int d,a; try{ d=0; a=42/d; System.out.println(“This will not be printed.”); } catch(ArithmeticException e){ System.out.println(“Division by zero.”); } System.out.println(“After catch statement.”); } } Result: Division by zero.… Read More »