Tuesday, 4 April 2017

What is JAVA !!!

1.    JAVA is a technology.
2.    Java is an OOP language.
3.    JAVA is a Platform.

Note : A Platform is hardware & software environment where we execute our java program. JAVA is itself a platform, bcz JAVA has it's own JRE(Java Runtime Environment).

How to convert ArrayList to String array in Java.

ArrayListExample1.java

package java8s;

import java.util.*;
public class ArrayListExample1 {

    public static void main(String[] args) {
        List al = new ArrayList<String>();
         
        al.add("First");
        al.add("Second");
        al.add("Third");
        al.add("Fourth");
        al.add("Fivth");
     
        String[] s1 = new String[al.size()];
        al.toArray(s1);
     
        for(String s2 : s1)
        System.out.println(s2);

    }


}

Output:
First
Second
Third
Fourth
Fivth

How to avoid duplicates from List / ArrayList

Hii frnds…this is the question that I have faced many times in interview, very popular frequently asked question. Let’s see in detail.
ArrayListExample.java

package java8s;

import java.util.*;
public class ArrayListExample {

          public static void main(String[] args) {
                  
                  
                                      ArrayList al = new ArrayList();
                    
                                            al.add("First");
                                            al.add("Second");
                                            al.add("Third");
                                            al.add("First");     //Duplicate
                                            al.add("First");     //Duplicate
                    
                                          
                                              ArrayList al1 = new ArrayList(new HashSet(al));     
                    
                                           // ArrayList al2 = new ArrayList(new LinkedHashSet(al));     
                    
                                           Iterator it= al1.iterator();
                                           while(it.hasNext())
                                           {
                                                 System.out.println(it.next());
                                           }
                    
                             }
                  

          }
Output:
Second
Third
First 

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