`
v64500
  • 浏览: 22615 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论

String b = new String("abc"); // DON'T DO THIS

阅读更多
  您可以用多种方式来创建字符串。每次创建字符串,Java虚拟机都会在背后创建一个String对象。下面有两种创建字符串对象的方式,并且将字符串对象赋值给一个变量:

String a = "abc";
String b = new String("abc"); // DON'T DO THIS

  避免使用第二种方式[2]。第二种方式创建了两个String对象,这样降低了性能:首先,Java虚拟机创建了String对象“abc”。然后,java虚拟机创建一个新的String对象,并把字符串“abc”传入构造函数。同样重要的是,这是一次不必要的构造,使得您的代码阅读起来更加困难。



  You can construct Strings in a number of ways. Any time you create a new String literal, the Java VM constructs a String object behind the scenes. Here are two ways to construct a String object and assign it to a reference variable:

String a = "abc";
String b = new String("abc"); // DON'T DO THIS

  Avoid the second technique.[2] It creates two String objects, which can degrade performance: First, the VM creates the literal String object "abc". Second, the VM constructs a new String object, passing the literal "abc" to its constructor. Equally as important, it is an unnecessary construct that makes your code more difficult to read.

分享到:
评论

相关推荐

    String a="hello" String b="hello" a==b 返回true的问题分析

    String b=new String("hello world"); //这种用new关键字定义的字符串,是在堆中分配空间的。而分配空间就是由new去完成的,由new去决定分配多大空间,并对空间初始化为字符串"hello world" 返回其在堆上的地址。 ...

    String.txt

    String b =new String("abc"); //创建字符串方式2 System.out.println(a==b); //false,内存地址不相等 String c =new String("abc"); String d =new String("abc"); System.out.println(c==d...

    超强JS(javascript)正则类

    */ //=================================// /* GetTagName(str,new Array(),0)*/ //=================================// /* getElememtById(string,id); */ //=================================//...

    关于String s=new String( "Hello")的例子思考

    关于String s=new String( "Hello")的例子思考

    java面试题122道基础题

    String s=new String("abc"); System.out.println(s); String s1="abc"; String s2=new String("abc"); System.out.println(s==s1);//false System.out.println(s==s2);//false System.out.println(s1==s2);...

    网页无组件上传图片与大家分享

    private byte[] b = new byte[4096]; //字节流存放数组 private boolean successful = true; private Hashtable fields = new Hashtable(); public UploadBean() { } //设置上传文件的后缀名 public void ...

    浅析为什么a=”abc” 不等于 a=new String(“abc”)

    b=new String("abc") typeof b // object a==b //true a===b //false 但是为什么呢?看了很多书,问了好几个大神,其实自己还是稀里糊涂的。这里记录一下,备查。 在js中,区分原始资料类型和包装类型。数字、字符串...

    java uploadify完整图片批量上传带返回路径

    String newName="";//新文件名 File f=filedata[i];//其中一个上传的文件对象 String fullname=filedataFileName[i];//源文件全名 //String type=filedataContentType[i];//源文件类型 ...

    支付宝即时到账系统java程序开发包

    Date Now_Date=new Date(); String paygateway = "https://www.alipay.com/cooperate/gateway.do?"; //'支付接口 String service = "create_direct_pay_by_user";//快速付款交易服务 String sign_type = "MD5"; ...

    c# object, dynamic, var的区别用例

    string info2 = dyn.param + new Program() + 1; // 正确。但.param不能智能感知。 // 编译通过是因为与dynamic相关的所有类型操作,编译器全部放行。 // 很显然,运行时必然通过“自动反射”进行检查。 string ...

    蓝牙打印声色版(联系我)

    String inputFile = "d:/img/b.tif"; String outputFile = "d:/img/b.bmp"; RenderedOp src = JAI.create("fileload", inputFile); OutputStream os = new FileOutputStream(outputFile); BMPEncodeParam ...

    asp.net连接数据库

    string str=texuser.Text.ToString(); DataSet Dataset1 = new DataSet(); SqlConnection objConnect;//打开数据库连接 SqlDataAdapter objCommand= new SqlDataAdapter();//数据库命令 string strConnect;//...

    delphi通用函数单元一

    返回'abC#Eg#lkh'} function StrTran(psInput:String; psSearch:String; psTranWith:String):String; {测试通过} {* 返回替换后字符串[替换字符串] Examples: StrTran('aruyfbn','ruy','=====');; 返回'a=====fbn...

    JAVA数据库连接方法

    String URL = "jdbc:mysql://localhost:3306/db_name"; // 连接的URL, db_name为数据库名 String Username = "username"; // 用户名 String Password = "password"; // 密码 Class.forName(Driver).newInstance...

    XPathUtil,根据Xpath解析xml文件

    XPathUtil util = XPathUtil.newInstance(xmlPath); Product product = new Product(); String title = util.getSingleValue("book/title"); product.setTitle(title); String classification = util....

    简易实用的JavaMail邮箱程序

    MailboxTypes = new HashMap<String,String>(); MailboxTypes.put("163","smtp.163.com"); MailboxTypes.put("139","smtp.139.com"); MailboxTypes.put("126","smtp.126.com"); MailboxTypes.put("qq", ...

    有关Java String常用方法的总结.docx

    Java中的String类是编程中最常用的类之一,它用于表示不可变的字符序列。在这个文档中,我们主要讨论了关于Java String类的一些常用方法以及与String相关的常量池问题。 首先,我们要理解Java中的final关键字。当...

    oss上传-java

    展示部分代码: ... String dateDir = sdf.format(new Date()); String name= dateDir+UUID.randomUUID().toString(); String destfileName=""; String baseDir=""; String absolutePath=""; //存 try {

    Javabean封装数据库

    String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs"; //数据库用户名 String user = "sa"; //数据库密码 String password = "123456"; //根据数据库参数取得一个数据库连接 conn =...

    Android增量更新

    private String newapk = "mnt/sdcard/app2.apk";//新版本路径 private String pathapk = "mnt/sdcard/path.apk";//补丁(差分包路径) private String complexapk = "mnt/sdcard/newapk.apk";//合成的apk路径 ...

Global site tag (gtag.js) - Google Analytics