Binary Search in Java

Binary search is an optimized algorithm to perform searching operation. It follows Divide and Conquer approach for searching. Pre-condition of Binary search : To perform Binary search the array should be sorted in ascending or descending order. Logic of Binary Search Let us consider array is sorted in ascending order. Now we compare the element … Read more

How to exit a program in Java

In Java if we want to terminate the execution of the program then we make use of System.exit(n) method. Let us see important points : System.exit(n) method takes a non zero argument to indicate abnormal termination. System.exit(n) will exit the program irrespective of the operation being performed. If we call System.exit(n) within a try catch … Read more

Escape Sequence in Java

Escape sequence is a character which is preceded by the backslash (\). Compiler has special meaning for escape sequences. Use of Escape Sequences Sometimes we may need to print some text within double quotes or we may need insert a new line in between texts. So these escape sequences were introduced to perform these special … Read more

Constructor Chaining in Java

Constructor chaining is the way through which a constructor make a call to another constructor. Constructor chaining can be done in 2 ways: Output In the above code we called Example2() constructor for object creation which in turn calls Example2(int age) constructor using this(29). Now inside Example2(int age) constructor a call is made to Example2(int … Read more

Constructor Overloading

Constructor overloading is the way through which multiple constructors can be defined within the class by changing the arguments of the signature. Let us understand this with an example: Output In the above code we have overloaded the constructor by changing the number of parameters in signature. We can use any constructor for creation of … Read more