Vous êtes sur la page 1sur 9

Strings, which are widely used in Java programming, are a sequence of characters.

In the Java programming language, strings are objects. The Java platform provides the String class to create and manipulate strings.
String is an array of characters:

char[] Sample={H', e', l', l, o}; String Sample1 = new String(Sample);


Or String Sample1 = Hello";

Important Points to Note: String is a Final class; i.e once created the value cannot be altered. Thus String objects are called immutable. The Java Virtual Machine(JVM) creates a memory location especially for Strings calledString Constant Pool. Thats why String can be initialized without new key word.

String class falls under java.lang.String hierarchy. But there is no need to import this class. Java platform provides them automatically.

Important Points to Note: String reference can be overridden but that does not delete the content; i.e., if String h1 = hello; h1 = hello+world; then hello String does not get deleted. It just looses its handle. Multiple references can be used for same String but it will occur in the same place; i.e., if String h1 = hello; String h2 = hello; String h3 = hello; then only one pool for String hello is created in the memory with 3 references-h1,h2,h3

Important Points to Note:


If a number is quoted in then it becomes a string, not a number any more. That means if String S1 =The number is: + 123+456; System.out.println(S1);

then it will print: The number is: 123456


If the initialization is like this: String S1 = The number is: +(123+456); System.out.println(S1); then it will print: The number is:579

length( ) This is a numerical returning method that gives the length of the string. It returns an integer value. Example: int citylength = city.length( ); System.out.println(citylength); charAt (n) This is a value returning method. The argument states which character to return. The subscripting of the locations of the characters starts with zero. Example: String holiday="Thanksgiving"; System.out.println (holiday.charAt (4) ); //prints out a 'k Example: String st1 ="Wally"; System.out.println (st1.charAt (0) ); //prints out a 'W'

trim() This is a value returning method that returns a copy of the argument after removing its leading and trailing blanks (not embedded blanks - blanks within the statement). The string will not be changed in memory. Example: String burp = " hic up "; System.out.println ( burp.trim() ); // prints out hic up toLowerCase() In memory, the string is not changed, but it will be printed out in all lower case letters. Example: String cityname = "Houston"; System.out.println (cityname.toLowerCase() );

substring (Start, End) This is a value returning method. A string is returned beginning at Start subscript up to but not including the End subscript. Example: String sport = "football"; System.out.println (sport.substring (1,3) ); // prints out "oot"substring ( Start)
Example: String sport = "football"; System.out.println (sport.substring (1) ); // prints out "ootball"

indexOf("Garfield") Returns the beginning subscript of the string where "Garfield" is found Example: (find the comma in a city, state, zip listing) String city = "Fulton, New York 13069"; System.out.println ( city.indexOf (",")); // prints 6 equals ( ) Used to compare two strings with the method to see if they are exactly the same, this includes any blanks or spaces within the string. Example: (check to see if the user entered a dog name of Bruno) if (dogname.equals(Bruno"); System.out.println ( "The user entered Bruno.);

compareTo() Compares strings to determine alphabetic location. Returns a zero if the two strings are equal, a negative if the first string is alphabetically before the compared string, and a positive if the first string is alphabetically after the compared string. Example: String subject = "mathematics"; boolean answer; answer = subject. compareTo ("biology"); // returns is a positive answer = subject. compareTo("philosophy"); // answer is negative answer = subject.compareTo (" mathematics"); //answer is zero

Vous aimerez peut-être aussi