Can not issue NULL query

本文通过一个具体的案例,深入解析了在Java中使用if语句判断String类型变量时,使用==与equals方法的区别。文章解释了为何在某些情况下使用==进行字符串比较会导致错误的结果,并给出了正确的做法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述: 判断一个用户的性别,是男生就用“-1”判断,是女生就用“1”来判断,根据男女不同,执行的sql语句也不同。


前期我写了一个:String sql=null;

接着就是用if语句判断男女,在if里面为sql赋值。

但是测试的时候报错了,null query,debug的时候发现根本没有执行if语句。


原因在这里:

if(gender=="-1"){
			sql22 = "。。。。。。。。。。" ;
		}
		else if(gender=="1"){
			 sql22 = "。。。。。。。。。" ;
		}else{
			group.setResult_code("SQLException");
		}


因为gender是一个String类,String类是覆写了equals方法的,所以用gender.equals()是判断字符串的内容,而不是我以为的内存地址。

这里再附上 ==和euqals的区别:


1. == 是一个运算符。
2.Equals则是string对象的方法,可以.(点)出来。
我们比较无非就是这两种 1、基本数据类型比较 2、引用对象比较
  1、基本数据类型比较

  ==和Equals都比较两个值是否相等。相等为true 否则为false;

如果String缓冲池内不存在与其指定值相同的String对象,那么此时虚拟机将为此创建新的String对象,并存放在String缓冲池内。

如果String缓冲池内存在与其指定值相同的String对象,那么此时虚拟机将不为此创建新的String对象,而直接返回已存在的String对象的引用。

  2、引用对象比较

  ==和Equals都是比较栈内存中的地址是否相等 。相等为true 否则为false;


需注意几点:

  1、string是一个特殊的引用类型。对于两个字符串的比较,不管是 == 和 Equals 这两者比较的都是字符串是否相同;   2、当你创建两个string对象时,内存中的地址是不相同的,你可以赋相同的值。   所以字符串的内容相同。引用地址不一定相同,(相同内容的对象地址不一定相同),但反过来却是肯定的;

  3、基本数据类型比较(string 除外) == 和 Equals 两者都是比较值;
总结出来就是:
1、同一对象,"=="和equals结果相同
2、如果值不相同,对象就不相同,所以"==" 和equals结果一样
3、对象不同,内容相同,"=="返回false,equals返回true


项目创建后有如下错误: AndroidManifest.xml文件的错误: 无法解析符号 '@style/Theme.FloatingSearchApp' READ_EXTERNAL_STORAGE is deprecated (and is not granted) when targeting Android 13+. If you need to query or interact with MediaStore or media files on the shared storage, you should instead use one or more new storage permissions: `READ_MEDIA_IMAGES`, `READ_MEDIA_VIDEO` or `READ_MEDIA_AUDIO`. Redundant label can be removed DatabaseHelper.kt文件的错误: 应为 <database name> 或 <defined table name>,得到 '$' Value must be ≥ 0 but `getColumnIndex` can be -1 Parameter 'question' is never used FloatingWindowService.kt文件的错误: Avoid passing `null` as the view root (needed to resolve layout parameters on the inflated layout's root element) 'TYPE_PHONE: Int' is deprecated. Deprecated in Java `onTouch` should call `View#performClick` when a click is detected Do not concatenate text displayed with `setText`. Use resource string with placeholders. String literal in `setText` can not be translated. Use Android resources instead. Variable 'answer' is never used Do not concatenate text displayed with `setText`. Use resource string with placeholders. String literal in `setText` can not be translated. Use Android resources instead. MainActivity.kt文件的错误: Unresolved reference: activity_main This declaration overrides deprecated member but not marked as deprecated itself. Please add @Deprecated annotation or suppress. See https://youtrack.jetbrains.com/issue/KT-47902 for details TextRecognizer.kt文件的错误: No value passed for parameter 'options' 'getter for defaultDisplay: Display!' is deprecated. Deprecated in Java 'getMetrics(DisplayMetrics!): Unit' is deprecated. Deprecated in Java Parameter 'context' is never used floating_window.xml文件的错误: Hardcoded string "问题将显示在这里", should use `@string` resource Hardcoded string "答案将显示在这里", should use `@string` resource
最新发布
06-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值