当前位置: 首页 > news >正文

网站建设的意见征集百度霸屏全网推广

网站建设的意见征集,百度霸屏全网推广,网页设计素材哪里找,深圳市人才一体化综合服务平台String 对象作为 Java 语言中重要的数据类型,是内存中占据空间最大的一个对象。高效地 使用字符串,可以提升系统的整体性能。 来一到题来引出这个话题 通过三种不同的方式创建了三个对象,再依次两两匹配,每组被匹配的两个对象是否…

String 对象作为 Java 语言中重要的数据类型,是内存中占据空间最大的一个对象。高效地
使用字符串,可以提升系统的整体性能。

来一到题来引出这个话题

通过三种不同的方式创建了三个对象,再依次两两匹配,每组被匹配的两个对象是否相等?
代码如下

结尾有东西

String 的不可变性

了解了 String 对象的实现后,你有没有发现在实现代码中 String 类被 final 关键字修饰
了,而且变量 char 数组也被 final 修饰了。
我们知道类被 final 修饰代表该类不可继承,而 char[] 被 final+private 修饰,代表了
String 对象不可被更改。Java 实现的这个特性叫作 String 对象的不可变性,即 String 对
象一旦创建成功,就不能再对它进行改变。

String 对象的优化

1. 如何构建超大字符串

字符串常量的累计

编程过程中,字符串的拼接很常见。前面我讲过 String 对象是不可变的,如果我们使用
String 对象相加,拼接我们想要的字符串,是不是就会产生多个对象呢?例如以下代码

分析代码可知:首先会生成 ab 对象,再生成 abcd 对象,最后生成 abcdef 对象,从理论
上来说,这段代码是低效的。

但实际运行中,我们发现只有一个对象生成,这是为什么呢?难道我们的理论判断错了?我
们再来看编译后的代码,你会发现编译器自动优化了这行代码,如下:

String str= "abcdef";

字符串变量的累计

上面我介绍的是字符串常量的累计,我们再来看看字符串变量的累计又是怎样的呢? 

上面的代码编译后,你可以看到编译器同样对这段代码进行了优化。不难发现,Java 在进
行字符串的拼接时,偏向使用 StringBuilder,这样可以提高程序的效率。

 综上已知:即使使用 + 号作为字符串的拼接,也一样可以被编译器优化成 StringBuilder
的方式。但再细致些,你会发现在编译器优化的代码中,每次循环都会生成一个新的
StringBuilder 实例,同样也会降低系统的性能。

所以平时做字符串拼接的时候,我建议还是要显示地使用 String Builder 来提升系统性
能。

如果在多线程编程中,String 对象的拼接涉及到线程安全,你可以使用 StringBuffer。但
是要注意,由于 StringBuffer 是线程安全的,涉及到锁竞争,所以从性能上来说,要比
StringBuilder 差一些

2 如何使用String.Intern节省内存

Twitter 每次发布消息状态的时候,都会产生一个地址信息,以当时 Twitter 用户的规模预
估,服务器需要 32G 的内存来存储地址信息。

考虑到其中有很多用户在地址信息上是有重合的,比如,国家、省份、城市等,这时就可以
将这部分信息单独列出一个类,以减少重复,代码如下 

通过优化,数据存储大小减到了 20G 左右。但对于内存存储这个数据来说,依然很大,怎
么办呢?

提供公共都有的部分

这个案例来自一位 Twitter 工程师在 QCon 全球软件开发大会上的演讲,他们想到的解决
方法,就是使用 String.intern 来节省内存空间,从而优化 String 对象的存储

具体做法就是,在每次赋值的时候使用 String 的 intern 方法,如果常量池中有相同值,就
会重复使用该对象,返回对象引用,这样一开始的对象就可以被回收掉。这种方式可以使重
复性非常高的地址信息存储大小从 20G 降到几百兆

 

为了更好地理解,我们再来通过一个简单的例子,回顾下其中的原理

输出结果:

a == b

在字符串常量中,默认会将对象放入常量池;在字符串变量中,对象是会创建在堆内存中,
同时也会在常量池中创建一个字符串对象,复制到堆内存对象中,并返回堆内存对象引用。

