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的区别,文中通过一个小示例让大家轻松的明白这两者的区别,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
安装npm install string-hashcode 例子var hashCode = require ( 'string-hashcode' ) ;var s = 'abc' ;console . log ( s . hashCode ) ; // undefinedvar code = hashCode ( s ) ;console . log ( s . hashCode ) ...
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...
gomongo 是 Go 语言访问 MongoDB 数据库的开发包。 示例代码: package main ... func main() { conn, _ := mongo.Connect(... mongo.Equal(doc, got) // true! collection.Drop() } 标签:gomongo
boolValue := xcompare.Equal.String("a","a") fmt.Println(boolValue) //true 不等于(不等于) 支持的类型 细绳 整数32 整数64 浮点32 浮动64 使用 boolValue := xcompare.NotEqual.String("a","b") fmt.Println...
Table of Contents Introduction....................................................................................................13 Code Examples.........................................................
toCamelCase(string, first = false) toCamelCase ( "hello-ya" ) . should . equal ( "helloYa" ) toCamelCase ( "chocolate-rain" ) . should . equal ( "chocolateRain" ) toCamelCase ( "hello-howare-ya" , ...
主要介绍了浅析java中String类型中“==”与“equal”的区别,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
string name = "yongfa365"; string strOK = "hello,yongfa365"; HelloWord hello = new HelloWord(); string result = hello.Say(name); Assert.AreEqual(strOK, result); string name = this.TestContext....
字符串混淆器编码和解码(加密和解密)字符串。 支持随机长度编码值。用法 var Obfuscator = require('string-obfuscator');var obfuscator = new Obfuscator({algorithm: 'aes-256-ctr', ...assert.equal(decoded,
拆箱原始 取消装箱的JS基本值的装箱。 此模块可以跨领域/ iframe运行,... equal ( unboxPrimitive ( new String ( 'f' ) ) , 'f' ) ;assert . equal ( unboxPrimitive ( new Number ( 42 ) ) , 42 ) ;const s = Symbol
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 Gleam绑定到模板库。 import gleam / ... equal (rendered, "Hello, World!" ) } 快速开始 # Build the project rebar3 compile # Run the eunit tests rebar3 eunit # Run the Erlang REPL reba
Tapef 磁带与摩卡咖啡毫不相称地繁殖。 从字面上看,摩卡的记者侵入了Tape。 我使用它的方式是在比较大对象或字符串时...test ( 'equal string' , function ( t ) { var a = 'apple' var b = 'banana' t . equal
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) {//两个字符串一样长的时候; ...
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
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 ...
/* 定义有final的变量为最终变量不能为其赋值. ... public static void main(String args[]) { Test1 t1=new Test1(); //t1.a=33;//无法为最终变量 a 指定值 System.out.println(t1.a); } }
assert模块提供了一组简单的断言测试,分严格模式(strict)和遗留模式(legacy),严格...全局使用strict模式后,assert.equal() 与assert.strictEqual()的效果是一样的。 如果不想全局使用的话,可以直接使用带strict
JButton equal=new JButton("="); JButton point=new JButton("."); JButton plus=new JButton("+"); JButton minus=new JButton("-"); JButton multi=new JButton("×"); JButton division=new JButton("÷")...