August 16, 2011

String class in Java

String:
String is a sequence of characters and it is widely used in Java.
First thing which we need to know about string in Java is that, it is not a primitive data type.
String is one of the predefined class that is implemented in Java. It helps to handle the strings in a required way.

We have to create an object or reference variable for the String class.

Syntax:
              String str = new String();

String Constructors:
There are many constructors in String class. Now we will see the most important ones.
  1. String( ) - Initialises the String object with no values in it.
  2. String(String str) - Initialises the String object with the values in str.
  3. String(byte[ ] b) - Initialises the String object using byte array b. Normally a byte stores numerical value. In this when we pass a byte array, the numerical values will be taken as ASCII values and converted into corresponding characters. Then the characters will be assigned to the String object.
  4.  String(byte[ ] b, int startindex, int count) - Initialises the String object from the byte array b starting from the startindex and length be the count.

    No comments:

    Post a Comment

    Connecting Java program to Database

    It is very simple to establish a connection to the Database from a Java program          Class.forName("com.mysql.jdbc.Driver...