..


Sponsored Links

Apply the equals method in StringBuffer and StringBuilder

Article written by Damiano Verda
Page 1 of 2

The equals method is commonly used in a program Java to test the equality of two objects. This is actually a method defined by the Object class from which all other classes inherit according to the founding principle of Java: "Everything is an object."

But of course the concept of equality between two objects must be verified in a timely manner according to the class of the objects themselves: in other words, the equals method can and often must be redefined (or overridden) by class interest.

The process of redefinition may be charged to the programmer in the case of special classes, defined by the programmer himself, but it's usually already been done for the most common classes.

The aim of this paper is to examine the nature and limits of the redefinition of the equals method for StringBuffer and StringBuilder classes, use extremely common for managing sets of alphanumeric characters.

StringBuffer and StringBuilder

Both classes are used to store a dynamic sequence of characters, which may then change during the execution of the program and both inherit from the same parent class String.

StringBuilder can be used successfully only if the program involves a single thread while StringBuffer supports the necessary synchronization for multithreading.

The peculiarity of these classes, with regard to the equals method, is embodied in the fact that is not made ​​any redefinition. The redefinition (or overriding) of this method is in fact made ​​only in the base class String, which both derive. This choice leads to consequences not entirely intuitive, which influence the use of those classes.

In the same category ...
E-Learning
Linux (Course) Linux (Course)
Complete guide to open-source system. From 49 €.
PHP (Course) PHP (Course)
Full course for creating dynamic Web sites. From 49 €.
Ruby and Ruby on Rails (Course) Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €.
Sponsored Links