String in Java





  • Unlike many other languages that implement strings as character arrays, Java implements strings as an object of class java.lang.String.

  • String are immutable objects in java.

  • Meaning of Immutability :-

  • We can’t change the sequence of Characters encapsulated in a String Object.

  • Each operation on that object will produce a new String object.

  • Creating String Objects :

  • Using new Keyword :

    String str = new String( );

    String str = new String( "india" );

  • Using String Literal :

    String str = "bharat";

  • Construct String object from char array:

    char [ ] ar = {'h', 'e', 'l', 'l', 'o'};
    String str = new String( ar );

  • When we create an object of String class using new operator, it creates String object in Heap memory area.

  • When we create an object of String class using String Literal, it creates String object in String Constant Pool area.

  • In Heap, we can create two or more objects of String class having identical character sequences.

  • In String Constant Pool, duplicate string objects are not allowed.


Example :

public class StringDemo
{  
 public static void main(String []ar)
 {  
  //creating by string literal
  String s1="Java";

  char ch[]={'P','r','o','w','e','s','s'};
  //converting char array to string  
  String s2=new String(ch);

  //creating by new keyword
  String s3=new String("App");
  System.out.println(s1);  
  System.out.println(s2);  
  System.out.println(s3);  
 }
}   
OUTPUT :
Java
Prowess
App

Java String class methods :

The java.lang.String class provides so many methods to perform operations on String object.

You can check the list of all methods by typing the command on terminal (command prompt )
javap java.lang.String

SOME USEFUL METHODS ARE :

1. concat() - concatenates two String objects

String s1 = "Java";
String s2 = " Prowess";
String s3 = s1.concat(s2);
System.out.print(s3);
  [OUTPUT :]
Java Prowess

2. length() - returns the characters count available in string.

String s1 = "Indian";
int  x = s1.length( );
System.out.print(x);
  [OUTPUT :]
6

3. toUpperCase, toLowerCase - changes the case from lower to upper and vice-versa.

String s1 = "hello";
String s2 = s1.toUpperCase( );
System.out.print(s2);
  [OUTPUT :]
HELLO

4. indexOf() - returns the index of character in String if available , return -1 otherwise.

String s1 = "Java Prowess";
int  y  =  s1.indexOf('P');
System.out.print(y);
  [OUTPUT :]
5

5. charAt() - returns character at specified index.

String s1 = "Java Prowess";
char ch = s1.charAt(5);
System.out.print(ch);
  [OUTPUT :]
P

6. equals() - compare two string and returns boolean

String s1 = "J2EE";
String s2 = "JEE";
boolean b = s1.equals(s2);
System.out.printl(b);	
  [OUTPUT :]
false

7. equalsIgnoreCase() - compare two string ignore the case and returns boolean

String s1 = "J2EE";
String s2 = "J2Ee";
boolean b = s1.equalsIgnoreCase(s2);
System.out.print(b);
  [OUTPUT :]
true

8. subString() - return substring from the given index

String s1 = "Java Prowess";
//from given index to last index
String s2 = s1.subString(5);
    OR
//from given index to secondIndex-1
String s3 = s1.subString(5,7);
System.out.println(s2);
System.out.print(s3);
  [OUTPUT :]
Prowess
Pr

StringBuffer Class :

  • java.lang.StringBuffer : StringBuffer is a peer class of String, that represents mutable type String objects on Heap memory area

  • StringBuffer defines these three constructors:
    StringBuffer( ) – Default Constructor
    StringBuffer(int size) - int Defines size of String Object
    StringBuffer(String str) – to get StringBuffer object from String object

    Example :
    StringBuffer sb1 = new StringBuffer( );

    StringBuffer sb = new StringBuffer("abcd");

Next topic is exception-handling




 







Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in



CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved