java String 两种不同的赋值 比较
原文:http://blog.163.com/woshihezhonghua@126/blog/static/1271436362012101214031911/
首先明确一点,String是一个类。下面我们主要讨论两个问题
a) String类的对象的两种赋值方式
b) 为什么String类的对象可以直接赋值
a) 1 类似普通对象,通过new创建字符串对象。String str = new String("Hello"); 内存图如下图所示,系统会先创建一个匿名对象"Hello"存入堆内存(我们暂且叫它A),然后new关键字会在堆内存中又开辟一块新的空间,然后 把"Hello"存进去,并且把地址返回给栈内存中的str, 此时A对象成为了一个垃圾对象,因为它没有被任何栈中的变量指向,会被GC自动回收。

a)2 直接赋值。 如String str = "Hello"; 首先会去缓冲池中找有没有一个"Hello"对象,如果没有,则新建一个,并且入池,所以此种赋值有一个好处,下次如果还有String对象也用直接赋 值方式定义为“Hello”, 则不需要开辟新的堆空间,而仍然指向这个池中的"Hello"



{
public static void main(String args[]){
String str1 = new String("Hello").intern();
String str2 = "Hello";
System.out.println( str1==str2 );
}
}
{
public static void main(String args[]){
String str1 = new String("Hello").intern();
String str2 = new String("Hello");
System.out.println( str1==str2 );
}
}
打开了String.class,有这么一段介绍:
- /**
- * The <code>String</code> class represents character strings. All
- * string literals in Java programs, such as <code>"abc"</code>, are
- * implemented as instances of this class.
- * <p>
- * Strings are constant; their values cannot be changed after they
- * are created. String buffers support mutable strings.
- * Because String objects are immutable they can be shared. For example:
- * <p><blockquote><pre>
- * String str = "abc";
- * </pre></blockquote><p>
- * is equivalent to:
- * <p><blockquote><pre>
- * char data[] = {'a', 'b', 'c'};
- * String str = new String(data);
- * </pre></blockquote><p>
- * Here are some more examples of how strings can be used:
- * <p><blockquote><pre>
- * System.out.println("abc");
- * String cde = "cde";
- * System.out.println("abc" + cde);
- * String c = "abc".substring(2,3);
- * String d = cde.substring(1, 2);
- * </pre></blockquote>
- * <p>
- */
通
过上面的介绍,我们可以清楚,直接赋值的话,是通过编译器在起作用,当你对"abc"没有通过new创建时,他会自动默认给你调用构造函数new
String(char value[]). 不显式调用String的构造函数(通过new叫显式调用),其实JDK编译器会自动给你加上。
java String 两种不同的赋值 比较的更多相关文章
- Java中String两种不同创建方式的区别及intern的用法
一, Java有两种创建字符串的方式, String str1 = "abc"; String str2 = new String("abc"); 用双引号创建 ...
- java中两种类型变量
Java中有两种类型的变量,一种是对象类型,另一种是基础类型(primitive type). 对象类型普遍采用引用的方式,比如 List a = new ArrayList(); List b = ...
- JAVA 中两种判断输入的是否是数字的方法__正则化_
JAVA 中两种判断输入的是否是数字的方法 package t0806; import java.io.*; import java.util.regex.*; public class zhengz ...
- Java中两种实现多线程方式的对比分析
本文转载自:http://www.linuxidc.com/Linux/2013-12/93690.htm#0-tsina-1-14812-397232819ff9a47a7b7e80a40613cf ...
- Java中有两种实现多线程的方式以及两种方式之间的区别
看到一个面试题.问两种实现多线程的方法.没事去网上找了找答案. 网上流传很广的是一个网上售票系统讲解.转发过来.已经不知道原文到底是出自哪里了. Java中有两种实现多线程的方式.一是直接继承Thre ...
- JavaScript 与 Java 是两种完全不同的语言,无论在概念还是设计上。
JavaScript 与 Java 是两种完全不同的语言,无论在概念还是设计上. Java(由 Sun 发明)是更复杂的编程语言. ECMA-262 是 JavaScript 标准的官方名称. Jav ...
- String基础: String两种创建对象方式的比较
字符串常量 在一般的语言中常量一旦声明则不可改变,在java中的字符串常量是以匿名对象来表示的 javaz中字符串两种定义方法: String strA= new String("hello ...
- String 两种实例化方式的区别
package com.java1234.chap03.sec08; public class Demo3 { public static void main(String[] args) { //1 ...
- Java HashMap两种遍历方式
第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Ma ...
随机推荐
- 设置一个POJO的某个属性的默认值
//月利率private BigDecimal monthRate=new BigDecimal(0);
- 函数buf_LRU_block_remove_hashed_page
/******************************************************************//** Takes a block out of the LRU ...
- 手机3D游戏开发:自定义Joystick的相关设置和脚本源码
Joystick在手游开发中非常常见,也就是在手机屏幕上的虚拟操纵杆,但是Unity3D自带的Joystick贴图比较原始,所以经常有使用自定义贴图的需求. 下面就来演示一下如何实现自定义JoySti ...
- Java [leetcode 11] Container With Most Water
问题描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
- date 获取昨天日期
使用date -d 选项: date +"%Y%m%d" -d "+n days" 今天的后n天日期 date +" ...
- HDU 5288 OO’s Sequence
题意:给一个序列,函数f(l, r)表示在[l, r]区间内有多少数字不是其他数字的倍数,求所有区间的f(l, r)之和. 解法:第一次打多校……心里还有点小激动……然而一道签到题做了俩点……呜呜呜… ...
- 扩展类加载器-------改变JAVA的父优先类加载顺序
java的类加载机制默认情况下是采用委托模型:当加载某个类时JVM会首先尝试用当前类加载器的父类加载器加载该类,若父类加载器加载不到再由当前类加载器来加载,因此这种模型又叫做“父优先”模型. 但是在实 ...
- Parallel for loops in .NET C# z
The start index: this is inclusive, i.e. this will be the first index value in the loop The end inde ...
- [King.yue]Ext中Grid得到选择行数据的方法总结
(1)grid.getStore().getRange(0,store.getCount()); //得到grid所有的行 (2)grid.getSelectionModel().getSele ...
- [GRYZ2015]快排练习
用快排完成n(n<=100)个人年龄的从小到大的排序,要求出生年相同的按月排. 输入:第一行n个人 2到n+1行出生 年 月(1992 9) 输出:年龄的从小到大的排序. type ss=r ...