Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6...

48
Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types

Transcript of Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6...

Page 1: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

Garside, JAVA: First Contact,

2ed

Java First Contact – 2nd Edition Garside and Mariani

Chapter 6 More Java Data

Types

Page 2: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

StringBuffer

Why we need it?

Page 3: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

constructors

StringBuffer()           Constructs a string buffer with no characters in it and an initial capacity of 16 characters. StringBuffer(int length)           Constructs a string buffer with no characters in it and an initial capacity specified by the length argument.

Page 4: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

StringBuffer(String str)           Constructs a string buffer so that it represents the same sequence of characters as the string argument; in other words, the initial contents of the string buffer is a copy of the argument string.

Page 5: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

Methods

public int length() Returns the length (character

count) of this string bufferpublic int capacity() Returns the current capacity of the

String buffer. The capacity is the amount of storage available.

Page 6: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

public void setLength(int newLength) Sets the length of this String buffer. This string

buffer is altered to represent a new character sequence whose length is specified by the argument.

if the newLength argument is less than the current length of the string buffer, the string buffer is truncated to contain exactly the number of characters given by the newLength argument. If the newLength argument is greater than or equal to the current length, sufficient null characters ('\u0000') are appended to the string buffer so that length becomes the newLength argument.

Page 7: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

public StringBuffer append(String str) Appends the string to this string buffer.

The characters of the String argument are appended, in order, to the contents of this string buffer, increasing the length of this string buffer by the length of the argument. If str is null, then the four characters "null" are appended to this string buffer.

Page 8: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

Actually there are many versions of append method, i.e., the parameter could be :boolean(“0”/”1”?), char, char[], int, long, double, float, StringBuffer, Object

Page 9: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

Used to implement the string concatenation operator +. x = "a" + 4 + "c"

is compiled to the equivalent of: x = new StringBuffer().append("a"). append(4).append("c") .toString()

Page 10: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

public StringBuffer delete(int start, int end) Removes the characters in a substring

of this StringBuffer. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the StringBuffer if no such character exists.

If start is equal to end, no changes are made.

Page 11: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

public StringBuffer replace(int start, int end, String str) Replaces the characters in a substring of this

StringBuffer with characters in the specified String. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the StringBuffer if no such character exists.

First the characters in the substring are removed and then the specified String is inserted at start.

The StringBuffer will be lengthened to accommodate the specified String if necessary.

Page 12: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

public StringBuffer insert(int offset, String str) Inserts the string into this string buffer. The

characters of the String argument are inserted, in order, into this string buffer at the indicated offset, moving up any characters originally above that position and increasing the length of this string buffer by the length of the argument. If str is null, then the four characters "null" are inserted into this string buffer.

Page 13: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

public StringBuffer reverse() The character sequence contained in

this string buffer is replaced by the reverse of the sequence.

Page 14: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

java.util.StringTokenizer

allows an application to break a string into tokens. A delimiters is a character that separates tokens the default delimiter set is " \t\n\

r\f"

Page 15: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

constructor

public StringTokenizer(String str) Constructs a string tokenizer for the

specified string.public StringTokenizer(String str, String delim) Constructs a string tokenizer for the

specified string. The characters in the delim argument are the delimiters.

Page 16: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

public StringTokenizer(String str, String delim, boolean returnDelims) Constructs a string tokenizer for the specified

string. All characters in the delim argument are the delimiters.

If the returnDelims flag is true, then the delimiter characters are also returned as tokens. Each delimiter is returned as a string of length one. If the flag is false, the delimiter characters are skipped and only serve as separators between tokens.

Page 17: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

public boolean hasMoreTokens() Tests if there are more tokens

available from this tokenizer's string.

If this method returns true, then a subsequent call to nextToken with no argument will successfully return a token.

Page 18: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

public String nextToken() Returns the next token from this

string tokenizer.

Page 19: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

public int countTokens() Returns the number of tokens

remaining in the string using the current delimiter set.

Page 20: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

java.util.Date

The class Date represents a specific instant in time, with millisecond precision.

Page 21: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

constructors

public Date() (extended ) Allocates a Date object and

initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.

Page 22: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

public Date(long date) Allocates a Date object and

initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

Page 23: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

public String toString() Converts this Date object to a

String of the form: dow mon dd hh:mm:ss zzz yyyy

Page 24: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

methods

public boolean after(Date when) Tests if this date is after the

specified date. public boolean before(Date when) Tests if this date is before the

specified date.

Page 25: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

public boolean equals(Object obj) Compares two dates for equality.

The result is true if and only if the argument is not null and is a Date object that represents the same point in time, to the millisecond, as this object.

Page 26: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

public long getTime() Returns the number of milliseconds

since January 1, 1970, 00:00:00 GMT represented by this Date object.

