Friday, March 29, 2013

StringTokenizer Example

StringTokenizer Example:

StringTokenizer is the class in java.util package.

The use of StringTokenizer class is breaks the given string into number of tokens based on the delimiter.

Here i am giving the very simple StringTokenizer Example. you can easily understand the below code.

In the below example, i am using the delimiter as ';'

While creating the StringTokenizer object I am passing the given string and the delimiter.

By using the countTokens() method, will get the total number of tokens.

String Tokenizer Sample code

 import java.util.StringTokenizer;  
  * Java String Tokenizer example  
 public class StringTokenizerExample {  
      public static void main(String[] args) {  
           String str = "value1;value2;value3;value4";  
           StringTokenizer stringTokenizer = new StringTokenizer(str, ";");  
           //return the total number of tokens  
           int totalTokens = stringTokenizer.countTokens();  
           System.out.println("Total Tokens " + totalTokens);  
           //Iterate and print all the tokens  
           while (stringTokenizer.hasMoreElements()) {  
           //You can use the below method also to print the tokens.  
           // while(stringTokenizer.hasMoreTokens()){  
           // System.out.println(stringTokenizer.nextToken());  
           // }  

Output :

Total Tokens 4

You can also use split method to split the given string into number of tokens.

String[]   tokens = str.split(";");

Wednesday, March 20, 2013

Java HashMap Example

Java HashMap Example :

HashMap is the class in java.util package, The main advantage of HashMap is to store the information in the form of Key/Value pairs. In this HashMap Object we can store the null values also. that means the HashMap accepts to store the null values as key/value pairs. Suppose you are retrieving the data from the database, if some records are returned with null values. you can store these null values also in this HashMap object.

HashMap is not synchronized.

Below is the very straight and quick start example for HashMap.

The below HashMap example stores key/value pairs and just retrieve from the HashMap again and printing the key/value data.

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashMapExample {

    public static void main(String[] args) {
        HashMap map = new HashMap();
        // add the elements to the hashmap
        map.put("key1", "value1");
        map.put("key2", "value2");
        map.put("key3", "value3");
        map.put("key4", null);
        Set set = map.entrySet();
        Iterator iterator = set.iterator();
        // Iterate teh elements from the hashmap
        while (iterator.hasNext()) {
            Map.Entry keyValue = (Map.Entry);
            System.out.println("key....." + keyValue.getKey() + " value...."
                    + keyValue.getValue());



Output :

key.....key4 value....null
key.....key3 value....value3
key.....key2 value....value2
key.....key1 value....value1

Sunday, March 17, 2013

Java ArrayList Example

Java ArrayList Example :

Java ArrayList Example, I am giving very straight, short and easy description, you can straight away use this in your Code.

ArrayList : You can find this ArrayList class in java.util package. There are lot of methods available to manipulate ArrayList Object.

The advantage of ArrayList is to store the different kind of objects, either string or user defined class objects.

In the below i am giving the ArrayList Example how you can store the String Objects and again get the objects from that ArrayList object.

Here i am giving the two ways two get the Elements from the ArrayList object,

1) Get the elements from the ArrayLsit  by using Iterator.
2) Get the elements from the ArrayLsit  by using For Loop.

ArrayList Example 

import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListExample {
    public static void main(String[] args) {
        // Create an ArrayList Object and add the elements into it.
        ArrayList aList = new ArrayList();
        // Add the String objects in the ArrayList
        // Get the Elements from the ArrayList
        Iterator itr = aList.iterator();
                .println("Using Iterator to get the Elememts from the ArrayList: ");
        while (itr.hasNext()) {
        // Another Way To get the elements from the ArrayList
                .println("Using for Loop to get the Elememts from the ArrayList: ");
        for (int i = 0; i < aList.size(); i++) {
            System.out.println((String) aList.get(i));

OutPut :

Using Iterator to get the Elements from the ArrayList:
Using for Loop to get the Elements from the ArrayList:

Thursday, March 14, 2013

Core Java Samples

Hello World Example                                   LinkedHashSet Example

Java ArrayList Example                               Java static import

Java HashMap Example                               Java String

StringTokenizer Example                             Java BufferedReader

Java Iterator Example                                  JavaBufferedWriterExample

Java TreeSet Example                                 Java Constructor Example

Java Enahnced For Loop Example                Java File Input Stream Example

Java ListIterator Example                            Java File Output Stream Example

Java HashSet Example                                Access Specifiers in Java

java static import example

Hello World Example

Java Hello World Example 

It is very simple in java to write the Hello World Example. In this Example, We just print the Text as Hello World Example on the console after executing this Program.

The below Example is very straight forward example, take the below code and just compile using from the  command prompt or you can use any tools like eclipse.

Use the Javac command to compile the java program, for Example Javac Once you compile this program, the Java compiler compiles and generates the .class file. It is in the byte code format.

Then you execute this program by using java command for example java HelloWorld

Once you invoke the above command, we get the output as 'Hello World Example'.

 public HelloWorldExample{
  public static void main(String[] args){
   System.out.println("Hello World Example");  

Monday, March 11, 2013

Java Interview Questions

  •   if(null==null){

System.out.println("Both are equal");
System.out.println("Both are not equal");
           What is the output for the above code?

          Output : Both are equal.

  • Difference between finally and finalize?
  • What is cloning? what is deep and shallow cloning? How to do deep cloning?
  • How you write the program to read the file?
  • which version of java you used?
  • What is the difference between encapsulation and polymorphism?
  • Can you tell me about oops concepts?
  • what is polymorphism? Did you used anywhere in your project?
  • Did you work on multi threading in any of the project?
  • Can you tell me the scenarios, where you exactly used syncronization?
  • What is immutability?
  • you know how to make class as immutable object?
  • What is garbage collection? Can we invoke the garbage collection explicitly?
  • What are collections you used in your project?
  • Difference between Hashset and ArrayList?
  • How Hashset eliminates duplicates?
  • If you try to add duplicate element to the set, what happens?
  • what is the difference between java.util.Date and java.sql.Date?
  • what is the difference between java null and sql null?
  • how hashmap works internally?
  • Difference between HashMap and HashTable?
  • What is the difference between Array List and Vector?

Disclaimer : If you find any mistakes / corrections / feedback Please let us know...This website author shall not accept liability or responsibility for any errors, mistakes or misstatements found in this website.