Wednesday 28 December 2016

String in java

Strings are generally sequence of characters.

Java treats String as an Object.

Java has a class named String which has numerous methods using which one can perform various operation on String object. The String class is in java.lang package.

String class implements

·         Serializable interface
·         Comparable interface
·         CharSequence interface.

Create String Object

In java, String object can be created in the following two ways:

1.      String literal
String s = “Java”;

2.      By using new keyword
String s = new String(“Java”);


Most important characteristics of java String is immutability. String is immutable in java.


Tuesday 27 December 2016

Java Basics

Java is a popular programming language as well as a platform. It was first introduced in 1995 by Sun MicroSystems.
Java can be used to develop standalone (Desktop) application, mobile application, web application, applets, games, etc.
Java SE, Java EE and Java ME are the various edition available to develop applications in java.

Significant features:

·         Java is a high level programming language.
·         Java follows Object Oriented Programming concept.
·         Java is platform independent as it is Write Once, Run Anywhere (WORA) programming language.        (Learn why?)
·         Java is a multi-threaded programming language.
·         Java is secure, robust and portable.

Previous Versions: You don’t need to remember each one of them. Only the difference between any two consecutive versions that is marked in bold needs to be known. It may be asked during interviews.

  • JDK Alpha and Beta (1995)
  • JDK 1.0 (23rd Jan, 1996)
  • JDK 1.1 (19th Feb, 1997)
  • J2SE 1.2 (8th Dec, 1998)
  • J2SE 1.3 (8th May, 2000)
  • J2SE 1.4 (6th Feb, 2002)
  • J2SE 5.0 (30th Sep, 2004)
  • Java SE 6 (11th Dec, 2006)
  • Java SE 7 (28th July, 2011)
Latest Version:

·         Java SE 8 is the latest version of java.
Requirements to get started:

·         Install the JDK in case you don’t have it on your System.
·         Install an editor like Eclipse, Netbeans, or any other as per your choice. (All the example in this tutorial is programmed on Eclipse)
·         Set path and Classpath
Let’s get started with our first Java Program.

Friday 23 December 2016

Difference : Iterator vs ListIterator

Iterator and ListIterator differ based on following parameters:

1.      Traversal direction
Iterator allows to traverse the List, Set and Map in only forward direction. Iterator is unidirectional in nature.
ListIterator allows to traverse the List (remember only List) in both forward and backward direction. ListIterator is bidirectional.

2.      Method
Iterator has next() method, using which forward traversal is possible.
ListIterator has next() method for forward traversal and previous() method for backward traversal.

3.      Collection implementations that can be traversed
Iterator can traverse List, Set and Map.
ListIterator can only be used for List traversal.

4.      Modifying the elements while traversing
Iterator can only remove the element during iteration. It has only remove() method.
ListIterator can add, remove and set elements during iteration.

5.      Current position
Iterator cannot determine the current position of iteration during iterating over List.
ListIterator can determine the current position of iteration during iterating over List.

6.      Retrieve index
Index of the element cannot be retrieved using Iterator.
Index of the element can be retrieved using ListIterator using previousIndex() and nextIndex() method.

7.      Example

Wednesday 21 December 2016

Difference : List Vs Map

List and Map differ based on following parameters:

1.      Approch
List stores only the value of the element.
Map applies different mechanism as it stores value of the element along with a unique key associated to the each stored value.

2.      Memory requirement
As List only stores the value of the element so it requires less memory space in comparison to Map.
On other hand Map needs to store a unique key for each value so memory requirement increases.

3.      Implementation
ArrayList, LinkedList and Vector implements List interface.
HashMap, LinkedHashMap, TreeMap, etc. implements Map interface.

4.      Order maintainence
List ensures that the insertion order is maintained.
All Map implementations doesn’t guarantee of maintaining insertion order (if you care about order, useLinkedHashMap).
*In case where insertion order is maintained we get the elements returned in same order in which they were inserted while retrieving them.

5.      Null value
Any number of null values can be stored in List.
Only one null key is allowed in Map. But you can store any number of null values.

6.      Duplicate value
List accepts duplicate values while storing.
Map doesn’t accept duplicate Key. But duplicate values are allowed.

7.      Retrieve element
In List elements can be retrieved specifying index of the element.
In Map elements can be retrieved by specifying its Key.

8.      Example
List Example


package javaRadarArrayList;

import java.util.ArrayList;
import java.util.List;

public class SimpleArrayList {

      public static void main(String[] args) {
            //create Array List
            List<String> javaRadarList = new ArrayList<String>();
            //Add elements to ArrayList
            javaRadarList.add("Java");
            javaRadarList.add("Spring");
            javaRadarList.add("Hibernate");
            javaRadarList.add("EJB");

            System.out.println(javaRadarList);
      }
}

OUTPUT:

[Java, Spring, Hibernate, EJB]


Map Example

package map;

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

public class MapExample {

        public static void main(String[] args) {

                        Map m = new HashMap<String, String>();
                        m.put("x", "1");
                        m.put("w", "2");
                       
                        Set set = m.entrySet();
                        Iterator itr = set.iterator();
                       
                        while(itr.hasNext()){
                                        Map.Entry entry = (Map.Entry) itr.next();
                                        System.out.println(entry.getKey() + " "+ entry.getValue());
                        }

        }

}

