What would you use to compare two String variables the operator == or the method equals() in java?

  1. We use String compare using equels() methods. Its compare both String's contents.
  2. In case using = = then check both String object's memory address.
  3. If you know more info about, how do handling String object in JVM? More...

 class StringCompare  
 {  
      public static void main(String[] arg)  
      {  
           System.out.println("String comparition difference between == and equals()");  
           String str1 = "hello";  
           String str2 = "hello";  
           String str3 = new String("hello");  
           String str4 = new String("hello");  
           System.out.println("str1 : "+str1);  
           System.out.println("str2 : "+str2);  
           System.out.println("str3 : "+str3);  
           System.out.println("str4 : "+str4);  
           System.out.println("==");  
           System.out.print("str1 == str2 : ");  
           System.out.println(str1 == str2);  
           System.out.print("str1 == str3 : ");  
           System.out.println(str1 == str3);  
           System.out.print("str3 == str4 : ");  
           System.out.println(str3 == str4);  
           System.out.println("equals()");  
           System.out.print("str1.equals(str2) : ");  
           System.out.println(str1.equals(str2));  
           System.out.print("str1.equals(str3) : ");  
           System.out.println(str1.equals(str3));  
           System.out.print("str3.equals(str4) : ");  
           System.out.println(str3.equals(str4));  
      }  
 }  

Output:


Difference between daemon thread and non-daemon thread in java

  • Daemon term is mainly used in UNIX. 
  • In Java, this is used to indicate a special type of thread. 
  • Normally when a thread is created in Java, by default it is a non-daemon thread. 
  • Whenever a Java Program is executed, the Java Virtual Machine (JVM) will not exit until any non-daemon threads are still running. 
  • This means if the main thread of an application ends and the remaining threads left are Daemon threads, then the JVM will exit killing all the daemon threads without warning.
  • A daemon thread should be used for some background task that might provide a service to the applications. 
  • e.g. a Server thread listening on a port for the clients` requests. A thread for which an exit method is not provided or which does not have an exit mechanism can be marked as a daemon thread.
 /*  
 Daemon term is mainly used in UNIX. In Java, this is used to indicate a special type of thread.   
 Normally when a thread is created in Java, by default it is a non-daemon thread.   
 Whenever a Java Program is executed, the Java Virtual Machine (JVM) will not exit until any non-daemon threads are still running.   
 This means if the main thread of an application ends and the remaining threads left are Daemon threads,   
 then the JVM will exit killing all the daemon threads without warning.  
 A daemon thread should be used for some background task that might provide a service to the applications.   
 e.g. a Server thread listening on a port for the clients` requests.   
 A thread for which an exit method is not provided or which does not have an exit mechanism can be marked as a daemon thread.   
 */  
 public class DaemonThread implements Runnable   
 {  
      public void run()   
      {  
           System.out.println("Entering run()");  
           try   
           {  
                System.out.println("Inside run(): currentThread() is :: "+ Thread.currentThread());  
                while (true) {  
                     try{  
                      Thread.sleep(500);  
                     }   
                     catch (InterruptedException x) {  
                     }  
                     System.out.println("Inside run(): Wake up Again");  
                }  
           }  
           finally {  
            System.out.println("Leaving run()");  
           }  
      }  
      public static void main(String[] args) {  
           System.out.println("Enter to Main()");  
           Thread t = new Thread(new DaemonThread());  
           //Not contain setDaemon(true) is called non-daemon thread.  
           //See this program output.  
           //After uncomment following line. then see the program's output.  
           //You easy to understand, what is different between daemon thread and non-daemon thread.  
           //t.setDaemon(true);  
           t.start();  
           try {  
            Thread.sleep(3000);  
           } catch (InterruptedException x) {  
           }  
           System.out.println("Leaving Main()");  
      }  
 }