pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

避免Java中的重復字符串

Avoid duplicate Strings in Java(避免Java中的重復字符串)
本文介紹了避免Java中的重復字符串的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想問一個關于在 Java 中避免字符串重復的問題.

I want to ask a question about avoiding String duplicates in Java.

context 是:一個帶有如下標簽和屬性的 XML:

The context is: an XML with tags and attributes like this one:

<product id="PROD" name="My Product"...></product>

使用 JibX,此 XML 在這樣的類中編組/解組:

With JibX, this XML is marshalled/unmarshalled in a class like this:

public class Product{
private String id;
private String name;
// constructor, getters, setters, methods  and so on
}

程序是一個長時間的批處理,所以Product對象被創建、使用、復制等.

The program is a long-time batch processing, so Product objects are created, used, copied, etc.

嗯,問題是:當我使用 Eclipse 內存分析器 (MAT) 之類的軟件分析執行時,我發現了幾個重復的字符串.例如,在 id 屬性中,PROD 值在 2000 個實例左右重復,等等.

Well, the question is: When I analysed the execution with software like Eclipse memory analyzer (MAT), I found several duplicated Strings. For example, in the id attribute, the PROD value is duplicated around 2000 instances, etc.

如何避免這種情況?Product 類中的其他屬性可能會在執行過程中改變它們的值,但像 id、name 等屬性不會如此頻繁地改變.

How can I avoid this situation? Other attributes in Product class may change their value along the execution, but attrs like id, name... don't change so frequently.

我已經閱讀了一些關于 String.intern() 方法的內容,但我還沒有使用過,我不確定它是否可以解決這個問題.我可以在類中的 static final 常量等屬性中定義最常見的值嗎?

I have readed something about String.intern() method, but I haven't used yet and I'm not sure it's a solution for this. Could I define the most frequent values in those attributes like static final constants in the class?

我希望我能以正確的方式表達我的問題.非常感謝任何幫助或建議.提前致謝.

I hope I'd have expressed my question in a right way. Any help or advice is very appreciated. Thanks in advance.

推薦答案

interning 將是正確的解決方案,如果你真的有問題.Java 將字符串字面量和許多其他字符串存儲在一個內部池中,每當 將要創建一個新字符串時,JVM 首先檢查該字符串是否已經在池中.如果是,它不會創建新實例,而是將引用傳遞給 interned String 對象.

interning would be the right solution, if you really have a problem. Java stores String literals and a lot of other Strings in an internal pool and whenever a new String is about to be created, the JVM first checks, if the String is already in the pool. If yes, it will not create a new instance but pass the reference to the interned String object.

有兩種方法可以控制這種行為:

There are two ways to control this behaviour:

String interned = String.intern(aString); // returns a reference to an interned String
String notInterned = new String(aString); // creates a new String instance (guaranteed)

所以也許,這些庫確實為所有 xml 屬性值創建了新實例.這是可能的,您將無法更改它.

So maybe, the libraries really create new instances for all xml attribute values. This is possible and you won't be able to change it.

實習生具有全球影響力.一個實習字符串可以立即用于任何對象"(這個視圖實際上沒有意義,但它可能有助于理解它).

