Remember Me
forgot your password?

Advantage and Disadvantages of String Implementation in Java

Advantages of the String implementation in JAVA

1. Compilation creates unique strings. At compile time, strings are resolved as far as possible. This includes applying the concatenation operator and converting other literals to strings. So hi7 and (hi+7) both get resolved at compile time to the same string, and are identical objects in the class string pool. Compilers differ in their ability to achieve this resolution. You can always check your compiler (e.g., by decompiling some statements involving concatenation) and change it if needed.

2. Because String objects are immutable, a substring operation doesn’t need to copy the entire underlying sequence of characters. Instead, a substring can use the same char array as the original string and simply refer to a different start point and endpoint in the char array. This means that substring operations are efficient, being both fast and conserving of memory; the extra object is just a wrapper on the same underlying char array with different pointers into that array.

3. Strings are implemented in the JDK as an internal char array with index offsets (actually a start offset and a character count). This basic structure is extremely unlikely to be changed in any version of Java.

4. Strings have strong support for internationalization. It would take a large effort to reproduce the internationalization support for an alternative class.

5. The close relationship with StringBuffers allows Strings to reference the same char array used by the StringBuffer. This is a double-edged sword. For typical practice, when you use a StringBuffer to manipulate and append characters and data types, and then convert the final result to a String, this works just fine. The StringBuffer provides efficient mechanisms for growing, inserting, appending, altering, and other types of String manipulation. The resulting String then efficiently references the same char array with no extra character copying. This is very fast and reduces the number of objects being used to a minimum by avoiding intermediate objects. However, if the StringBuffer object is subsequently altered, the char array in that StringBuffer is copied into a new char array that is now referenced by the StringBuffer. The String object retains the reference to the previously shared char array. This means that copying overhead can occur at unexpected points in the application. Instead of the copying occurring at the toString( ) method call, as might be expected, any subsequent alteration of the StringBuffer causes a new char array to be created and an array copy to be performed. To make the copying overhead occur at predictable times, you could explicitly execute some method that makes the copying occur, such as StringBuffer.setLength( ). This allows StringBuffers to be reused with more predictable performance.

The disadvantages of the String implementation are

1. Not being able to subclass String means that it is not possible to add behavior to String for your own needs.

2. The previous point means that all access must be through the restricted set of currently available String methods, imposing extra overhead.

3. The only way to increase the number of methods allowing efficient manipulation of String characters is to copy the characters into your own array and manipulate them directly, in which case String is imposing an extra step and extra objects you may not need.

4. Char arrays are faster to process directly.

5. The tight coupling with String Buffer can lead to unexpectedly high memory usage. When StringBuffer toString( ) creates a String, the current underlying array holds the string, regardless of the size of the array (i.e., the capacity of the StringBuffer). For example, a StringBuffer with a capacity of 10,000 characters can build a string of 10 characters. However, that 10-character String continues to use a 10,000-char array to store the 10 characters. If the StringBuffer is now reused to create another 10-character string, the StringBuffer first creates a new internal 10,000-char array to build the string with; then the new String also uses that 10,000-char array to store the 10 characters. Obviously, this process can continue indefinitely, using vast amounts of memory where not expected.

The advantages of Strings can be summed up as ease of use, internationalization support, and compatibility to existing interfaces. Most methods expect a String object rather than a char array, and String objects are returned by many methods. The disadvantage of Strings boils down to inflexibility. With extra work, most things you can do with String objects can be done faster and with less intermediate object-creation overhead by using your own set of char array manipulation methods.

Kamlesh Patel

Spec-India is Software Development Company that Offers Custom Application Development, Mobile Software Application Development, Custom Software Development, Offshore Software Solutions Services, Legacy System Migration, Onsite Software Development and Application Support and Maintenance.

Rate this Article: 5 / 5 stars - 1 vote(s)
Print Email Re-Publish


Article Source: http://www.articlesbase.com/programming-articles/advantage-and-disadvantages-of-string-implementation-in-java-607418.html
Add new Comment



Captcha

  • Latest Programming Articles
  • More from Kamlesh Patel

php software development company

By: usha sharma | 08/07/2009
Professional Web Development Company phpmaestro provides custom website development web application development ecommerce website design and development services. Custom web application development services and professional website development at affordable rates from phpmaestro Company. www.phpmaestro.com is a php Application Development Shopping Cart for e-commerce stores. Our shopping cart software gives our client full control over your online shop its products design development prices sh

