What are checked and unchecked Exceptions in Java?

What are Checked Exceptions?

If you write a piece of code that is logically correct but can fail in certain scenarios then you need to handle those scenarios. So for example if you write code which reads a File and suppose at run time the File is not available then it will throw an Exception. So you can handle this by catching a FileNotFoundException.

What are Runtime Exceptions?

Suppose you write a piece of code which is logically not correct. Say you write code that does not use an API correctly then your code is buggy. So no point handling exception here. Let the error be printed in the log so that you can make corrections to the code.

The application can catch this exception, but it probably makes more sense to eliminate the bug that caused the exception to occur.

What are Errors?

When the exception is beyond your application’s scope. Like some hardware issue. Say at runtime some I/O error occurs.

Both Runtime and Error are unchecked. You don’t need to handle them.

Top 5 features added to Java SE 9

Background

Java has been evolving at a good pace ever since it was introduced in early 90’s. With every passing year and each release it has tried to keep pace with emerging market demands.Java has been evolving at a good pace ever since it was introduced in early 90’s. With every passing year and each release it has tried to keep pace with emerging market demands.

Let’s take a look at the top 5 features added to Java 9

1. Modules
The result of Project Jigsaw, modules are the most important addition to Java 9.

  • Java is now modular which means the library packages are now organized as modules.
  • It changes the way we design and build Java Applications.
  • It is different from OSGi.
  • It helps Programmers to be more productive.
  • Managing and evolving modules is easier.

2. JShell : Read-Eval-Print loop for Java
A tool that allows you to develop and test Java code interactively.

Earlier:

class Sample{
	public static void main(String[] a){
		System.out.println("Hello World");
	}
}

And now using JShell you can print Hello without writing other stuff.
You can type JShell at command line to get started.
And write

System.out.println(“Hello !!”);

3. Private Methods in Interfaces

You can now add private methods to your Interfaces. This can help you to create  reusable code.

Example:

public interface Sample {

private void printMyStuff(String abc){

System.out.println(abc);

}

}

4. Collections
Earlier you would write

List<Employee> empList = new ArrayList<>();

empList.add(new Employee(1, “Ankur”));

empList.add(new Employee(2, “Rohan”));

Now you can write

List<Employee> empList = List.of(new Employee(1, “Ankur”), new Employee(2, “Rohan”));

Reason – Thanks to changes in Java 8. Interfaces can now have default methods.

5. Concurrency Updates

Java 8 introduced Streams. Java 9 has gone a step ahead and added

  • Reactive Streams publish-subscribe framework
  • Enhancements to CompletableFuture class which was introduced in Java 8.

Other Notable Changes

HTTP Client API to support HTTP/2 and WebSocketThe new incubator HTTP Client is capable of working with both HTTP/1.1 and HTTP/2

 

3 Key Updates in Java SE 8

What is new in Java 8?

Java has been evolving at a good pace since it was introduced in early 90’s. With every passing year it has tried to keep pace with emerging market demands.

Let’s take a look at 3 key updates introduced in Java 8:
1. Lambda Expression  

Lambda Expression adds functional programming features to Java. It changes the way programming solutions are conceptualized and the way code is written in Java. It reduces the amount of code needed to create certain constructs like Anonymous Classes.

2. Stream API

The Stream API supports pipeline operations on data. Stream API is designed with Lambda Expression in mind.Powerful way to handle data in an efficient way.You can perform very sophisticated operations that search, filter or manipulate data. Plus it can be done in Parallel.

3. Interface Methods

Another Lambda inspired feature is the ability to define a default implementation for a method in the Interface. This feature helps you to update the codebase without impacting the classes that implement an interface.

Other new features

1. New Date and Time API

2. Ability to use parallel processing when sorting an Array.

Key Considerations in Java Design

What Java intends to provide?

  • Simple
    Java should be simple to learn and use by the Professional Programmers.
  • Object Oriented Features
    The Object Model in Java is simple and easy to extend.
  • Robust
    Programs written in Java should be able to run reliably in any Environment.
  • Multithreading
    To support Concurrency and Parallelism
  • Architecture Neutral
    Thanks to Java Virtual Machine  
  • Interpreted and High Performance
  • Distributed
    Java supports Remote Method Invocation.

Apart from these Java is a strictly typed language so it checks your program at both compile and run time.

It also frees you from Memory Management activities which can cause major errors at run time, if not managed properly.

A simple QuickSort Program in Java

Introduction to Quick Sort

QuickSort algorithm is based on the Divide and Conquer Approach.

For a given sequence S there are 4 basic steps involved:

  • Pick a number X from S. This program picks the last element.
  • Create two Lists. One list will store elements less than X and the other will store greater than X.
  • Sort the two lists recursively.
  • Combine the left list elements with X and the right list elements.

The program below sorts a sequence of Integers. After which another version has been provided which can be used to sort a sequence of characters.These are my own versions of the QuickSort Algorithm.

Continue reading “A simple QuickSort Program in Java”