Category: Java



AccessDeniedException is a  Checked exception thrown when a file system operation is denied. Typically due to a file permission or other access check.

Below is an example where this Exception is thrown by the Files.write() method because it can’t find a file at “C:\\test\\” directory.

Path path = Paths.get("C:\\test\\");

try {
	Files.write(path, new byte[] {127,0,0,1}); //will throw java.nio.file.AccessDeniedException
} catch (IOException e) {

java.nio.file.AccessDeniedException: C:\test
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(Unknown Source)
at java.nio.file.spi.FileSystemProvider.newOutputStream(Unknown Source)
at java.nio.file.Files.newOutputStream(Unknown Source)
at java.nio.file.Files.write(Unknown Source)
at test2.Main.main(
Filed under: Exceptions, JavaTagged with: , , , ,

Comparing Strings in Java

How to compare two Strings in Java?

To compare two Strings in Java use the .equals() method of the String Class.
Don’t use == to compare Strings, you might get unexpected results.

String hello  = "Hello";
String hello2 = "Hello";
String hello3 = new String("Hello");
String world  = "World";
//Objects default hashCode
System.out.println( System.identityHashCode(hello)   );     //366712642
System.out.println( System.identityHashCode(hello2)  );     //366712642
System.out.println( System.identityHashCode(hello3)  );     //1829164700
System.out.println( System.identityHashCode("Hello") );     //366712642
System.out.println( System.identityHashCode("World") );     //2018699554

//Comparing with .equals 
// .equals method compares the actual string characters
System.out.println(  "Hello".equals(hello)   );     //true 
System.out.println(  hello.equals(hello2)    );     //true
System.out.println(  hello.equals(hello3)    );     //true
System.out.println(  hello.equals(world)     );     //false
System.out.println(  "Hello".equals("World") );     //false

//comparing with ==
// == compares the reference values
System.out.println(  hello == hello2     );    //true
System.out.println(  hello == hello      );    //true
System.out.println(  hello == hello3     );    //false
System.out.println(  hello == "Hello"    );    //true
System.out.println(  "Hello" == "Hello"  );    //true

The .equals() method is case sensitive, whereas the .equalsIgnoreCase() is not case sensitive.

//Case Sensitive
System.out.println( "Hello World".equals("hELlo WOrlD")            );    //false
//Case Insensitive 
System.out.println( "Hello World".equalsIgnoreCase("hELlo WOrlD")  );    //true
Filed under: Java, StringTagged with: ,

RuntimeException in Java

RuntimeException is a sublclass of Exception that can be extended to create unchecked Exceptions.


Unchecked Exceptions are those Exceptions that don’t have to be caught on compile time, where as Checked Exceptions do have to be caught on compile time.

A NullPointerException is a subclass of RuntimeExceprion. You aren’t required to catch a NullPointerException.

Object a = null;
a.toString(); // throws NullPointerException

A IOException is direct subclass of Exception so it is a Checked Exception and you are required to catch it.

try {
    //thorows IOException
    FileReader fr = new FileReader(filePath);
} catch (IOException e) {


Filed under: Exceptions, JavaTagged with: , ,

null and Null Pointer Exception in Java – java.lang.NullPointerException

What is null in Java?

In Java null is just a special value of type literal just like: 1, 1.0, ‘a’, “abc”, etc;
You can think of null as either no value is assigned to a reference variable or the reference variable is pointing to nothing.

Object a;
// is  the same as
Object a = null;

You can cast null to any type

Object a = null;
Object a2 = (Object)null

String s = null
String s2 = (String)null

Integer i = null
Integer i2 = (Integer)null

What is NullPointerException?

NullPointerException is a RuntimeException.


NullPointerException is thrown when you use an object reference that has value null.

public class Car{
    public String color = "red";
    public void start(){
        //start the engine

Car myCar = null;
int[] myArr = null;

//Invoking a method from a null object

//Accessing or modifying a null object’s field
myCar.color = "black";

//Taking the length of null, as if it were an array
System.out.println( myArr.length );

//Accessing or modifying the slots of null object, as if it were an array
myArr[1] = 10;

//Throwing null, as if it were a Throwable value
public void foo() throws Exception{
    Exception e = null;
    throw e;



Filed under: JavaTagged with: , ,