Thus, two Date objects are equal if and only if the getTime method returns the same long value for both.

Page 27: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

java.text.SimpleDateFormat

format (date -> text) parse (text -> date) dates in a locale-sensitive manner

Page 28: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

Date and Time Patterns

G Era designator Text AD y Year Year 1996; 96 M Month in year Month July; Jul; 07 w Week in year Number 27 W Week in month Number 2 D Day in year Number 89 d Day in month Number 10 F Day of week in month Number 2 E Day in week Text Tuesday; Tue a Am/pm marker Text PM H

Page 29: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

H Hour in day (0-23) Number 0 h Hour in am/pm (1-12) Number 12k Hour in day (1-24) Number 24 K Hour in am/pm (0-11) Number 0 m Minute in hour Number 30 s Second in minute Number 55 S Millisecond Number 978 z Time zone General time

zone Pacific Standard Time; PST; GMT-08:00

*Z Time zone RFC 822 time zone -0800

Page 30: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

"yyyy.MM.dd G 'at' HH:mm:ss z" 2001.07.04 AD at 12:08:56 PDT

"EEE, MMM d, ''yy" Wed, Jul 4, '01

"h:mm a" 12:08 PM

"hh 'o''clock' a, zzzz" 12 o'clock PM, Pacific Daylight Time

Page 31: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

"K:mm a, z" 0:08 PM, PDT

"yyyyy.MMMMM.dd GGG hh:mm aaa"

02001.July.04 AD 12:08 PM

"EEE, d MMM yyyy HH:mm:ss Z" Wed, 4 Jul 2001 12:08:56 -0700

"yyMMddHHmmssZ" 010704120856-0700

Page 32: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

public SimpleDateFormat() Constructs a SimpleDateFormat using the

default pattern and the default date format symbols for the default locale

public SimpleDateFormat(String pattern) Constructs a SimpleDateFormat using the

given pattern and the default date format symbols for the default locale.

Page 33: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

public final String format(Date date) Formats a Date into a date/time

string.

Page 34: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

public Date parse(String text, ParsePosition pos) Parses text starting at the index given by pos. from a string to produce a Date. If parsing succeeds, then the index of pos is updated

to the index after the last character used (parsing does not necessarily use all characters up to the end of the string), and the parsed date is returned.

The updated pos can be used to indicate the starting point for the next call to this method.

If an error occurs, then the index of pos is not changed, the error index of pos is set to the index of the character where the error occurred, and null is returned.

Page 35: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

SimpleDateFormat sdf = new SimpleDateFormat ("K:mm a, z" ); myString = sdf.format(myDate);

Page 36: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

java.util.Calendar

for converting between a Date object and a set of integer fields such as YEAR, MONTH, DAY, HOUR, and so on.

Page 37: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

Field

public static final int JANUARY -DECEMBER

public static final int SUNDAY –SATURDAY

Page 38: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

public static final int DAY_OF_YEAR(takes values 1-365) , public static final int DAY_OF_MONTH(takes values 1-31) public static final int DAY_OF_WEEK (takes values SUNDAY- SATURDAY )

Page 39: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

public static final int YEAR public static final int MONTHpublic static final int HOURpublic static final int HOUR_OF_DAY

Page 40: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

public static final int MINUTEpublic static final int SECONDpublic static final int MILLISECOND

Page 41: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

costructors

protected Calendar() Constructs a Calendar with the

default time zone and locale. protected Calendar(TimeZone zone, Locale aLocale) Constructs a calendar with the

specified time zone and locale.

Page 42: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

methods

public static Calendar getInstance() Gets a calendar based on the

current time using the default time zone and locale.

Page 43: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

public final void set(int year, int month, int date) Sets the values for the fields

year, month, and date. Previous values of other fields are retained. If this is not desired, call clear first.

Page 44: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

public final void set(int year, int month, int date, int hour, int minute, int second) Sets the values for the fields

year, month, date, hour, minute, and second. Previous values of other fields are retained. If this is not desired, call clear first.

Page 45: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

public final void clear() Clears the values of all the time

fields. public final void clear(int field) Clears the value in the given time

field. Q: what kind of field should be put

in?

Page 46: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

public final void setTime(Date date) Sets this Calendar's current time

with the given Date.public final Date getTime() Gets this Calendar's current

time.

Page 47: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

Reviewing Questions

What is the difference between length( ) and capacity( ) method of StringBuffer?Why we use sb.length() to get the length of a StringBuffer instance sb while using s.length for a String instance s?When should we use append(..) and insert(.. ) method of StringBuffer ?

Page 48: Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.

What is the use of returnDelims In the following method ?

pulbic StringTokenizer(String str, String delim, boolean returnDelims)What should be noted when using the Calendar class set(……) method? Why?