惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
月光博客
月光博客
T
Threat Research - Cisco Blogs
小众软件
小众软件
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tailwind CSS Blog
Cisco Talos Blog
Cisco Talos Blog
V
V2EX
博客园 - 【当耐特】
C
Cybersecurity and Infrastructure Security Agency CISA
Hugging Face - Blog
Hugging Face - Blog
The Cloudflare Blog
The Last Watchdog
The Last Watchdog
Simon Willison's Weblog
Simon Willison's Weblog
T
Threatpost
S
Secure Thoughts
O
OpenAI News
P
Proofpoint News Feed
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
Scott Helme
Scott Helme
T
Tenable Blog
A
Arctic Wolf
L
LINUX DO - 热门话题
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
Visual Studio Blog
Hacker News: Ask HN
Hacker News: Ask HN
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
博客园 - Franky
WordPress大学
WordPress大学
Know Your Adversary
Know Your Adversary
博客园_首页
雷峰网
雷峰网
IT之家
IT之家
PCI Perspectives
PCI Perspectives
L
LINUX DO - 最新话题
H
Heimdal Security Blog

博客园 - Bryan Wong

Pushlets的初始化陷阱 在Tomcat部署Solr 4.3 Spring Security如何防止会话固定攻击(session fixation attack) Jdk自带的定时任务TimerTask和ScheduledExecutorService及其在Spring中的集成 Lucene索引,查询及高亮显示 记录几个Json的lib 蛋疼的腾讯微博数据类型和API文档 语言检测工具language-detection 你所不知道的Quartz特性 Spring Data集成MongoDB访问 Jetty的jar包依赖关系图 CAPS & BHCA Java中的集合类图 下载SUSE Linux 10 sp1的经历好曲折 C#代码检查工具:stylecop 圈复杂度基础 Scrum——“鸡”和“猪”的寓言 使用java断言调测程序 无所不能的final关键字
不同于C#的Java值类型和String类型
Bryan Wong · 2010-02-18 · via 博客园 - Bryan Wong

与C#一样,java也对基本类型进行了封装,int封装出Integer类,boolean封装出Boolean类。这样一来,基本类型就突破了值类型的限制,获得了面向对象的好处。然而,与.NET不同,java中的封装类是真正的引用类型,比如在java中用“==”比较两个封装类对象,尽管值相等,得到的结果也很可能是false,而不是像.NET一样,将基本类型的一些操作符作了重载,使得“==”符号的行为看起来与基本类型完全一致。

在java中,“Integer i=10,j=10;”使得i==j为true,但如果i或者j任何一个使用“new Integer(10)”,那么i==j的值将是false,因为java遇到new关键字就会开辟新的堆空间,这样就导致两个封装类对象的引用地址不同。同样的差异还出现在String类型。对此在java中强烈推荐使用equals方法而不是“==”来比较封装类对象的值。

测试代码如下:

code

public class ValueType
{
    
public static void main(String[] args)
    {
        
int ii = 10, jj = 10;
        System.out.println(
"ii==jj:" + (ii == jj)); // true

        Integer i 
= new Integer(10), j = new Integer(10);
        System.out.println(
"i==j:" + (i == j)); // false
        System.out.println("i.equals(j):" + i.equals(j)); // true

        System.out.println(
"ii==j:" + (ii == j)); // true
        System.out.println("i.equals(jj):" + i.equals(jj)); //true

        String x 
= "xxx";
        String y 
= "xxx";
        String z 
= new String("xxx");

        System.out.println(

"x==y:" + (x == y)); //true
        System.out.println("x.equals(y):" + (x.equals(y))); // true
        System.out.println("x==z:" + (x == z)); // false
        System.out.println("x.equals(z):" + (x.equals(z))); // true

        System.out.println(
"x.subString(1,2):" + x.substring(1,2)); // x
    }
}