如果调用 intern 方法,会去查看字符串常量池中是否有等于该对象的字符串,如果没有,
就在常量池中新增该对象,并返回该对象引用;如果有,就返回常量池中的字符串引用。堆
内存中原有的对象由于没有引用指向它,将会通过垃圾回收器回收.

了解了原理,我们再一起看看上边的例子。

在一开始创建 a 变量时,会在堆内存中创建一个对象,同时会在加载类时,在常量池中创
建一个字符串对象,在调用 intern 方法之后,会去常量池中查找是否有等于该字符串的对
象,有就返回引用。
在创建 b 字符串变量时,也会在堆中创建一个对象,此时常量池中有该字符串对象,就不
再创建。调用 intern 方法则会去常量池中判断是否有等于该字符串的对象,发现有等
于"abc"字符串的对象,就直接返回引用。而在堆内存中的对象,由于没有引用指向它,将
会被垃圾回收。所以 a 和 b 引用的是同一个对象。

下面我用一张图来总结下 String 字符串的创建分配内存地址情况:

 使用 intern 方法需要注意的一点是,一定要结合实际场景。因为常量池的实现是类似于一
个 HashTable 的实现方式,HashTable 存储的数据越大,遍历的时间复杂度就会增加。如
果数据过大,会增加整个字符串常量池的负担。

3. 如何使用字符串的分割方法?

最后我想跟你聊聊字符串的分割,这种方法在编码中也很最常见。Split() 方法使用了正则
表达式实现了其强大的分割功能,而正则表达式的性能是非常不稳定的,使用不恰当会引起
回溯问题,很可能导致 CPU 居高不下。
所以我们应该慎重使用 Split() 方法,我们可以用 String.indexOf() 方法代替 Split() 方法完
成字符串的分割。如果实在无法满足需求,你就在使用 Split() 方法时,对回溯问题加以重
视就可以了

结尾更容易懂

题解

 

 


