equal 和 ==
刚才看了一下别人的博客,想加深一下对 equal 和 == 的了解。
总结了几点:
1.equal 每个类都有必要覆盖一下,对于String 类,已经覆盖,比较的是String对象的字符序列是否相等。
2.== 比较的是内存中两个对象是否为同一个,即地址是否相等;而对于基本数据类型,比较的是字面数值是否一样。
package com.java.test;
import org.junit.Test;
public class MyEqual {
@Test
public void test() {
int a1 = 10;
int a2 = 10;
Integer b1 = 10;
Integer b2 = 10;
String s1 = "abcd";
String s2 = "abcd";
String s3 = new String("abcd");
System.out.println(a1 == a2);//true
System.out.println(b1 == b2);//true
System.out.println(b1.equals(b2));//true
System.out.println(b1 == a1);//true
System.out.println(b1.equals(a1));//true
System.out.println(s1 == s2);//true
System.out.println(s1 == s3);//false
System.out.println(s1.equals(s3));//true
}
}
equal 和 ==的更多相关文章
- [LeetCode] Minimum Moves to Equal Array Elements II 最少移动次数使数组元素相等之二
Given a non-empty integer array, find the minimum number of moves required to make all array element ...
- [LeetCode] Minimum Moves to Equal Array Elements 最少移动次数使数组元素相等
Given a non-empty integer array of size n, find the minimum number of moves required to make all arr ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- Equal Sides Of An Array
参考:http://stackoverflow.com/questions/34584416/nested-loops-with-arrays You are going to be given an ...
- Int,Long比较重使用equal替换==
首先,==有很多限制,如Integer 类型的值在[-128,127] 期间,Integer 用 “==”是可以的(参考),超过范围则不行,那么使用equal则代替则完全ok public stati ...
- 无法解决 equal to 操作中 "SQL_Latin1_General_CP1_CI_AS" 和 "Chinese_PRC_CI_AS"
无法解决 equal to 操作中 "SQL_Latin1_General_CP1_CI_AS" 和 "Chinese_PRC_CI_AS" 之间 2011-0 ...
- LeetCode Minimum Moves to Equal Array Elements II
原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/ 题目: Given a non-empt ...
- LeetCode Minimum Moves to Equal Array Elements
原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements/ 题目: Given a non-empty i ...
- conflict between "Chinese_PRC_CI_AI" and "Chinese_PRC_CI_AS" in the equal to operation
在SQL SERVICE做关联查询的时候遇到了"conflict between "Chinese_PRC_CI_AI" and "Chinese_PRC_CI ...
- why happen "WaitHandles must be less than or equal to 64"
一.背景: 在一个项目中碰到大数据插入的问题,一次性插入20万条数据(SQL Server),并用200个线程去执行,计算需要花费多少时间,因此需要等200个线程处理完成后,记录花费的时间,需要考虑的 ...
随机推荐
- zoj 3591 Nim 博弈论
思路:先生成序列再求异或,最多的可能为n*(n+1)/2: 在去掉其中必败的序列,也就是a[i]=a[j]之间的序列. 代码如下: #include<iostream> #include& ...
- 浅谈Asp.net的sessionState
见:http://my.oschina.net/kavensu/blog/330436
- linux环境几个特殊的shell变量
特殊的shell变量: $0 获取当前执行的shell脚本的文件名 $n 获取当前执行的shell脚本的第n个参数值,n=1..9 $* 获取当前shell的所有参数 “$1 $2 $3 …注意 ...
- ruby 学习 -- Array --2
定义: [1, 2, 3] # An array that holds three Fixnum objects [-10...0, 0..10,] # An array of two ranges; ...
- VCL设计方法概论(自己总结了9条),以及10个值得研究的控件 good
VCL设计方法概论 1. 把Delphi对象改造成一个Windows窗口,主要是要设置Handle和回调函数.在创建一个Windows窗口后,将其句柄赋值给Delphi对象的属性,这个并不难,相当于从 ...
- 299. Bulls and Cows
题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...
- MyBatis学习总结_01_MyBatis快速入门
一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...
- Android 实现Path2.0中绚丽的的旋转菜单
上图先: 那么下面开始吧~ 首先,将整个菜单动画分解开来. 1. 一级菜单按钮的旋转动画2个,十字和叉叉状态的转换. 2. 二级菜单按钮的平移动画2个,弹簧效果的in和out ...
- MySQL 数据库性能优化之索引优化
接着上一篇 MySQL 数据库性能优化之表结构,这是 MySQL数据库性能优化专题 系列的第三篇文章:MySQL 数据库性能优化之索引优化 大家都知道索引对于数据访问的性能有非常关键的作用,都知道索引 ...
- 《c程序设计语言》读书笔记--大于8 的字符串输出
#include <stdio.h> #define MAXLINE 100 #define MAX 8 int getline(char line[],int maxline); voi ...