Showing posts with label Interface. Show all posts
Showing posts with label Interface. Show all posts

Wednesday, February 3, 2016

Java try-with-resources statement vs. try-finally block

The try-with-resources is a feature added in Java 1.7. A resource is an object implementing the java.lang.AutoCloseable interface. The try-with-resources statement declares a block on one or more resources. The resources automatically close at the end of the block. The resources are always closed regardless of exceptions. If more than one resources are included, the resources are separated by semicolon.


1. An example of using the try-with-resources block.

public void printData(String sql) throws SQLException {
      //try-with-resources block
      try (PreparedStatement ps = con.prepareStatement(sql)) {
            //try-with-resources block
            try (ResultSet rs = ps.executeQuery()) {
                  //code processing the ResultSet.
                  while (rs.next()) {
                        System.out.println(rs.getString(1));
                  }
            }
      }
}

The above code automatically closes the rs at the end of the inner try-with-resources block and the ps at the end of the outer try-with-resources block.


2. Comparing the above try-with-resources code with the below try-finally code.

public void printData(String sql) throws SQLException {
      PreparedStatement ps = null;
      ResultSet rs = null;

      //try-finally block
      try {
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            //code processing the ResultSet.
            while (rs.next()) {
                 System.out.println(rs.getString(1));
            }
      } finally {
            if (rs != null) rs.close();
            if (ps != null) ps.close();
      }
}

Both codes ensure that the rs and ps are closed. However, if the database query code or the result processing code throws a SQLException and then one of the rs.close() and ps.close() methods also throws a SQLException, the SQLException thrown by the printData(String sql) is different using try-with-resources statement from try-finally statement. With the try-with-resources statement, the printData(String sql) throws the SQLException occurred from the database query code or the result processing code. Whereas the printData(String sql) throws the SQLException generated by the close() method if the  try-finally block is used.


3. Examples of including more than one resources.

try (BufferedReader reader = new BufferedReader(new FileReader(file));
             BufferedWriter writer = new BufferedWriter(new FileWriter(file2, true))) {
      String line;
      while (line = reader.readLine() != null) {
            writer.write(line);
      }
}

public void printData(String sql) throws SQLException {    
      try (PreparedStatement ps = con.prepareStatement(sql); ResultSet rs = ps.executeQuery()) {
            //code processing the ResultSet.
            while (rs.next()) {
                  System.out.println(rs.getString(1));
            }
      }

}
---------------------------------------------------------------------------------------------------------

                        

If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Monday, March 31, 2014

Is Interface going to replace Abstract Class - Java 8?

In Java 8, interface is altered so that it can have abstract methods and default methods. With this alteration, the methods we used to put in an abstract class can now be moved to the interface. Does this mean that we do not need abstract class anymore?

With interfaces in java 8 can have default and abstract methods, it is likely that we are going to create less abstract classes. However, it is unlikely that abstract class is going to be totally replaced by interface, because in an interface, you cannot put private, protected, non-static, non-final fields and non-static and non-default methods.

For example, you have a Car interface:

      public interface Car {
              public void run();
              public String getCarType();

              default boolean readyToDrive() {
                    return  true;
             }
      }

Surely, you don't want to put the carType field in the interface, which automatically becomes final and static. If you can only implement the run method when you are dealing with a specific type of car, you need an abstract class to implement the getCarType method.

The advantages of using interface over abstract class
An interface can extends one or multiple other interfaces, but cannot extends any class.
A class can implement one or multiple interfaces, but can only extends one class.

The advantages of using abstract class over interface
An abstract class can have protected and private fields and methods.

--------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.