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.

Sunday 24 July 2016

How to avoid duplicate values from ArrayList in Java?

This is one of the good interview question. I have faced many times this question in HCL & TCS interview. So frnds, let's see the following answer:

ArrayListDemo1.java

import java.util.*;
public class ArrayListDemo1 {

public static void main(String[] args)
{
ArrayList al = new ArrayList();

     al.add("Prajukta");
     al.add("Bishnupriya");
     al.add("Nitasha");
     al.add("Prajukta");
     al.add("Prajukta");      //Duplicate
     al.add("Prajukta");     //Duplicate

     ArrayList al2 = new ArrayList(new HashSet(al));

    Iterator it= al2.iterator();
    while(it.hasNext())
    {
    System.out.println(it.next());
    }

}
}

Output 
Prajukta
Bishnupriya
Nitasha

Tuesday 19 July 2016

Difference between Java and C

Difference Between Java and C
Java vs. C :
Java
C
Java is object-oriented

C is procedural-oriented
Java is both compiled and interpreted language

C is a compiled language.
Java is platform independent

C is platform dependent
Java is a high level language

C is a low-level language
Java uses bottom-up approach

C uses top-down approach
In Java, pointer is in back stage

C uses pointer explicitly
Java supports method overloading

C does not support method overloading
Java does not support pre-processor

C support pre-processor
Java handles exception

C does not support exception handling mechanism
Java collect garbage automatically
C collect garbage explicitly



Friday 8 July 2016

Why Interface in JAVA ?

We need interface due to the followings:
1.    It supports the functionality of multiple inheritance
2.    It achieves the concept of loose coupling which is the main advantage of interface

3.    It achieves 100% concept of abstraction, because interface must contain abstract method, not normal method. 

What is Dynamic Method Dispatch?

Method overriding is a disadvantageous fact, because in this concept only sub class property is coming as output, super class property is hidden. So to eliminate this disadvantage we need run-time polymorphism which is also known as dynamic method dispatch.
  • Dynamic method dispatch is a mechanism in which a call to an overriden method is reserved at runtime rather than compile time.
  • This is a mechanism that represents how java implement runtime polymorphism.
  • In this mechanism a super class reference variable can refer to a sub class object.
  • When an overriden method is called through a super class reference, java determines which method is executed and this is happened at runtime.
Example:

class Test
{
void display()
{
system.out.println("JavaRace");
}
}
class Test1 extends Test
{
void display( )
{
system.out.println("Silan Software");
}
}
class Test2 extends Test
{
void display( )
{
system.out.println("Silan Technology");
}
}
class Dispatch {
public static void main(String[] args)
{
Test obj1=new Test( );
Test1 obj2=new Test1( );
Test2 obj3=new Test2( );
Test r;
r=obj1;
r.display();
r=obj2;
r.display();
r=obj3;
r.display();
}
}

Output
JavaRace
Silan Software
Silan Technology 

Monday 6 June 2016

Can we write a Java program without main() method ?

Yes, We can write a java program without main method. it is possible by using static block only, which was supported in java 1.6 version. But not possible in Java 7 or Java 8.

class Test{  
  static{  
  System.out.println("Silan Software");  
  System.exit(0);  
  }  
}  

Output 
Silan Software (if not JDK7)


In JDK7 and above, the output will be:
Error: Main method not found in class Test, please define the main method as:public static void main(String[] args)


Thursday 2 June 2016

Why JAVA does not support global variables ?



The answer is to your question is, because Java doesn't support global variables, by design. Java was designed with object-oriented principles in mind and as such, every variable in Java is either local or a member of a class.
Static class members are globally-accessible, which is certainly one possible definition of a global variable, depending upon your interpretation of the term. To be pedantic, while Static class members are accessible via the class name and therefore across multiple scopes, they are still class members; and therefore not truly global variables as such.
Java's lack of support for global variables is a good thing, as using global variables is a design anti-pattern.

Monday 30 May 2016

Java program to check whether a given mail id is valid or invalid.

import java.util.regex.*;
class RegexDemo7
{
   public static void main(String[] args) 
   {
      Pattern p=Pattern.compile("[a-zA-Z0-9][a-zA-Z0-9_.]*@[a-zA-Z0-9]+([.][a-zA-Z]+)+");
      Matcher m=p.matcher(args[0]);
      if(m.find() && m.group().equals(args[0]))
      {
        System.out.println("valid mail id");
       }
       else
       {
         System.out.println("invalid mail id");
        }
    }
}

Output

1st run : 
java RegexDemo7  trilochan4u@gmail.com
valid mail id
2nd run : 
java RegexDemo7  trilochan4u
nvalid mail id
   

Java program to check whether a given number is a valid mobile number or not.

import java.util.regex.*;
class RegexDemo6
{
   public static void main(String[] args)
    {
        Pattern p=Pattern.compile("(0/91)?[7-9][0-9]{9}");
        Matcher m=p.matcher(args[0]);
        if(m.find() && m.group().equals(args[0]))
        {
System.out.println("valid mobile number");
         }
        else
        {
System.out.println("invalid number");
         }
      }
   }

Output 

                         1st run : java RegexDemo6  9439202111
                         valid mobile number
                         2nd run : java RegexDemo6  929291929394
                         invalid number

Java program to check whether a given string is palindrome or not without taking reverse.

import java.util.*;
public 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 palindrome");
else
System.out.println("the given string is not palindrome");

}

}

Output
1st Run:
enter a string
katak
the given string is palindrome

2nd Run:
enter a string
Silan
the given string is not palindrome