Wednesday, 22 February 2017

JAVA Program to check whether a given string is palindrome or not without taking the concept of reverse

.import java.util.*;
class StringPalindrome {

public static void main(String[] args)
{
String str;
int l,c=0;
Scanner s= new Scanner(System.in);
System.out.println("enter a string");
str= s.nextLine();
l=str.length();
for(int i=0;i<(l-1)/2; i++)
{
if(str.charAt(i)==str.charAt(l-1-i))
{
continue;
}
else
{
c++;
}
}
if(c==0)
System.out.println("the given string is a pallindrome");
else
System.out.println("the given string is a not pallindrome");

}

}

Output
enter a string
katak
the given string is pallindrome

Friday, 3 February 2017

Interface in JAVA 8(JDK 1.8 version)

It is possible to define method inside the interface by implementing default method or static method in JAVA 8.

Example1: using default method:

package test;
interface A{
default void show()
{
     System.out.println("java");
}
}
class B implements A
{
    

}

class InterfaceExample1
{
     public static void main(String[] args)
     {
          B obj=new B();
          obj.show();
     }
}
Output

java

Example2: Using static method:
package test;
interface A{
static void show()
{
    System.out.println("java");
}
}

class InterfaceExample2
{
    public static void main(String[] args)
    {
        A.show();
    }
}
Output
java

Saturday, 5 November 2016

Why Interface in Java ?

Ans:

1. Support the functionality of multiple inheritance.

2. Achieve fully abstraction.

3. Achieve loose coupling, which is the biggest advantage of interface.

Tuesday, 1 November 2016

How to sort ArrayList in JAVA.

To sort values in ArrayList, we use Collections class, and invoke sort() method. consider this example:

package java8s;
 
import java.util.*;
 
public class SortCollectionDemo {
 
public static void main(String[] args)
{
 
    List li = new ArrayList();
 
    li.add("Santosh");
    li.add("Akshay");
    li.add("Tapuuu");
    li.add("Millan);
    li.add("Rakesh");
 
    Collections.sort(li);
 
for(String temp: li)
{
System.out.println("Countries : "+temp);
}
}
}

Output:
Akshay
Millan
Rakesh
Santosh
Tapuuu

Monday, 24 October 2016

How to make ArrayList synchronized ?


ArrayList methods are non-synchronized but still if there is a need that we can make them synchronized as :
//Use Collecions.synzhonizedList method
List l = Collections.synchronizedList(new ArrayList());
...

//If you want to use iterator on the synchronized list, use it
//like this. It should be in synchronized block.
synchronized (l) {
  Iterator it = l.iterator();
  while (it.hasNext())
      ...
      it.next();
      ...

}

Difference Between ArrayList and Vector in JAVA


 ArrayList and Vector both are the implementation class of List interface and use resizable array as a data structure internally and insertion order is preserved. However there are few differences between ArrayList and Vector.

ArrayList Vs Vector

1) Synchronization: ArrayList is non-synchronized that means multiple threads can work on ArrayList at the same time. For e.g. if one thread is performing an add operation on ArrayList, there can be an another thread performing remove operation on ArrayList at the same time in a multithreaded environment
while Vector is synchronized. This means if one thread is working on Vector, no other thread can get a hold of it. Unlike ArrayList, only one thread can perform an operation on vector at a time.
2) Resize: Both ArrayList and Vector can grow and shrink dynamically to maintain the optimal use of storage, however the way they resized is different. ArrayList grow by half of its size when resized while Vector doubles the size of itself by default when grows.
3) Performance: ArrayList gives better performance because it is non-synchronized. Vector operations gives poor performance as they are thread-safe.

4) Legacy : ArrayList is not a legacy class, because it is introduced in JDK 1.2 version, where as Vector is a legacy class introduced in JDK 1.0 version

Tuesday, 9 August 2016

JAVA AWT vs. JAVA Swing



JAVA AWT
JAVA Swing
Java AWT components are platform dependent.
Java Swing components are platform independent.
AWT components are heavyweight.
Swing components are lightweight.
AWT does not support pluggable look and feel.
Swing supports pluggable look and feel.
AWT does not follow MVC.
Swing follow MVC (Model View Controller) where model represents data, view represents presentation and controller acts as an interface between model and view.