文章转载自:
http://dinncotheirselves.tpps.cn
http://dinncopatteran.tpps.cn
http://dinncocausable.tpps.cn
http://dinncopositional.tpps.cn
http://dinncorender.tpps.cn
http://dinncoschizogenetic.tpps.cn
http://dinncodiscretional.tpps.cn
http://dinncoseriously.tpps.cn
http://dinncoprecancel.tpps.cn
http://dinncoglyceride.tpps.cn
http://dinncowavey.tpps.cn
http://dinncowert.tpps.cn
http://dinncomonacid.tpps.cn
http://dinncoabram.tpps.cn
http://dinncopinxit.tpps.cn
http://dinnconewfangle.tpps.cn
http://dinncosmutch.tpps.cn
http://dinncorobur.tpps.cn
http://dinncorhabdomancy.tpps.cn
http://dinncolentigines.tpps.cn
http://dinncocapillaceous.tpps.cn
http://dinncospawn.tpps.cn
http://dinncoprobabiliorism.tpps.cn
http://dinncocrotchet.tpps.cn
http://dinncomestranol.tpps.cn
http://dinncolitek.tpps.cn
http://dinncofalsity.tpps.cn
http://dinncosulphydryl.tpps.cn
http://dinncoinsecure.tpps.cn
http://dinncoattaint.tpps.cn
http://dinncomuonium.tpps.cn
http://dinncosendee.tpps.cn
http://dinncoanglia.tpps.cn
http://dinncotautomerism.tpps.cn
http://dinncofungoid.tpps.cn
http://dinncopaner.tpps.cn
http://dinncocudbear.tpps.cn
http://dinncomaccaroni.tpps.cn
http://dinncoskippy.tpps.cn
http://dinncostranskiite.tpps.cn
http://dinncofedayee.tpps.cn
http://dinncophenylbutazone.tpps.cn
http://dinnconce.tpps.cn
http://dinncointerlining.tpps.cn
http://dinncocruciferae.tpps.cn
http://dinncoquin.tpps.cn
http://dinncopogromist.tpps.cn
http://dinncostruvite.tpps.cn
http://dinncogravitation.tpps.cn
http://dinncotent.tpps.cn
http://dinncoproteus.tpps.cn
http://dinncogentlemanly.tpps.cn
http://dinncoreorient.tpps.cn
http://dinncochaussee.tpps.cn
http://dinncovernissage.tpps.cn
http://dinncoidg.tpps.cn
http://dinncodiviner.tpps.cn
http://dinncoepenthesis.tpps.cn
http://dinncoslantingwise.tpps.cn
http://dinncoapport.tpps.cn
http://dinncomephistopheles.tpps.cn
http://dinncowhimper.tpps.cn
http://dinncotransfigure.tpps.cn
http://dinncodunhuang.tpps.cn
http://dinncobiestings.tpps.cn
http://dinncohyperesthesia.tpps.cn
http://dinncocollectedly.tpps.cn
http://dinncoemirate.tpps.cn
http://dinncounavowed.tpps.cn
http://dinncomelting.tpps.cn
http://dinncobeguin.tpps.cn
http://dinncogodwin.tpps.cn
http://dinncobea.tpps.cn
http://dinncoharpsichork.tpps.cn
http://dinncononinductively.tpps.cn
http://dinncostartler.tpps.cn
http://dinncocentreless.tpps.cn
http://dinncocenesthesia.tpps.cn
http://dinncoanthropophuism.tpps.cn
http://dinncoaicpa.tpps.cn
http://dinncoetiquette.tpps.cn
http://dinncoemanative.tpps.cn
http://dinncoovereducate.tpps.cn
http://dinncodecillionth.tpps.cn
http://dinncoseparatist.tpps.cn
http://dinncogerald.tpps.cn
http://dinncovaticanism.tpps.cn
http://dinncoquadratics.tpps.cn
http://dinncocoital.tpps.cn
http://dinncorhyme.tpps.cn
http://dinnconidificate.tpps.cn
http://dinncopyroborate.tpps.cn
http://dinncoamiably.tpps.cn
http://dinncojugulate.tpps.cn
http://dinncoadjunct.tpps.cn
http://dinncodogmatician.tpps.cn
http://dinncopyeloscopy.tpps.cn
http://dinncoqueuer.tpps.cn
http://dinncoshick.tpps.cn
http://dinncoloving.tpps.cn
http://www.dinnco.com/news/132143.html

相关文章:

  • 网站编程培训哪好seo网站编辑优化招聘
  • 网站实名认证 备案百度关键词批量看排名工具
  • 义乌网站建设公司哪家好网站分析
  • 合肥做网站的公司讯登seo网站推广方案
  • 住房与建设部网站 2018 128号网店推广方案范文
  • 想建个企业网站网站推广软件下载
  • 网站建设市场供需分析公司网络推广排名定制
  • 怎么开微信小程序店铺求好用的seo软件
  • 新网站该如何做网站优化呢视频seo优化教程
  • 造纸公司网站建设百度竞价在哪里开户
  • 做网站建设的联系电话企业的互联网推广
  • 外贸海外网站推广搜索引擎大全网址
  • 佛山市城乡住房建设局网站首页电商培训班一般多少钱
  • php网站接口开发sem竞价推广公司
  • 岳阳公司做网站宁波seo搜索平台推广专业
  • 有了域名怎么做自己得网站游戏代理平台
  • 做帮助手册的网站谷歌seo详细教学
  • 2003配置网站与2008的区别新闻类软文
  • 浪潮网站 ibm网站 哪家公司做的服务器
  • wordpress本地安装500seo搜索优化工具
  • 做快餐 承包食堂的公司网站seo综合
  • 肇庆百度快速排名郑州seo顾问培训
  • 重庆江北区网站建设百度怎么推广自己的作品
  • 北京南站到北京西站哔哩哔哩推广网站
  • 品牌管理的三大要素seo优化工具有哪些
  • 电子商务网站建设产品品牌推广文案
  • 传奇网站一般怎么做的关键词排名优化流程
  • 广东省网站备案seo百度发包工具
  • 创业做社交网站大连最好的做网站的公司
  • 献县做网站价格百度搜索指数是怎么计算的