Wednesday, November 21, 2012

java 7 features


Java 7 Features:

In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example. To separate groups of digits in numeric literals, which can improve the readability of your code.

For instance, if your code contains numbers with many digits, you can use an underscore character to separate digits in groups of three, similar to how you would use a punctuation mark like a comma, or a space, as a separator.
Example:
package com.jsl.core;
public class Numbers {
              public static void main(String[] args) {
                     long cardno=4214920316734659l;
                     long ccno=4214_9203_1673_4659l;
                     System.out.println(cardno);
                     System.out.println(ccno);
              }
}

You can place underscores only between digits; you cannot place underscores in the following places:
At the beginning or end of a number
            Adjacent to a decimal point in a floating point literal
Prior to an F or L suffix
In positions where a string of digits is expected

Diamond
Java 1.6 we must write the
List<Product> list6=new ArrayList<Product>();
From java 1.7
           List<Product> list7=new ArrayList< >();

package com.jsl.core;

import java.util.ArrayList;
import java.util.List;

class Product{
       String name;
       public Product(String name){
              this.name=name;
       }
       @Override
       public String toString() {
                     return "The product name is :"+name;
       }
}
public class Numbers {
              public static void main(String[] args) {
                           List<Product> list6=new ArrayList<Product>();
                           list6.add(new Product("Laptop"));
                           list6.add(new Product("Monitor"));
                           System.out.println(list6);
                          
                           /* From java 1.7 we can use */
                          
                           List<Product> list7=new ArrayList<>();
                           list7.add(new Product("Laptop"));
                           list7.add(new Product("Monitor"));
                           System.out.println(list7);
                          
              }
}
Catch with multiple exceptions
Since Java 7 you can write it like this, which makes our lives a lot easier:
package com.jsl.core;
public class Numbers {
 public static void main(String[] args) {
 try{
       String name="Krish";
       System.out.println(name.substring(2,8));
       int res=10/0;
       System.out.println(res);
    }catch(StringIndexOutOfBoundsException | ArithmeticException |
                                                       NullPointerException e){
           e.printStackTrace();
       }
 }
}





String with switch statement

Since Java 7 one can use string variables in switch clauses. Here is an example:

package com.jsl.core;
public class Numbers {
              public static void main(String[] args) {
                           String month="Mar";
                           switch(month){
                           case "Jan":System.out.println("Jan");
                                                break;
                           case "Feb":System.out.println("Feb");
                                      break;
                           case "Mar" : System.out.println("Mar");
                                       break;
                           default :System.out.println("Default ");
                           }
              }
}


Try with resource handling
The compiler detects unhandled exceptions thrown by automatic close() invocation on a resource.
package com.jsl.core;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Numbers {
    public static void main(String[] args) {
    try(BufferedReader bis =new BufferedReader(new FileReader("welcome.txt"))){
                                 
                     String message=null;
                     while((message=bis.readLine())!=null){
                           System.out.println(message);
                     }
              }catch (IOException e) {
                     e.printStackTrace();
              }
                          
              }
}

Friday, November 2, 2012

Java program without main method

Writing java program without main method


package com.jsl.sta.core;
public class StaticDemo {
static{
System.out.println("Block-1");
System.exit(0);
}
}

The above code will execute upto java 1.6 but it won't be executed in java 1.7 and gives error message


Error: Main method not found in class com.jsl.sta.core.StaticDemo, please define the main method as:

public static void main(String[] args)


In Java 7 without main method java program can't be executed.


Note:

If ask some one you can say still 1.6 its fine, Printing some message using static block without main method, But In Java 7 its required main method in order to execute java program.

Spring Boot 3 : JWT with SecurityFilterChain, AuthorizeHttpRequests, RequestMatchers

pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"...