View RSS Feed

Java String

All about Java String.

  1. Problem with String in Java

    by , 04-26-2012 at 07: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
  2. Concatenating Strings

    by , 04-26-2012 at 07: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
  3. String Length

    by , 04-26-2012 at 07: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
  4. Creating Strings

    by , 04-26-2012 at 07: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
  5. String representation of Objects

    by , 04-26-2012 at 07: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
  6. Strings in Java

    by , 04-26-2012 at 07: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
  7. String vs. StringBuffer

    by , 01-08-2012 at 12:26 PM
    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 07:42 PM by Java String

    Categories
    String , StringBuffer
Page 2 of 2 FirstFirst 12