intern has a global effect. An interned String is immediatly available "for any object" (this view doesn't really make sense, but it may help to understand it).

所以,假設我們在類 Foo 中有一行,方法 foolish:

So, lets say we have a line in class Foo, method foolish:

String s = "ABCD";

字符串文字立即被實習.JVM 檢查ABCD"是否已經在池中,如果沒有,則ABCD"存儲在池中.JVM 將對實習字符串的引用分配給 s.

String literals are interned immediatly. JVM checks, if "ABCD" is already in the pool, if not, "ABCD" is stored in the pool. The JVM assigns a reference to the interned String to s.

現在,也許在另一個類 Bar 中,在方法 barbar 中:

Now, maybe in another class Bar, in method barbar:

String t = "AB"+"CD";

然后JVM會像上面一樣實習AB"和CD",創建連接的String,看,如果它已經被intered,嘿,是的,并將對interned StringABCD"的引用分配給<代碼>t.

Then the JVM will intern "AB" and "CD" like above, create the concatenated String, look, if it is intered already, Hey, yes it is, and assign the reference to the interned String "ABCD" to t.

調用 "PROD".intern() 可能會起作用,也可能會失敗.是的,它實習字符串PROD".但是有一個機會,jibx 確實為屬性值創建了新的字符串

Calling "PROD".intern() may work or fail. Yes, it will intern the String "PROD". But there's a chance, that jibx really creates new Strings for attribute values with

String value = new String(getAttributeValue(attribute));

在這種情況下,value 不會引用一個實習字符串(即使 "PROD" 在池中),而是引用一個新的 String 實例在堆上.

In that case, value will not have a reference to an interned String (even if "PROD" is in the pool) but a reference to a new String instance on the heap.

而且,對于您命令中的另一個問題:這僅在運行時發生.編譯只是創建類文件,字符串池是對象堆上的數據結構,由 JVM 使用,執行應用程序.

And, to the other question in your command: this happens at runtime only. Compiling simply creates class files, the String pool is a datastructure on the object heap and that is used by the JVM, that executes the application.

這篇關于避免Java中的重復字符串的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 土壤检测仪器_行星式球磨仪_土壤团粒分析仪厂家_山东莱恩德智能科技有限公司 | 警用|治安|保安|不锈钢岗亭-售货亭价格-垃圾分类亭-移动厕所厂家-苏州灿宇建材 | 中视电广_短视频拍摄_短视频推广_短视频代运营_宣传片拍摄_影视广告制作_中视电广 | 石英陶瓷,石英坩埚,二氧化硅陶瓷-淄博百特高新材料有限公司 | 深圳活动策划公司|庆典策划|专业公关活动策划|深圳艺典文化传媒 重庆中专|职高|技校招生-重庆中专招生网 | 武汉刮刮奖_刮刮卡印刷厂_为企业提供门票印刷_武汉合格证印刷_现金劵代金券印刷制作 - 武汉泽雅印刷有限公司 | 数字展示在线_数字展示行业门户网站| 等离子空气净化器_医用空气消毒机_空气净化消毒机_中央家用新风系统厂家_利安达官网 | 依维柯自动挡房车,自行式国产改装房车,小型房车价格,中国十大房车品牌_南京拓锐斯特房车 - 南京拓锐斯特房车 | 早报网| pos机办理,智能/扫码/二维码/微信支付宝pos机-北京万汇通宝商贸有限公司 | 煤矿支护网片_矿用勾花菱形网_缝管式_管缝式锚杆-邯郸市永年区志涛工矿配件有限公司 | 大立教育官网-一级建造师培训-二级建造师培训-造价工程师-安全工程师-监理工程师考试培训 | 成都办公室装修-办公室设计-写字楼装修设计-厂房装修-四川和信建筑装饰工程有限公司 | 纯水设备_苏州皙全超纯水设备水处理设备生产厂家 | 智能终端_RTU_dcm_北斗星空自动化科技 | 除甲醛公司-甲醛检测治理-杭州创绿家环保科技有限公司-室内空气净化十大品牌 | 实木家具_实木家具定制_全屋定制_美式家具_圣蒂斯堡官网 | 地源热泵一体机,地源热泵厂家-淄博汇能环保设备有限公司 | 健康管理师报名入口,2025年健康管理师考试时间信息网-网站首页 塑料造粒机「厂家直销」-莱州鑫瑞迪机械有限公司 | 造价工程师网,考试时间查询,报名入口信息-网站首页 | 杜康白酒加盟_杜康酒代理_杜康酒招商加盟官网_杜康酒厂加盟总代理—杜康酒神全国运营中心 | 市政路灯_厂家-淄博信达电力科技有限公司 | 算命免费_生辰八字_免费在线算命 - 卜算子算命网 | 碎石机设备-欧版反击破-欧版颚式破碎机(站)厂家_山东奥凯诺机械 高低温试验箱-模拟高低温试验箱订制-北京普桑达仪器科技有限公司【官网】 | 厂房出售_厂房仓库出租_写字楼招租_土地出售-中苣招商网-中苣招商网 | 阀门智能定位器_电液动执行器_气动执行机构-赫尔法流体技术(北京)有限公司 | 安全,主动,被动,柔性,山体滑坡,sns,钢丝绳,边坡,防护网,护栏网,围栏,栏杆,栅栏,厂家 - 护栏网防护网生产厂家 | 济南品牌包装设计公司_济南VI标志设计公司_山东锐尚文化传播 | 电气控制系统集成商-PLC控制柜变频控制柜-非标自动化定制-电气控制柜成套-NIDEC CT变频器-威肯自动化控制 | 扒渣机,铁水扒渣机,钢水扒渣机,铁水捞渣机,钢水捞渣机-烟台盛利达工程技术有限公司 | 贵州科比特-防雷公司厂家提供贵州防雷工程,防雷检测,防雷接地,防雷设备价格,防雷产品报价服务-贵州防雷检测公司 | 哈希PC1R1A,哈希CA9300,哈希SC4500-上海鑫嵩实业有限公司 | 丁基胶边来料加工,医用活塞边角料加工,异戊二烯橡胶边来料加工-河北盛唐橡胶制品有限公司 | 馋嘴餐饮网_餐饮加盟店火爆好项目_餐饮连锁品牌加盟指南创业平台 | 淄博不锈钢,淄博不锈钢管,淄博不锈钢板-山东振远合金科技有限公司 | 橡胶弹簧|复合弹簧|橡胶球|振动筛配件-新乡市永鑫橡胶厂 | 氢氧化钙设备_厂家-淄博工贸有限公司 | 全自动在线分板机_铣刀式在线分板机_曲线分板机_PCB分板机-东莞市亿协自动化设备有限公司 | IIS7站长之家-站长工具-爱网站请使用IIS7站长综合查询工具,中国站长【WWW.IIS7.COM】 | 挖掘机挖斗和铲斗生产厂家选择徐州崛起机械制造有限公司 |