View RSS Feed

String

Java String

  1. Understanding String memory usage

    by , 04-26-2012 at 06:36 PM
    Fields which are present at the String object shall be looked first of all so that for a better understanding of the above calculations:

    • a char array: Separate object which consist of the actual characters;
    • an integer offset where the string starts;
    • String Length
    • Another int, for hash code’s cached calculations.



    This shows that if no characters are present in a string, even then 4 bytes would be required for sake of the char array reference along ...
    Categories
    String
  2. Memory usage of Java Strings and string-related objects

    by , 04-26-2012 at 06:35 PM
    Everywhere strings are present. It is quite difficult thing to write down an application that is not used by them or others use it extensively. Various typical applications like server entities that are obtained from databases, or data present to be displayed at web, most of the objects that have been stored on heap might be Strings. Therefore, when usage of memory is crucial it means that understanding of the Strings memory usage or the related objects is also crucial.

    If your have ...
    Categories
    String
  3. Getting Characters and Substrings by Index

    by , 04-26-2012 at 06:34 PM
    Character can be obtained at one particular index, within a string, by charAt() accessor method’s invoking. First character index is 0 however last character index is length()-1. This given example shows the code which obtains the character, at index 9 present in string.
    String anotherPalindrome = "Niagara. O roar again!";

    char aChar = anotherPalindrome.charAt(9);

    Indices start with 0 therefore the characters which are present at the index 9 are 'O'. ...
    Categories
    String
  4. Converting Strings to Numbers

    by , 04-26-2012 at 06:32 PM
    In a string object, usually a program would be ending up with numeric data, a value which user will enter. For example:
    Primitive numeric types (Short, Integer, Byte, Double, Long and Float) are being wrapped by the Number subclasses. A class method known as valueOf is provided by each which makes a string to be converted into object of that type. For example, ValueOfDemo will be getting 2 strings to convert them into no. and then performing the arithmetic operations at values.
    ...
    Categories
    String
  5. The difference between == and equals()

    by , 04-26-2012 at 06:31 PM
    The == operator:
    References are compared by == operator, of the 2 objects which are present in memory. When these 2 objects are pointing similar memory location, then it returns true. It shall be remembered that in java strings are immutable therefore if string variables “str1” is present along with “abc” value and then another variable along with other value is created, instead of new string variable creation with same value, then Java simply would be pointing str2 to similar location of ...
    Tags: equals() Add / Edit Tags
    Categories
    String
  6. String Conversion and toString() in Java

    by , 04-26-2012 at 06:30 PM
    When data is converted into string representation while concatenation is done, versions that are overloaded string conversion methods valueOf() are called. They are defined by the help of String.valueOf(), which are overloaded for type Object and all simpler types. In case of simple types, a string is returned by the valueOf() in which human readable are present that are equal to value that has been called. On the object, toString() method is called by ValueOf(). Here toString() method shall be ...
    Categories
    String
  7. String Equality and Interning

    by , 04-26-2012 at 06:29 PM
    In Java Strings are the objects however they resemble primitives like charS or intS. In that java source, code may be having the String literals and + operator might be used to concatenate the Strings. These features are quite convenient, however similarity present b/w Strings & primitives may cause certain confusions, when comparison is done of the Strings.

    Two mechanisms are provided by Java to do the test for equality. Use “==” operator for testing of the primitive values equality. ...
    Categories
    String
  8. Differences between String and StringBuffer in Java

    by , 04-26-2012 at 06:29 PM
    Major difference that exist b/w StringBuffer and String is the immutable string, while mutability of the StringBuffer means that StringBuffer object could be modified once it has been created, without creation of new object. StringBuffer is made an ideal choice by this mutable property to deal with Java strings. StringBuffer can be converted into String by the help of toString() method. StrinBuffer Vs String, or difference b/w String & StringBuffer is most popular for the interview questions, ...
    Categories
    String
  9. Problem with String in Java

    by , 04-26-2012 at 06:28 PM
    In java string, biggest strength well known as immutability is actually the biggest problem encountered in case it is not used appropriately. String is created many times and lots of operations are being performed at them. For example, string conversion into lowercase, uppercase, concatenating with string or getting substring out. Though String is considered as immutable class, when a new string is created evertime and the old one gets discarded that makes temporary garbage a lot, in heap. If String ...
    Categories
    String
  10. Concatenating Strings

    by , 04-26-2012 at 06:27 PM
    A method is present in String class for concatenation of 2 strings:
    Java Code:
    string1.concat(string2);
    New string will be returned, that is, string1 and string2 will be added in the end. Concat() method could also be used along with string literals:
    Java Code:
    "My name is ".concat("Zara");
    Commonly, strings are concatenated along with +operator:
    Java Code:
    "Hello," + " world" + "!"
    Result ...
    Categories
    String
  11. String Length

    by , 04-26-2012 at 06:26 PM
    An object’s information is seeked by certain methods and those methods are named as accessor methods. Among them, one that could be used along with strings is known as length() method that would be returning no. of characters present in string object.
    Java Code:
    public class StringDemo{
       public static void main(String args[]){
          String palindrome = "Dot saw I was Tod";
          int len = palindrome.length();
          System.out.println( "String Length
    ...
    Categories
    String
  12. Creating Strings

    by , 04-26-2012 at 06:25 PM
    Another direct method for creation of string is, to write:
    Java Code:
    String greeting = "Hello world!";
    In your code, whenever string literal is encountered, a String object is created by the compiler along with value, “Hello World”
    Eleven constructors are present in String class which permits to give the string’s initial value by usage of different kind of resources, like character array.
    Java Code:
    public class StringDemo{
       public static void main(String
    ...
    Categories
    String
  13. String representation of Objects

    by , 04-26-2012 at 06:25 PM
    Tostring() method is implemented by all java objects that will be returning String object, which will be describing that object in a best manner. For instance, red colored object can be printed out by using:
    Java Code:
    Color color = new Color( 255, 0, 0 );
    String colorStr = color.toString();
    Console.println( colorStr );
    Printout will be:
    Java Code:
    java.awt.Color[r=255,g=0,b=0]
    In such a case, there is no need to call tostring() method explicitly. ...
    Categories
    String
  14. Strings in Java

    by , 04-26-2012 at 06:24 PM
    The most common use of the string class is manipulation and holding of the text strings, in Java programs. In case of automatically imported standard java.lang packages this is usually found, therefore nothing special is needed to use it.


    String class can be used in its simplest form by typing text that is usually double quotes surrounded. This is what is known as String literal.
    "This is a Java String"

    Anywhere a String object is needed, string ...
    Categories
    String
  15. String vs. StringBuffer

    by , 01-08-2012 at 11:26 AM
    Following are the major differences between these 2:

    • String is immutable as compared to StringBuffer. You need to create a new instance to replace an object.

    Java Code: This is an example of immutable string
    //Inefficient version using immutable String
    String output = “Some text”
    Int count = 100;
    for(int I =0; i<count; i++) {
    output += i;
    }
    return output;
    • StringBuffer is mutable. Also it is not synchronized.

    Java Code: This is an example of mutable string
    //More
    ...

    Updated 01-14-2012 at 06:42 PM by Java String

    Categories
    String , StringBuffer