Presentation is loading. Please wait.

Presentation is loading. Please wait.

String Class.

Similar presentations


Presentation on theme: "String Class."— Presentation transcript:

1 String Class

2 String Class Present in java.lang package
An object of the String class represents a fixed length, immutable sequence of characters A lot of other string manipulation methods are available JavaDocs can be referred for a detailed list of methods

3 String Class Strings are fundamental part of all computing languages.
At the basic level, they are just a data structure that can hold a series of characters. However, strings are not implemented as a character array in Java as in other languages.

4 String Class Strings are implemented as two classes in Java
java.lang.String provides an unchangeable String object java.lang.StringBuffer provides a String object that can be amended

5 String Class Declaring String //assign a literal to a String variable
String name = “Robert”; //calling a method on a literal String char firstInitial = “Robert”.charAt(0); //calling a method on a String variable char firstInitial = name.charAt(0);

6 Empty String An empty String has no characters. It’s length is 0.
Not the same as an uninitialized String. String word1 = ""; String word2 = new String(); Empty strings private String errorMsg; errorMsg is null

7 String Class Defines a data type used to store a sequence of characters Strings are objects String objects can't be modified: If attempted to do so. Java creates a new object having the modified character sequence String myName = “Elliot Koffman”; myName = “Koffman, Elliot”; String Value = “Elliot Koffman” myName = String Value = “Koffman, Elliot”

8 String Class Common String Operations String concatenation
Many more, check String class in Java Docs String u - "Hello"; String t = " World"; String s = u + t; // s refers to "Hello world" int i = s.length(); // returns 11 u.equals(t) // comparison, returns false u.compareTo(t) // returns negative number s.charAt(l) // returns 'e', index runs // from 0 to length-1 String x = u.toUpperCase(); //returns "HELLO"

9 String Class Referring Java Documentation
Java provides a rich set of library classes Java API Documentation provides detailed help on all classes Browse Java API Documentation

10 Assessment – Java Basics: Part 2
The quiz contains 10 questions. You will have one attempt to answer each question. You can exit the quiz at any time. If you exit the quiz without completing all 10 questions, on return you will have to start the quiz from the beginning. Scoring 80% or higher means you have understood the topic well. Start

11 If we do not specify access modifier to any member of class, which of the following is automatically assigned to it? public default protected

12 Java Strings are also objects.
True False

13 What happens with objects which are not referenced?
They are kept in memory They are Garbage Collected They are moved to Secondary memory

14 In Java, String objects are mutable?
True False

15 What is the initial value of any Reference Variable?
1 null

16 Identify the incorrect statement regarding static method
Accessed using class name.method name Is not a class method Creation of instance not necessary for using static method

17 Which of the following initializes an object?
Constructor Garbage Collector Destructor

18 A class is created with which of the following keyword?
struct enum

19 A programmer want to print number of command line arguments passed
A programmer want to print number of command line arguments passed. How he will achieve this? Using length property of array Using length method of array Using len method of array

20 Identify the incorrect statement regarding static variable
Belong to a Class A single copy to be shared by all instances of the class Creation of instance is necessary for using static variables

21 The Results


Download ppt "String Class."

Similar presentations


Ads by Google