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