SlideServe
Menu
Préférences
© SlideServe · v2026

Understanding the String Class in Java: Overview, Methods, and Constructors

kendall-tyson avatar
10 diapositives · 250 Views · Nov 13, 2014

This guide provides a comprehensive overview of the String class in Java, highlighting its nature as a class (not a primitive type) and its unique features. Learn about the various constructors available for creating String objects, the importance of methods like length(), charAt(), and equals(), and how to manipulate strings with toLowerCase(), toUpperCase(), and substring(). Understand the distinction between comparing strings with.equals() versus ==, and discover how parseInt() can convert strings to integers. Ideal for beginners and those looking to deepen their Java knowledge.

Understanding the String Class in Java: Overview, Methods, and Constructors - slide 1
01 / 10Understanding the String Class in Java: Overview, Methods, and Constructors
1 / 17

Télécharger la présentation

Understanding the String Class in Java: Overview, Methods, and Constructors
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. STRINGS Chapter 13

  2. String class • A String is a Class – not a primitive • Therefore, a String is a ‘doughnut’ • A String object has data • A String object has methods

  3. constructors • A constructor is a method that is executed when an object is created • The String class has multiple constructors • String s1; • String s2 = new String(); • String s3 = new String(“Buffalo”); • String s4 = new String(s3); • String s5 = “Bills”; • String s6 = s3 + “ “ + s5 + “ are great”;

  4. String methods • Since a String is a class, a String object has methods • To declare a String: • String s1 = “Buffalo”; • To run a String method, you key the String object name, a . (period), and then the method name • s1.charAt(3)

  5. length() • The length() method returns an int that contains the number of characters in the String. • A String is not an array of characters terminated by a null (that is C) • String s1 = “Buffalo”; • int x = s1.length(); // x is 7 • String s2 = “Hi Mom and Dad!”; • int x = s2.length(); // x is 15

  6. charAt(int) • charAt(int x) returns the character that is in the xth position of the String. • Note: the first character of a String is in position 0 (not 1) • String s1 = “Buffalo”) • char ch = s1.charAt(5); // ch is an l • char ch = s1.charAt(0); // ch is an B • char ch = s1.charAt(9); // error

  7. equals(String) • The equals method returns a true or false • String s1 = “Buffalo”; • String s2 = “Bills”; • if(s1.equals(s2)) // returns false • If(s1.equals(“Buffalo”) // returns true • If(s1 == s1) will probably return false • You cannot use == to compare Strings. • == compares the references to 2 Strings

  8. toLowerCase() toUpperCase() • toLowerCase() returns a copy of the String with all lower case • toUpperCase() returns a copy of the String with all upper case • String s1, s2, s3 = “Buffalo”; • s1 = s3.toLowerCase(s3); // buffalo • s2 = s3.toUpperCase(s3); // BUFFALO

  9. substring() • substring(int x, int y) • Returns a substring from position x to one character before position y • substring(int x) • Returns a substring from position x to the end of the String • String s1, s2 = “Buffalo”; • s1 = s2.substring(2,5); // returns ffa • s1 = s2.substring(4); // returns alo

  10. parseInt(String) • parseInt() is a method in the Integer class • parseInt() converts a String into an int • Int x; • String s1 = “123” • X = Integer.parseInt(“2”); // x is 2 • X = Integer.parseInt(s1); // x is 123 • X = Integer.parseInt(“ab”); // error