android错误之==与equals的区别

本文详细解释了Java中==与equals的区别,并通过实例演示了两者在比较对象时的不同表现。==用于比较基本数据类型的值或引用类型的内存地址,而equals则用于比较对象的内容是否相等。

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

在做一个电话号码匹配的时候,

开始我使用的是这种情况

if (num == inCall.getNum()) {
//操作...
}

但是无论如何结果都是false,就算两个号码打印出来一模一样还是false,

突然想起来错在哪里了,改成如下

if (num.equals(call.getNum())) {
//操作...
}

结果不言而喻,正确了,这就是==和equals的区别了:

简单来说就是,==比较的是值,如果是int,long,或者float当然正确,只要值相等就相等了,

而equals比较的是实际的值,比如对于上面两个引用来说,他们的地址是不同,但是地址里存放的内容是相同的,

那么这时候用==比较就会得到false,用equas比较就得到true

表达能力不好,如果看不懂的话看下面的例子,看得懂的话就到此为止吧,别往下浪费时间了.

 

下面举例说明:

public class Equivalence {
  public static void main(String[] args) {
    Integer n1 = new Integer(47);
    Integer n2 = new Integer(47);
    System.out.println(n1 == n2);
    System.out.println(n1 != n2);
  }
}

表达式System.out.println(n1 == n2)可打印出内部的布尔比较结果。一般人都会认为输出结果肯定先是true,再是false,因为两个Integer对象都是相同的。但尽管对象的内容 相同,句柄(可以理解为两个对象在内存中的地址)却是不同的,而==和!=比较的正好就是对象句柄。所以输出结果实际上先是false,再是true。这 自然会使第一次接触的人感到惊奇。
若想对比两个对象的实际内容是否相同,又该如何操作呢?此时,必须使用所有对象都适用的特殊方法equals()。但这个方法不适用于“主类型”,那些类型直接使用==和!=即可。下面举例说明如何使用:

public class EqualsMethod {
  public static void main(String[] args) {
    Integer n1 = new Integer(47);
    Integer n2 = new Integer(47);
    System.out.println(n1.equals(n2));
  }
}

正如我们预计的那样,此时得到的结果是true。但事情并未到此结束!假设您创建了自己的类,就象下面这样:

class Value {
  int i;
}

public class EqualsMethod2 {
  public static void main(String[] args) {
    Value v1 = new Value();
    Value v2 = new Value();
    v1.i = v2.i = 100;
    System.out.println(v1.equals(v2));
  }
} 

此时的结果又变回了false!这是由于equals()的默认行为是比较句柄。所以除非在自己的新类中改变了equals(),否则不可能表现出我们希望的行为。但要注意equals()的这种行为方式同时或许能够避免一些“灾难”性的事件。
对于Object类,它提供了一个最最严密的实现,那就是只有是同一对象是,equals方法才返回true,也就是人们常说的引用比较而不是值比较。这个实现严密得已经没有什么实际的意义,所以在具体子类(相对于Object来说)中,如果我们要进行对象的值比较,就必须实现自己的equals方法。
大多数Java类库都实现了equals(),所以它实际比较的是对象的内容,而非它们的句柄。

作者:jason0539

微博:http://weibo.com/2553717707

博客:http://blog.youkuaiyun.com/jason0539(转载请说明出处)



