if (name == "") {
//do something
}
if (name.equals("")) {
//do something
}
if (!name.equals("")) {
//do something
}
if (name != null && !name.equals("")) {
//do something
}
if (!"".equals(name)) {//将""写在前头,这样,不管name是否为null,都不会出错。
//do something
}
public class Test {
public static void main (String args[]){
String value = null;
testNullOrEmpty(value);
value = "";
testNullOrEmpty(value);
value = " ";
testNullOrEmpty(value);
value = "hello me";
testNullOrEmpty(value);
}
static void testNullOrEmpty(String value){
if(value == null){
System.out.println("value is null");
} else if ("".equals(value)){
System.out.println("value is blank but not null");
} else {
System.out.println("value is \"" + value + "\"");
}
if (value == "") { //NG 错误的写法
//别用这种写法
}
}
}
c:\>javac TestNullOrEmpty.java c:\>java TestNullOrEmpty
value is null. value is blank but not null. value is " " value is "hello me!"
package com;
public class A
{
/**
* @param args
*/
public static void main(String[] args)
{
String a = "hello";
String b = "he";
String c = a.substring(0, 2);
System.out.println(b.equals(c));//true
System.out.println(b==c);//false
String d = new String("hello");
System.out.println(d.equals(a));//true
System.out.println(d==a);//false
String e = new StringBuilder("hello").toString();
System.out.println(e.equals(a));//true
System.out.println(e==a);//false
System.out.println(e.equals(d));//true
System.out.println(e==d);//false
String f = "hello";
System.out.println(f.equals(a));//true
System.out.println(f==a);//true
System.out.println(f=="hello");//true
System.out.println(f=="hell"+"o");//true
String g = b+"llo";
System.out.println(g==f);//false
String h = "he"+"llo";
System.out.println(h==f);//true
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有