String Object vs String literal :
when we compare a String literal with an String object created using new() operator using == operator, it will return false. Both are pointing same object only then why it return false. Anyone clarify my doubt...
String a = new String("java");
String b = "java";
System.out.println(a == b); // False
(This is concept of String constant pool) Actually when you use any string literal in your java code that time jvm allocate one memory slot for that literal. With String literal if you use same literal anywhere else jvm first finds that literal in String constant pool if it present there then jvm does not allocate new memory space for that literal.It directly picks up that same object (String literal) from string constant pool but if you create String object using new keyword then jvm create one object in heap memory and it finds literal("java") whatever argument you passed inside new String("java") in string constant pool if it present in string constant pool then jvm will pick up that object from string constant pool and jvm will pass that literal (String object) inside new String("Object passed from String constant pool") and if literal is not present in string constant pool then jvm first allocate memory for that literal then It will pick up the recently create literal object from String constant pool so If you try to compare both objects it will be different because of both objects have different reference codes .