Thursday, February 7, 2013

What happens when a Static Variable has the same name as a Static Class in Java?


public
class Test {
public static void main(String[] args) {
System.out.println(X.Y.Z);
  }
}

class
X {
static class Y {
static String Z = "Black";
}
static C Y = new C();
}
 

class
C {
String
Z = "White";
}

What Does It Print? Do not run it in eclipse and tell me the answer. I need to know why as well?

Ans: White


Reason :Outer static variable priority is higher than inner static variable.. 
You're hiding the class Y with a static instance of C named Y. The class Y is still there and can be used.

Try:
System.out.println(X.Y.Z);
System.out.println((new X.Y()).Z);
The output should be:
White
Black

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