OUTPUT:
w 2
x 1



You may also like to read:

Tuesday 20 December 2016

Difference : List Vs Set

List and Set differs based on the following parameters.

1.       Duplicate Object
List allows duplicate elements to be stored in it.
Set doesn’t allow duplicate element. All the element needs to be unique.

2.       Order Maintenance
List generally maintains the insertion order.
In set maintenance of insertion order depends on the implementing class. (HashSet-> order not guaranteed, LinkedHashSet-> Order is maintained)

3.       Null value
In List, one can store unlimited null values
In Set, only one null value is permitted.

4.       Implementations
ArrayList class, LinkedList class and Vector class implements List interface.
HashSet class, LinkedHashSet class and TreeSet class implements Set interface.

5.       Traversal
You can use both ListIterator and Iterator to traverse elements in List.
You cannot use ListIterator to iterate Set. To traverse Set use Iterator.

Example

package collection;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ListandSet {

      public static void main(String[] args) {
            List<String> arrayList = new ArrayList<String>();
            arrayList.add("A");
            arrayList.add("B");
            arrayList.add("A");
            arrayList.add("C");
            arrayList.add("D");
            System.out.println("List elements:"+ arrayList);
           
            Set<String> set = new HashSet<String>();
            set.add("A");
            set.add("B");
            set.add("A");
            set.add("C");
            set.add("D");
            System.out.println("Set elements:"+ set);

      }


}

Output:
List elements:[A, B, A, C, D]
Set elements:[A, B, C, D]

Saturday 17 December 2016

Difference : ArrayList Vs HashMap


ArrayList and HashMap differ based on following parameters:

1.      Approach
ArrayList stores only the value of the element.
HashMap applies different mechanism as it stores value of the element along with a unique keyassociated to the stored value.

2.      Memory requirement
As ArrayList only stores the value of the element so it requires less memory space in comparision to HashMap.
On other hand HashMap needs to store a unique key for each value so memory requirement increases.

3.      Implementation
ArrayList implements List interface.
HashMap implements Map interface.

4.      Order maintainence
ArrayList ensures that the insertion order is maintained.
HashMap doesn’t guarantee of maintaining insertion order (if you care about order, use LinkedHashMap).
*In case where insertion order is maintained we get the elements returned in same order in which they were inserted while retrieving them.

5.      Null value
Any number of null values can be stored in ArrayList.
Only one null key is allowed in HashMap. But you can store any number of null values.

6.      Duplicate value
ArrayList accepts duplicate values while storing.
HashMap doesn’t accept duplicate Key. But duplicate values are allowed.

7.      Retrieve element
In ArrayList elements can be retrieved specifying index of the element.

Tuesday 13 December 2016

Difference: ArrayList Vs Array

ArrayList and Array differ based on following parameters:

1.       Nature
Array is static in nature. Its length is predefined and thus array cannot grow or shrink as per need. If it gets full then we cannot add more element to it.

ArrayList is dynamic in nature thus grows or shrink automatically as per the requirement.


2.       Memory Consumption
If element is removed from Array, the memory consumption doesn’t decrease as array doesn’t shrink automatically. It remains fixed even if element is removed thus holding memory without providing any proper utilization.

If element is removed from ArrayList, it automatically shrinks thus saving programmer from the issue of wasting memory.


3.       Provision of predefined methods
ArrayList provides lots of predefined methods which helps in making programmer task easier while implementing code thus gaining advantage over Array.

      4.       Example


Array Example:

package array;

public class ArrayExample {

      public static void main(String[] args) {
            int array[] = new int[5];
           
            for(int i=0;i<=5;i++){
                  array[i]=i*i;
                  System.out.println(i+" index contains "+ array[i]);
            }

      }

}


OUTPUT:
0 index contains 0
1 index contains 1
2 index contains 4
3 index contains 9
4 index contains 16
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
      at array.ArrayExample.main(ArrayExample.java:9)


Why is the exception thrown here?

Reason: We have declared array which has capacity to store 5 elements at max. So it stores 5 elements without any complaint(from index 0 to 4). But when we are trying to store value (5*5) at index array[5] we are sure to get ArrayIndexOutOfBoundsException: 5
To store one more element we need to have array of size 6. So here you have to change size like this:

int array[] = new int[6];

Now save the program and run again to see 25 getting stored at index 5.


ArrayList Example

package javaRadarArrayList;

import java.util.ArrayList;

public class SimpleArrayList {

      public static void main(String[] args) {
            //create Array List
            ArrayList<String> javaRadarList = new ArrayList<String>();
            //Add elements to ArrayList
            javaRadarList.add("Java");
            javaRadarList.add("Spring");
            javaRadarList.add("Hibernate");
            javaRadarList.add("EJB");

            System.out.println(javaRadarList);
      }

}

OUTPUT:
[Java, Spring, Hibernate, EJB]

In ArrayList we don't need to declare size. It automatically allocates a default size first. And later if required arraylist automatically re-sizes itself.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.