Interview stuff about String class in java

All of us must have gone though some common questions related to String class in java. These questions range from immutability to memory leak issues. I will try to cover such questions in this post.

Questions discussed below:

  • Why strings are immutable?
  • String pool concept
  • Keyword ‘intern’ usage
  • Matching Regular expressions?
  • Strings comparison?
  • Memory leak issue

String in java are like any other programming language, a sequence of characters. This is more like a utility class to work on that char sequence. This char sequence is maintained in following variable:

?
1
2
/** The value is used for character storage. */
private final char value[];

To access this array in different scenarios, following variables are used:

?
1
2
3
4
5
/** The offset is the first index of the storage that is used. */
private final int offset;
 
/** The count is the number of characters in the String. */
private final int count;

Why strings are immutable?

We all know that strings in java are immutable. If you want to know, what immutability is and how it is achieved? follow this post: How to make a java class immutable?

Here the question is WHY? Why immutable? Lets analyze.

1) The very first reason i can think of is performance increase. Java language was developed to speed up the application development as it was not that much fast in previous languages. JVM designers must have been smart enough to identify that real world applications will consist of mostly Strings in form of labels, messages, configuration, output and such numerous ways.

Seeing such over use, they imagined how dangerous can be string’s improper use. So they came up with concept of String pool (next section). String pool is nothing but a collection of some strings mostly unique. The very basic idea behind String pool is to reuse string once created. This way if a particular string is created 20 times in code, application end up having only one instance.

2) Second reason i see as security considerations. Strings are most used parameter type in each aspect of java programming. Be it loading a driver or open a URL connection, you need to pass the information as parameter in form of string. If strings have not been final then they have opened up a Pandora box of security issues.

Apart from above two reasons, i didn’t find any convincing answer for this question. If you any something appealing, please share with me.

String pool concept

String pool is a special memory area separate from regular heap memory where these string constants are stored. These objects are referred string variables during the life cycle of application.

In java, String can be created by many ways. Lets understand them:

1) String assignment

?
1
String str = "abc";

Above code causes JVM to verify if there is already a string “abc” (same char sequence). If such string exist, JVM simply assign the reference of existing object to variable str, otherwise a new object “abc” will be created and its reference will be assigned to variable str.

2) Using new keyword

?
1
String str = new String("abc");

This version end up creating two objects in memory. One object in string pool having char sequence “abc” and second in heap memory referred by variable str and having same char sequence as “abc”.

As java docs says : Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable.

Keyword ‘intern’ usage

This is best described by java docs:

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

?
1
2
3
String str = new String("abc");
 
str.intern();

It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true. Means if s and t both are different string objects and have same character sequence, then calling intern() on both will result in single string pool literal referred by both variables.

Matching Regular expressions

Not so secret but useful feature if you still have not explored it. You must have seen usage of Pattern and Matcher for regular expression matching. String class provides its own shortcut. Use it directly. This method also uses Pattern.matches() inside function definition.

?
1
2
3
String str = new String("abc");
 
str.matches( "<regex>" );

Strings comparison

Another favorite area in interviews. There are generally two ways to compare objects

  • Using == operator
  • Using equals() method

== operator compare for object references i.e. memory address equality. So if two string objects are referring to same literal in string pool or same string object in heap then s ==t will return true, else false.

equals() method is overridden in String class and it verify the char sequences hold by string objects. If they store the same char sequence, the s.equals(t) will return true, else false.

转自:http://howtodoinjava.com/2012/10/28/interview-stuff-about-string-class-in-java/

最后关于内存泄露的我没有转,那个应该是之前版本,有兴趣的大家去网站看看吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
信息数据从传统到当代,是一直在变革当中,突如其来的互联网让传统的信息管理看到了革命性的曙光,因为传统信息管理从时效性,还是安全性,还是可操作性等各个方面来讲,遇到了互联网时代才发现能补上自古以来的短板,有效的提升管理的效率和业务水平。传统的管理模式,时间越久管理的内容越多,也需要更多的人来对数据进行整理,并且数据的汇总查询方面效率也是极其的低下,并且数据安全方面永远不会保证安全性能。结合数据内容管理的种种缺点,在互联网时代都可以得到有效的补充。结合先进的互联网技术,开发符合需求的软件,让数据内容管理不管是从录入的及时性,查看的及时性还是汇总分析的及时性,都能让正确率达到最高,管理更加的科学和便捷。本次开发的医院后台管理系统实现了病房管理、病例管理、处方管理、字典管理、公告信息管理、患者管理、药品管理、医生管理、预约医生管理、住院管理、管理员管理等功能。系统用到了关系型数据库中王者MySql作为系统的数据库,有效的对数据进行安全的存储,有效的备份,对数据可靠性方面得到了保证。并且程序也具备程序需求的所有功能,使得操作性还是安全性都大大提高,让医院后台管理系统更能从理念走到现实,确确实实的让人们提升信息处理效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值