Date Archives August 7, 2016

[String] concat() vs. +

Screen Shot 2016-08-07 at 4.44.23 PM

concat() and + both add up strings. It looks same, but + do one more step. As seen below, + will create string builder and append the strings inside, and then it will give the string. So, concat() is less expensive.

/*      Using + sign      */
String myStr = "str";
myStr += "end";

// Same as above
String myStr = "str";
myStr = new StringBuilder().append(myStr).append("end").toString();


/*      Using concat()     */
String myStr = "str";
myStr = myStr.concat("end");



Definition & steps

DHCP stands for Dynamic Host Configuration Protocol. When DHCP assigns an IP address, it actually leases that connection identifier to the user’s computer for a specific amount of time. The default lease is five days.


  • Because there is certain lease duration, if not using after the duration, they will automatically collect it after the lease. So, it can be used for other computer.
  • Because you don’t need to set the IP one by one, if you move to other office and connect to the that network, it will automatically assign an IP for you.

Screen Shot 2016-08-07 at 12.36.24 PM

How DHCP works

  • Discover: client computer sends a broadcast request looking for a DHCP server to answer.
  • Router directs the DISCOVER packet to the correct DHCP server.
  • The server receives the DISCOVER packet. Based on availability and usage policies set on the server, the server determines an appropriate address (if any) to give to the client.
  • Offer: The server then temporarily reserves that address for the client and sends back to the client an OFFER packet, with that address information.
  • Request: Letting the server know that it intends to use the address.
  • ACK: Confirming that the client has a been given a lease on the address for a server-specified period of time.



Error & Exception

Error & Exception

Screen Shot 2016-08-06 at 10.39.14 PM

Error is thrown by JVM in a scenario which is fatal and there is no way for the application program to recover from that error.

  • Unchecked type: Errors happen at run time. They will not be known to compiler
  • Cannot recover
  • eg. java.lang.StackOverflowError, java.lang.OutOfMemoryError

Exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime.

  • Unchecked & Checked type: checked exceptions are known to compiler where as unchecked exceptions are not known to compiler because they occur at run time.
  • Can recover from exceptions by handling then through try-catch blocks.


Types of exceptions

  • Checked Exception: The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions. Checked Exceptions are checked at compile-time.
    • e.g.IOException, SQLException etc.
  • Unchecked Exception: Unchecked exceptions are not checked at compile-time rather they are checked at runtime.
    • e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.


Unchecked Exception scenarios

  • ArithmeticException: If we divide any number by zero, there occurs an ArithmeticException.
int a = 50/0;


  • NullPointerException: If we have null value in any variable, performing any operation by the variable occurs an NullPointerException.
String s = null;


  • NumberFormatException: The wrong formatting of any value, may occur NumberFormatException. Suppose I have a string variable that have characters, converting this variable into digit will occur NumberFormatException.
String s = "abc";
int i = Integer.parseInt(s);


  • ArrayIndexOutOfBoundsException: If you are inserting any value in the wrong index, it would result ArrayIndexOutOfBoundsException as shown below.

int a[] = new int[5];

a[10] = 50;


Handling checked exceptions

Due to compilation error, checked exceptions need to be handled by programmer. There are two ways to handle these exceptions.

  • Throw when declaring method
  • Try, catch, and finally inside the method


Throw when declaring method: Toss the exception handling to user when using the method.

public void connect() throws MyException{
   // try ~ catch ~ finally will not be used


Try, catch, and finally inside the method: Handling exception inside the method

public void connect(){

      // Do work here
   } catch (MyException e){
      // Catch lower Exception first
   } catch (Exception e){
      // Highest Exception at the end

      // There are three ways to see the excetption
   } finally {
      // Code on here will be always printed

  • e.printStackTrace(): Name of the Exception, Message of the Exception, Stack trace
    • On example code above:
java.lang.ArithmeticException: /by Zero
   at main()
  • e.toString(): Name of the Exception, Message of the Exception
    • On example code above:
java.lang.ArithmeticException: /by zero
  • e.getMessage(): Message of the Exception
    • On example code above:
/by zero

Creating your own Exception

Extends Exception classe: you can make the most suitable exception for your program.

public class MyException extends Exception{
   public Myexception(String msg){