Working with Strings in Java - String Class in Java, How to create Strings in Java


Working with Strings in Java, String Class in Java, How to create Strings in Java

String is a collection of characters. To working with string in c we need to make a char array but in java we don't need to do this because java provide many classes  to working with strings like String, StringBuffer and StringBuilder.


($). Constructing a String :-In Java there is many ways to constructing  Strings some of constructing methods are as follows--
  •   String message="Welcome to Java";
  •   String message=new String("Welcome to Java");
  •   String s=new String();
Operations on Strings in Java :-

($). Obtaining String Length and retrieving individual characters in a String. 
($). String Concatenation (concat()).
($). Substrings (substring(index), substring(start,end)).
($). Comparisons (equals, compareTo()). 
($). String Conversions.
($). Finding a Character or a substring in a String.
($). Conversions between Strings and Array.
($). Converting Characters and Numeric  values to String.


Types of Strings in Java :-


  • Mutable Strings
  • Immutable Strings 
Immutable Strings :-
                           That String Objects which value can't be changed after declaration are called Mutable strings. The String class in java is the example of Mutable strings.

Example

String str="Hello World";
String str1=str.toUpper();
System.out.println(str);
System.out.println(str1);

In above example the 2nd statement str.toUpper() method change the case of the String "Hello World" to "HELLO WORLD" and store it to the another String object str1 but it can't be change the actual String contents. and the output of the above example come as
Hello World
HELLO WORLD

All String Class Methods and Description
Mutable Strings :-
                                 That String Objects which value can't be changed after declaration are called Mutable strings. The StringBuffer class in java is the example of Mutable strings.

StringBuffer str="Hello World";
StringBuffer str1=str.toUpper();
System.out.println(str);
System.out.println(str1);


In above example the 2nd statement str.toUpper() method change the case of the String "Hello World" to "HELLO WORLD" and store it to the another String object str1 and it also be change the actual String contents. and the output of the above example come as
HELLO WORLD
HELLO WORLD




{ 0 comments... read them below or add one }

Post a Comment