Archive for category Core Java olmazsa olmazlar

java 7 comparator,condition Check

package list;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class MyClass {

public static void main(String[] args) {

List people = Arrays.asList(new Person(“Remzi”, “Altan”, 23), new Person(“Ali”, “Tuzen”, 23),
new Person(“Funda”, “Tekin”, 24), new Person(“Ceyda”, “Ozbay”, 25));

Collections.sort(people, new Comparator() {

public int compare(Person ob1, Person ob2) {

return ob1.getName().compareTo(ob2.getName());


printCond(people, new Condition() {

public boolean match(Person p) {

return p.getLastname().startsWith(“T”);


private static void printCond(List people, Condition condition) {
for (Person p : people) {
if (condition.match(p)) {
System.out.println(p.getName() + ” ” + p.getLastname() + ” ” + p.getAge());




interface Condition {
boolean match(Person p);



What is the use of the finally block? Is finally block in Java guaranteed to be called? When finally block is NOT called?

Finally is the block of code that executes always. The code in finally block will execute even if an exception is occurred. Finally block is NOT called in following conditions

  • If the JVM exits while the try or catch code is being executed, then the finally block may not execute. This may happen due to System.exit() call.
  • if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.
  • If a exception is thrown in finally block and not handled then remaining code in finally block may not be executed.
  • Leave a comment

    Difference between StringBuffer, String and StringBuilder in Java

    In summary here are list of difference between StringBuffer, String and StringBuilder in Java :

    1) String is immutable while StringBuffer and StringBuilder is mutable object.
    2) StringBuffer is synchronized while StringBuilder is not which makes StringBuilder faster than StringBuffer.
    3) Concatenation operator “+” is internal implemented using either StringBuffer or StringBuilder.
    4) Use String if you require immutability, use Stringbuffer in java if you need mutable + thread-safety and use StringBuilder in Java if you require mutable + without thread-safety.

    That’s all on famous String vs StringBuffer or StringBuffer vs StringBuilder discussion. All these differences helps to avoid common coding mistake of using String in place of StringBuffer in many places. from Java 5 onwards either use + operator of StringBuilder for concatenating String in Java.

    Read more:

    Leave a comment