mistakes made with string object in java
DESCRIPTION
Mistakes made with string object in javaTRANSCRIPT
http://www.tutorsonnet.com/java-programming-homework-help.php Page 1
Mistakes Made With String Object In Java:
Summary:
In this presentation we will discuss about the common mistakes made while using
String class object in a Java program.
Mistake 1:
This is a very common mistake to use '==' operator to compare the string objects
instead of '.equals' method. When two string objects are compared with '=='
operator, the result is true only if both the object references point to the single
object. This is called as a 'shallow comparison'. On the other hand, when two string
objects are compared with '.equals' method, the result will be dependent on the
actual string data of the objects and not on the references of the objects. This is
called as a 'deep comparison'.
Mistake 2:
JVM keeps a separate String literal pool for String objects. A common mistake
made while creating a string object is to use 'new' operator. Each time a new string
literal is created without using 'new' operator, JVM first checks if it is present in
the literal pool. If it is there, then the reference to the same is returned otherwise a
new literal is created in the same pool and its reference is returned. But when the
new literal is created using 'new' operator, the literal is allocated the memory
outside the string literal pool even if equal literal presents in the string literal pool.
The string literal pool is used to increase performance and decrease memory
overhead, so using the 'new' operator to create the string literal will not cause any
error to the logic but will increase the memory overhead. So better to use it only
when you know you need it.
http://www.tutorsonnet.com/java-programming-homework-help.php Page 2
Mistake 3:
As we now know that string literals are stored in a separate literal pool, and two
objects with the same literal will point to the same memory of the literal pool, the
string objects are made immutable in Java. That means once the string object is
created its contents cannot be modified. So if you perform any string modification
operations on the string object, for each different result a new object will be
created in the pool. This ensures that if there are any other references to the string
then they will still point to the original string object and not the modified one. And
that is the reason we should avoid using String objects if we need to modify the
string data frequently. It is not the efficient way as it would create many strings
occupying the new storage area. In such a case we should use `StringBuffer' or
`StringBuilder' class.
For More Details:
Website: http://www.tutorsonnet.com
Mail Us: [email protected]
Phone: +91 98803 85500
List of Subjects which we Provide Homework Help:
1 Accounting 9 C 17 JavaScript
2 Finance 10 Visual Basic 18 Shell Scripting
3 Statistics 11 C# (C Sharp) 19 Math
4 Economics 12 Matlab Programming 20 Chemistry
5 Operations 13 Database 21 Physics
6 Marketing 14 ER Diagrams 22 Dissertations
7 Java 15 Oracle 23 Case Study
8 C++ 16 Microsoft (MS) Access 24 Project Management