Differences between String and StringBuffer in Java - Prof. Brian F. Hanks, Study notes of Javascript programming

The key differences between the string and stringbuffer classes in java. It covers their immutability, methods for modification, and syntax for initialization and concatenation. The document also introduces the stringbuffer constructors and exceptions. Students are encouraged to identify issues in sample code and correct them using try/catch blocks.

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-9cq-1
koofers-user-9cq-1 ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 โ€“ Lecture 37
Announcements: Practice exam next week. Read 12.5.2, 12.5.3.
Last Time: String and StringBuffer
What are some major differences between the String and StringBuffer classes?
- Strings are immutable (value cannot be changed)
- StringBuffer class contains methods that change the value of a StringBuffer object
- String has special syntax for initialization and concatenation
Did you have to import anything to use StringBuffer? Why not? Because it is in the
java.lang package, which gets imported automatically.
The StringBuffer class is similar to String, except that StringBuffer objects can be
modified. Let's look at some methods provided by the StringBuffer class:
append โ€“ add some characters to the end of the StringBuffer
insert โ€“ insert characters in the middle of the StringBuffer
setCharAt โ€“ change the character at position p to a new value
deleteCharAt โ€“ delete the character at position p
toString โ€“ return a String representation of the StringBuffer
Also has methods like the String class: length, charAt.
Constructors:
StringBuffer()
StringBuffer( String s )
Exceptions
Let's look at the problems project. The Example class includes three public methods.
Don't execute these methods yet!
Remind students about array initialization.
Let's look at the loop1 method. What is wrong with it? What will happen when we run it?
How about the listExample method? What's going to happen when we run it?
What about the getUserInput method? Is it correct?
Java throws an Exception when something goes wrong that it cannot recover from.
A couple of weeks ago, we said that there were two types of errors that we could make
when we wrote a program: syntax errors and logic errors.
pf3
pf4

Partial preview of the text

Download Differences between String and StringBuffer in Java - Prof. Brian F. Hanks and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 โ€“ Lecture 37

Announcements: Practice exam next week. Read 12.5.2, 12.5.3. Last Time: String and StringBuffer What are some major differences between the String and StringBuffer classes?

  • Strings are immutable (value cannot be changed)
  • StringBuffer class contains methods that change the value of a StringBuffer object
  • String has special syntax for initialization and concatenation Did you have to import anything to use StringBuffer? Why not? Because it is in the java.lang package, which gets imported automatically. The StringBuffer class is similar to String, except that StringBuffer objects can be modified. Let's look at some methods provided by the StringBuffer class: append โ€“ add some characters to the end of the StringBuffer insert โ€“ insert characters in the middle of the StringBuffer setCharAt โ€“ change the character at position p to a new value deleteCharAt โ€“ delete the character at position p toString โ€“ return a String representation of the StringBuffer Also has methods like the String class: length, charAt. Constructors: StringBuffer() StringBuffer( String s ) Exceptions Let's look at the problems project. The Example class includes three public methods. Don't execute these methods yet! Remind students about array initialization. Let's look at the loop1 method. What is wrong with it? What will happen when we run it? How about the listExample method? What's going to happen when we run it? What about the getUserInput method? Is it correct? Java throws an Exception when something goes wrong that it cannot recover from. A couple of weeks ago, we said that there were two types of errors that we could make when we wrote a program: syntax errors and logic errors.

Let's look at the logic error category in more detail. What kinds of errors did we just see? These are called runtime errors. The program terminates because something goes wrong. There is another type of logic error โ€“ the program doesn't throw an exception, but it produces the wrong result. What distinctions can you see between the runtime errors we just saw?

  • Some are due to programmer error: IndexOutOfBoundsException, NullPointerException.
  • Others are due to user errors: InputMismatchException. In this case, the program is correct, but the user made an error. What can we do to correct these problems?
  • Fix the program errors (for the first type of problem)
  • Catch the Exception so that the program does not terminate Can you think of any other situations where something can go wrong that is not because of programmer error?
  • File Not Found
  • Reading past the end of a file Exception Syntax Normally, a program terminates when an exception is thrown. You can override this behaviour by enclosing statements that might cause an exception in a try-catch block. try { statements that might throw an exception } catch ( ExceptionClass exceptionArg ) { statements to handle exception } You can have multiple catch blocks after a try - one for each exception class that you wish to handle. An exception matches a catch block if the type of the exception is the same as the argument type, or is a subclass of the argument type. You don't need to worry about what subclass means โ€“ all that matters for now is that Exceptions are organized in a hierarchy, and some of them are more specialized than others. For example catch (IOException e) { ... }

Lab 29 Create a new project. Create a new class named DaysPerMonth. This class has one public method: public void displayMonth() This method should call the following two private methods to:

  1. Ask the user to enter an int with a value between 1 and 12. The value represents a month of the year. Name this method getMonth.
  2. Print out a message that says how many days are in that month. Name this method printDaysPerMonth. For example, if the user entered a 3, the message would be: There are 31 days in March. Your getMonth method must:
  • Validate the user input to make sure that the value is between 1 and 12.
  • Make sure that the user enters an integer (use try/catch).
  • Keep asking the user for a value until they enter a valid one. Hint The {} form of array initialization will be very helpful here. You will probably need two arrays: one for the number of days per month, and one for the names of the months. (You may assume that February has 28 days). Use the homework submission system to turn in DaysPerMonth.java as "Lab 29".