Hire ASP .Net Developers Hire Dedicated ASP.Net Developers Offshore ASP.Net Programmers

By: Arshad | 08/07/2009
Since the concept of outsourcing changed the economy of many countries it has been very clear that paying Indian development service providers is very much economical than funding an in-house private team. Analysis says that more than 50% of the cost can be saved by inking contracts with Indian IT service providers.

Windbg Minidump Tutorial:Setting up & Reading Minidump Files

By: Jeannie Lee | 07/07/2009
Windgb Minidump tutorial to set up and read minidump files (.dmp). Setting Symbol File Path. Output of Windbg command. windbg.exe -z [file path to minidump file.dmp] -c !analyze -v.

Javascript Validate Name Field

By: Jeannie Lee | 07/07/2009
Simple Javascript tutorial on validating a name field. Checks to see if there is a value in the name field with Javascript after the user submits a form.

Logo Design- The image creator!

By: Jhonny Sharma | 07/07/2009
Brand image is something that many consumers look for while buying a product. Brand image and brand positioning have become important concepts in the corporate world. When image building strategies are talked about, what tops the list is a creative logo design.

Design principles in logo

By: Jhonny Sharma | 07/07/2009
An element of balance is a mandatory aspect of design. A design is considered to be a great design when it incorporates all design aspects in the required proportion. The design principles are vital for any kind of design.

Organization specific software

By: Manish Shrivastava | 07/07/2009
Different businesses have different technological needs depending upon the type of work they are engaged in. Some businesses might require minimum use of software technology where there might be others whose very business might depend upon the optimum use of technology.

Hire .Net Developers: Hire ASP.Net Programmers

By: Arshad | 07/07/2009
Since the concept of outsourcing changed the economy of many countries it has been very clear that paying Indian development service providers is very much economical than funding an in-house private team. Analysis says that more than 50% of the cost can be saved by inking contracts with Indian IT service providers.

Recommended Technologies by SAP for Integration

By: Kamlesh Patel | 16/06/2009 | Software
Seamless integration across business applications has always been a critical area of interest for any organization. The criticality of application integration is obvious from the fact that this is often a critical factor in purchase decision making.

Some Important Features of SOAP

By: Kamlesh Patel | 16/04/2009 | Information Technology
The SOAP standard contains the information for how the messages should be sent, the format the XML appears in, the different primitive types supposed, the roles different pieces of software take during the transmission of the SOAP documents, and the type of transports available, such as HTTP.

Frequency Fundamentals of Wireless Networks

By: Kamlesh Patel | 19/03/2009 | Information Technology
Many of the wireless technologies in the WPAN, WLAN, and WWAN categories transmit information using radio waves. For this to take place, the data is superimposed onto the radio wave, which is also known as the carrier wave, since it carries the data.

Web Services And Soap

By: Kamlesh Patel | 19/02/2009 | Software
When given any XML parser, such as a Microsoft’s XML DOM component, anyone can pass a function name embedded into an XML message. Therefore, you do not need SOAP or any formal protocol around XML Web services to accomplish this.

Some Important Features of Dot Net

By: Kamlesh Patel | 20/12/2008 | Programming
Migrating to .NET from existing languages and platforms has been made much easier; Especially if that environment is COM or Java. COM, Interop is built into the framework, and C# will be very familiar for those developing in Java currently.

What is Grid Computing

By: Kamlesh Patel | 19/11/2008 | Information Technology
Grid computing is all about using the untapped resources of computers connected to a network. IBM perhaps defines it most succinctly: Grid computing is applying resources from many computers in a network-at the same time-to a single problem.

Advantage and Disadvantages of String Implementation in Java

By: Kamlesh Patel | 17/10/2008 | Programming
The advantages of Strings can be summed up as ease of use, internationalization support, and compatibility to existing interfaces. Most methods expect a String object rather than a char array, and String objects are returned by many methods.

Submit Your Articles Free: Signup


Article Categories




Use of this web site constitutes acceptance of the Terms Of Use and Privacy Policy | User published content is licensed under a Creative Commons License.
Copyright © 2005-2008 Free Articles by ArticlesBase.com, All rights reserved. (0.30, 6)