Wednesday, February 6, 2013

== vs equals in Strings in Java

The equals( ) method compares the characters inside a String object. The == operator compares two object references to see whether they refer to the same instance. The following program shows how two different String objects can contain the same characters, but references to these objects will not compare as equal:

package
blogspot.sbollam.com
 public class Sample {
 public Sample()
 {
  System.out.println("Sample.Sample()");
 }

 /**
  * @param args
  */
 public static void main(String[] args)
 {


  String s1= new String("abc");
  String s2= new String("abc");
  String s3 ="abc";
  String s4 ="abc";
  System.out.println(s1==s3);
  System.out.println(s1==s2);
  System.out.println(s1.equals(s2));
  System.out.println(s1.equals(s3));
  System.out.println(s3.equals(s4));
  System.out.println(s3==s4);
  /*OUTPPUT:
   *
   * false false true true true true
   * */
 }

}


KNOWLEDGE HUB: Java Openigns -Infoys

KNOWLEDGE HUB: Java Openigns -Infoys: Hi All, In Infosys  Openings are there on Java 3+ . Please send u r profiles to sambamca06@gmail.com Note:Plz send only genuine profi...

StringUtils.isNotEmpty() VS StringUtils.isNotBlank() in Java

isNotEmpty

public static boolean isNotEmpty(String str)
Checks if a String is not empty ("") and not null.
 StringUtils.isNotEmpty(null)      = false
 StringUtils.isNotEmpty("")        = false
 StringUtils.isNotEmpty(" ")       = true
 StringUtils.isNotEmpty("bob")     = true
 StringUtils.isNotEmpty("  bob  ") = true
 



Parameters:
str - the String to check, may be null

Returns:
true if the String is not empty and not null



isNotBlank

public static boolean isNotBlank(String str)
Checks if a String is not empty (""), not null and not whitespace only.
 StringUtils.isNotBlank(null)      = false
 StringUtils.isNotBlank("")        = false
 StringUtils.isNotBlank(" ")       = false
 StringUtils.isNotBlank("bob")     = true
 StringUtils.isNotBlank("  bob  ") = true
 



Parameters:
str - the String to check, may be null

Returns:
true if the String is not empty and not null and not whitespace








Java Openigns -Infoys

Hi All,

In Infosys  Openings are there on Java 3+ .

Please send u r profiles to sambamca06@gmail.com

Note:Plz send only genuine profiles.

Pawar Star Pavan Kalyan








StringUtils.isEmtpty() vs StringUtils.isBlank() in Java



1) isEmpty

public static boolean isEmpty(String str)
Checks if a String is empty ("") or null.

StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false


2) isBlank

public static boolean isBlank(String str)
Checks if a String is whitespace, empty ("") or null.

StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false

Difference between NULL and empty string in Java

What is difference between null string (String abc = null) and empty string (String abc = "") ?

Analysis:
--------
Try to call a method on both of them and you'll see the difference


  1. String s = "";   s.length();  
you actually declare a new String Object, and it means the variable (in this case - s) has a String Object associated to it. you can call String methods on that object.
it is like typing:
              String s = new String();
  if you type:   String s = "";   s.lentgh; // return 0  

   2.   String s = null;   s.length();
    s.lentgh;//returns a null pointer exception

 Basically a reference to an empty string points to an object in the heap so you can call methods on it. But a reference pointing to null has no object to point in the heap and thus you'll get a Null Pointer Exception