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