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