`
happy_tao_cool
  • 浏览: 17733 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

"".equal(String)与Stirng.equal("")的区别

    博客分类:
  • java
阅读更多
public class EqualDemo {

	public static void equalByStringInFront(String str){
		try{
			if(str.equals("")){
				System.out.println("字符串变量在前面的调用equal方法,返回true");
			}
		}catch(NullPointerException e){
			e.printStackTrace();
		}
	}
	
	public static void equalByConstantInFront(String str){
		if("".equals(str)){
			System.out.println("字符串常量在前面的调用equal方法,返回true");
		}else{
			System.out.println("字符串常量在前面的调用equal方法,返回false");
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String str = "";
		String str2 = null;
		EqualDemo.equalByStringInFront(str);
		EqualDemo.equalByConstantInFront(str);
		
		EqualDemo.equalByStringInFront(str2);
		EqualDemo.equalByConstantInFront(str2);
	}
	
}


运行以上代码,输出结果:

字符串变量在前面的调用equal方法,返回true
字符串常量在前面的调用equal方法,返回true
java.lang.NullPointerException
	at com.travelsky.equal.frontandback.EqualDemo.equalByStringInFront(EqualDemo.java:7)
	at com.travelsky.equal.frontandback.EqualDemo.main(EqualDemo.java:32)
字符串常量在前面的调用equal方法,返回false


当输入的string变量为空的话,string.equal("")方式的调用就会报出空指针异常。所以在进行字符串相等比较的时候,除非两个比较的参数都是变量,否则建议使用“”.equal(string)方法
分享到:
评论

相关推荐

    Java中Object.equals和String.equals的区别详解

    主要给大家介绍了Java中Object.equals和String.equals的区别,文中通过一个小示例让大家轻松的明白这两者的区别,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。

    string-hashcode:java.lang.String.hashCode

    安装npm install string-hashcode 例子var hashCode = require ( 'string-hashcode' ) ;var s = 'abc' ;console . log ( s . hashCode ) ; // undefinedvar code = hashCode ( s ) ;console . log ( s . hashCode ) ...

    hibernate sprint 封装应用

    fls.add(new QCls("YXDM", QCls._STRING_, QCls._F_EQUAL_, yx).getThis()); fls.add(new QCls("LQZY", QCls._STRING_, QCls._F_EQUAL_, zy).getThis()); fls.add(new QCls("SF", QCls._STRING_, QCls._F_EQUAL_, sf...

    MongoDB的Go开发包gomongo.zip

    gomongo 是 Go 语言访问 MongoDB 数据库的开发包。 示例代码: package main ... func main() { conn, _ := mongo.Connect(... mongo.Equal(doc, got) // true! collection.Drop() } 标签:gomongo

    xcompare:运算符比较库

    boolValue := xcompare.Equal.String("a","a") fmt.Println(boolValue) //true 不等于(不等于) 支持的类型 细绳 整数32 整数64 浮点32 浮动64 使用 boolValue := xcompare.NotEqual.String("a","b") fmt.Println...

    Doctrine ORM for PHP.pdf

    Table of Contents Introduction....................................................................................................13 Code Examples.........................................................

    to-camel-case:字符串到骆驼案例

    toCamelCase(string, first = false) toCamelCase ( "hello-ya" ) . should . equal ( "helloYa" ) toCamelCase ( "chocolate-rain" ) . should . equal ( "chocolateRain" ) toCamelCase ( "hello-howare-ya" , ...

    浅析java中String类型中“==”与“equal”的区别

    主要介绍了浅析java中String类型中“==”与“equal”的区别,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

    Visual Studio 2010 TDD 测试驱动开发 实战 视频及源码

    string name = "yongfa365"; string strOK = "hello,yongfa365"; HelloWord hello = new HelloWord(); string result = hello.Say(name); Assert.AreEqual(strOK, result); string name = this.TestContext....

    string-obfuscator:编码和解码(加密和解密)字符串。 支持随机长度编码值

    字符串混淆器编码和解码(加密和解密)字符串。 支持随机长度编码值。用法 var Obfuscator = require('string-obfuscator');var obfuscator = new Obfuscator({algorithm: 'aes-256-ctr', ...assert.equal(decoded,

    unbox-primitive:取消装箱的JS基本值的装箱

    拆箱原始 取消装箱的JS基本值的装箱。 此模块可以跨领域/ iframe运行,... equal ( unboxPrimitive ( new String ( 'f' ) ) , 'f' ) ;assert . equal ( unboxPrimitive ( new Number ( 42 ) ) , 42 ) ;const s = Symbol

    junit的jar包

    org.hamcrest.core.IsEqual.class org.hamcrest.core.IsInstanceOf.class org.hamcrest.core.IsNot.class org.hamcrest.core.IsNull.class org.hamcrest.core.IsSame.class org.hamcrest.internal.ArrayIterator....

    bbmustache::dragon_face:将胡须模板渲染为字符串

    bbmustache Gleam绑定到模板库。 import gleam / ... equal (rendered, "Hello, World!" ) } 快速开始 # Build the project rebar3 compile # Run the eunit tests rebar3 eunit # Run the Erlang REPL reba

    tapef:带有Mocha错误输出的Tape API。 骇客

    Tapef 磁带与摩卡咖啡毫不相称地繁殖。 从字面上看,摩卡的记者侵入了Tape。 我使用它的方式是在比较大对象或字符串时...test ( 'equal string' , function ( t ) { var a = 'apple' var b = 'banana' t . equal

    C++用类实现大数相加

    void equal(string x,string y,int n1,int n2); void nequal(string x,string y,int n1,int n2); private : string s1,s2; }; void ds::equal (string s1,string s2,int n1,int n2) {//两个字符串一样长的时候; ...

    SqlBuilder:该项目是一个SQL语句生成器,允许您用Java编写SQL

    equal(fruit_name, " apple " )); 由于输出sql是: DELETE FROM ` fruit ` WHERE ` name ` = " apple " ; 1.插入 String sql = new Insert ( String table) .addAll( Map< String> cv) // by map .add

    mcsphard,an important np-c question

    called the blocks, whose concatenation is equal to A. Given a partition P of a string A and a partition Q of a string B, we say that the pair hP, Qi is a common partition of A and B if Q is a ...

    5fanily,封装,==equal.rar

    /* 定义有final的变量为最终变量不能为其赋值. ... public static void main(String args[]) { Test1 t1=new Test1(); //t1.a=33;//无法为最终变量 a 指定值 System.out.println(t1.a); } }

    学习node.js 断言的使用详解

    assert模块提供了一组简单的断言测试,分严格模式(strict)和遗留模式(legacy),严格...全局使用strict模式后,assert.equal() 与assert.strictEqual()的效果是一样的。 如果不想全局使用的话,可以直接使用带strict

    calculator.java

    JButton equal=new JButton("="); JButton point=new JButton("."); JButton plus=new JButton("+"); JButton minus=new JButton("-"); JButton multi=new JButton("×"); JButton division=new JButton("÷")...

Global site tag (gtag.js) - Google Analytics