我正在完成移动智能设备应用开发课程设计,设计要求是在Android Studio软件上设计一个简易的计算器。 以下是布局文件代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/whq" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="49吴俊廷" android:gravity="center" android:textColor="#000000" android:textSize="35sp" /> <TextView android:id="@+id/firstNumView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/operatorView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/secondNumView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/resultView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal"> <Button android:id="@+id/btnClear" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:background="#AEACB7DD" android:alpha="0.5" android:text="c" /> <Button android:id="@+id/btnBack" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:alpha="0.5" android:text="√" android:background="#AEACB7DD" /> <Button android:id="@+id/btnPercent" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:background="#AEACB7DD" android:alpha="0.5" android:text="%" /> <Button android:id="@+id/btnAdd" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:background="#AEACB7DD" android:alpha="0.5" android:text="+" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#AEACB7DD" android:orientation="horizontal"> <Button android:id="@+id/btn7" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:alpha="0.5" android:background="#AEE9F0FF" android:text="7" /> <Button android:id="@+id/btn8" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:alpha="0.5" android:background="#AEE5E9FC" android:text="8" /> <Button android:id="@+id/btn9" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:alpha="0.5" android:background="#AEE5E9FC" android:text="9" /> <Button android:id="@+id/btnSubtract" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:alpha="0.5" android:background="#AEE5E9FC" android:text="-" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal"> <Button android:id="@+id/btn4" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:alpha="0.5" android:background="#AEACB7DD" android:text="4" /> <Button android:id="@+id/btn5" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:background="#AEACB7DD" android:alpha="0.5" android:text="5" /> <Button android:id="@+id/btn6" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:background="#AEACB7DD" android:alpha="0.5" android:text="6" /> <Button android:id="@+id/btnMultiply" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:alpha="0.5" android:background="#AEACB7DD" android:text="*" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal"> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:alpha="0.5" android:background="#AEACB7DD" android:text="1" /> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:alpha="0.5" android:background="#AEACB7DD" android:text="2" /> <Button android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:alpha="0.5" android:background="#AEACB7DD" android:text="3" /> <Button android:id="@+id/btnDivide" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:alpha="0.5" android:background="#AEACB7DD" android:text="/" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal"> <Button android:id="@+id/btnSign" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:alpha="0.5" android:background="#AEACB7DD" android:text="+/-" /> <Button android:id="@+id/btn0" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:alpha="0.5" android:background="#AEACB7DD" android:text="0" /> <Button android:id="@+id/btnDecimal" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:alpha="0.5" android:background="#AEACB7DD" android:text="." /> <Button android:id="@+id/btnEquals" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:alpha="0.5" android:background="#AEACB7DD" android:text="=" /> </LinearLayout> </LinearLayout> 以下是主函数代码: package com.example.qa;import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { // 声明界面组件 private TextView firstNumView, operatorView, secondNumView, resultView; // 计算器状态变量 private String currentInput = ""; private double firstOperand = 0; private String currentOperator = ""; private boolean resetOnNextInput = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 确保布局文件名匹配 // 初始化TextView组件 firstNumView = findViewById(R.id.firstNumView); operatorView = findViewById(R.id.operatorView); secondNumView = findViewById(R.id.secondNumView); resultView = findViewById(R.id.resultView); // 设置按钮监听器 setupButtonListeners(); } private void setupButtonListeners() { // 数字按钮监听器 setNumberButtonListeners(); // 操作符按钮监听器 setOperatorButtonListeners(); // 功能按钮监听器 setFunctionButtonListeners(); } private void setNumberButtonListeners() { int[] numberButtonIds = { R.id.btn0, R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7, R.id.btn8, R.id.btn9, R.id.btnDecimal }; for (final int id : numberButtonIds) { findViewById(id).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Button button = (Button) v; String input = button.getText().toString(); if (resetOnNextInput) { clearAll(); resetOnNextInput = false; } // 处理小数点输入 if (input.equals(".")) { if (currentInput.contains(".")) return; // 防止多个小数点 if (currentInput.isEmpty()) currentInput = "0"; } currentInput += input; updateDisplay(); } }); } } private void setOperatorButtonListeners() { int[] operatorButtonIds = { R.id.btnAdd, R.id.btnSubtract, R.id.btnMultiply, R.id.btnDivide }; for (final int id : operatorButtonIds) { findViewById(id).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Button button = (Button) v; String operator = button.getText().toString(); if (!currentInput.isEmpty()) { firstOperand = Double.parseDouble(currentInput); currentOperator = operator; currentInput = ""; resetOnNextInput = false; updateDisplay(); } else if (!currentOperator.isEmpty()) { // 允许更新操作符 currentOperator = operator; updateDisplay(); } } }); } } private void setFunctionButtonListeners() { // 等号按钮 findViewById(R.id.btnEquals).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { calculateResult(); } }); // 清除按钮 findViewById(R.id.btnClear).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { clearAll(); } }); // 正负号按钮 findViewById(R.id.btnSign).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { toggleSign(); } }); // 百分号按钮 findViewById(R.id.btnPercent).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { convertToPercent(); } }); // 平方根按钮 findViewById(R.id.btnBack).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { calculateSquareRoot(); } }); } private void updateDisplay() { firstNumView.setText(currentOperator.isEmpty() ? "" : String.valueOf(firstOperand)); operatorView.setText(currentOperator); secondNumView.setText(currentInput); resultView.setText(""); } private void calculateResult() { if (currentOperator.isEmpty() || currentInput.isEmpty()) return; double secondOperand = Double.parseDouble(currentInput); double result = 0; try { switch (currentOperator) { case "+": result = firstOperand + secondOperand; break; case "-": result = firstOperand - secondOperand; break; case "*": result = firstOperand * secondOperand; break; case "/": if (secondOperand == 0) { resultView.setText("除数不能为0"); resetOnNextInput = true; return; } result = firstOperand / secondOperand; break; } // 处理整数结果显示 String resultStr; if (result == (int) result) { resultStr = String.valueOf((int) result); } else { resultStr = String.format("%.2f", result); } resultView.setText(resultStr); firstOperand = result; currentInput = ""; resetOnNextInput = true; } catch (Exception e) { resultView.setText("错误"); resetOnNextInput = true; } } private void clearAll() { currentInput = ""; firstOperand = 0; currentOperator = ""; resetOnNextInput = false; firstNumView.setText(""); operatorView.setText(""); secondNumView.setText(""); resultView.setText(""); } private void toggleSign() { if (!currentInput.isEmpty()) { double value = Double.parseDouble(currentInput); currentInput = String.valueOf(value * -1); updateDisplay(); } else if (resultView.getText().length() > 0) { double value = Double.parseDouble(resultView.getText().toString()); resultView.setText(String.valueOf(value * -1)); } } private void convertToPercent() { if (!currentInput.isEmpty()) { double value = Double.parseDouble(currentInput); currentInput = String.valueOf(value / 100); updateDisplay(); } else if (resultView.getText().length() > 0) { double value = Double.parseDouble(resultView.getText().toString()); resultView.setText(String.valueOf(value / 100)); } } private void calculateSquareRoot() { if (!currentInput.isEmpty()) { double value = Double.parseDouble(currentInput); if (value < 0) { resultView.setText("无效输入"); return; } currentInput = String.valueOf(Math.sqrt(value)); updateDisplay(); } else if (resultView.getText().length() > 0) { double value = Double.parseDouble(resultView.getText().toString()); if (value < 0) { resultView.setText("无效输入"); return; } resultView.setText(String.valueOf(Math.sqrt(value))); } } } 运行后模拟机显示Unfortunately,qa has stopp。 帮我解决该问题。
最新发布
06-24
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值