首页 技术 正文
技术 2022年11月10日
0 收藏 881 点赞 4,291 浏览 616 个字

  java 程序中测试两个变量是否相等有两种方法:

  • ==
  • equals()方法

当使用==判断两个变量是否相等时,如果两个变量是基本类型变量,且都是数值类型(不一定要求数据类型严格相同),则只要两个变量的值相等时,才会返回true

public class m{
10 public static void main(String[] args){
11 String str1 = "abc";
12 String str2 = "abc";
13 System.out.println(str1 ==str2);
14 String str3 = new String("hello");
15 String str4 = new String("hello");
16 System.out.println(str3 == str4);
17 System.out.println(str3.equals(str4));
18 }
19 }

运行结果

String 有一个非常容易迷惑的地方:”hello”直接量和new String(“hello”)有什么区别?

当我们直接将形如”hello”的字符串直接量时,JVM将会使用常量池来管理这些字符串;

当使用new String(“hello”)时,JVM会先使用常量池来管理”hello”直接量,在调用String 类的构造器来创建一个新的String对象,新创建的String对象被保存在堆内存当中。

常量池专门用于管理在编译时被确认并保存在已经编译的.class文件中的一些数据。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,084
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,559
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,408
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,181
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,818
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,901