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

Slides:



Advertisements
Similar presentations
1 StringBuffer & StringTokenizer Classes Chapter 5 - Other String Classes.
Advertisements

Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
1 String Buffer & String Tokenizer Overview l Overview of String Buffer class and Methods l Overview of String Tokenizer class and methods l Preview: Notions.
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
23-Jun-15 Strings, Etc. Part I: String s. 2 About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects,
StringBuffer class  Alternative to String class  Can be used wherever a string is used  More flexible than String  Has three constructors and more.
Fundamental Programming Structures in Java: Strings.
1 StringTokenizer and StringBuffer classes Overview l StringTokenizer class l Some StringTokenizer methods l StringTokenizer examples l StringBuffer class.
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
28-Jun-15 String and StringBuilder Part I: String.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
Time & Date Representation in Java. Date class An object of type Date represents an instance in time Part of java.util.* (requires import statement) A.
Strings Edward J. Biebel. Strings Strings are fundamental part of all computing languages. At the basic level, they are just a data structure that can.
Server-Side Scripting with JSP (2) ISYS 350. Java Array Examples of declaring an array: – int[] anArray = new int[10]; 10 elements index from 0 to 9 –
Chapter 14 Internationalization F Processing Date and Time –Locale –Date –TimeZone –Calendar and GregorianCalendar –DateFormat and SimpleDateFormat F Formatting.
Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Textbook: Data Structures and the Java Collections Framework 3rd Edition by William Collins William Collins.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;
Operations on Strings. 8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation.
Chapter 14 Internationalization F Processing Date and Time –Locale –Date –TimeZone –Calendar and GregorianCalendar –DateFormat and SimpleDateFormat F Formatting.
Tokenizers 29-Nov-15. Tokens A tokenizer is a program that extracts tokens from an input stream A token is a “word” or a significant punctuation mark.
String Handling StringBuffer class character class StringTokenizer class.
Server-Side Scripting with JSP (2) ISYS 350. Java Array Examples of declaring an array: – int[] anArray = new int[10]; 10 elements index from 0 to 9 –
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
Geoff Holmes Date Math Weighted Distr Strings String methods Tokenizers System Examples Utility Classes (Chapter 17) import java.util.*;
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
String String Builder. System.String string is the alias for System.String A string is an object of class string in the System namespace representing.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology- George Koutsogiannakis 1.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
Java Util Package Prepared by, S.Amudha AP/SWE. Calendar 1.The abstract Calendar class provides a set of methods that allows you to convert a time in.
17-Feb-16 String and StringBuilder Part I: String.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Utility classes 1.Vector class 2.Random class 3.Date class 4.Scanner.
Working with Date ISYS 350.
Chapter 14 Internationalization
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Java String and Date ISYS 350.
EKT 472: Object Oriented Programming
String String Builder.
Java Date ISYS 350.
Formatting Output.
String and StringBuilder
Chapter 7: Strings and Characters
MSIS 655 Advanced Business Applications Programming
String and StringBuilder
String and StringBuilder
Java – String Handling.
Java Date ISYS 350.
Lecture 07 String Jaeki Song.
OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS
Tokenizers 25-Feb-19.
String and StringBuilder
Tokenizers 26-Apr-19.
Tokenizers 3-May-19.
Object Oriented Programming
Java Date ISYS 350.
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
In Java, strings are objects that belong to class java.lang.String .
Java Date ISYS 350.
Presentation transcript:

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?

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

StringBufferStringBuffer(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.String

Methods public int length() Returns the length (character count) of this string buffer public int capacity() Returns the current capacity of the String buffer. The capacity is the amount of storage available.

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.

public StringBuffer append(String str)StringBufferString 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.

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

Used to implement the string concatenation operator +. x = "a" "c" is compiled to the equivalent of: x = new StringBuffer().append("a"). append(4).append("c").toString()

public StringBuffer delete(int start, int end)StringBuffer 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.

public StringBuffer replace(int start, int end, String str)StringBuffer String 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.

public StringBuffer insert(int offset, String str)StringBuffer String 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.

public StringBuffer reverse()StringBuffer The character sequence contained in this string buffer is replaced by the reverse of the sequence.

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"

constructor public StringTokenizer(String str)String Constructs a string tokenizer for the specified string. public StringTokenizer(String str, String delim)String Constructs a string tokenizer for the specified string. The characters in the delim argument are the delimiters.

public StringTokenizer(String str, String delim, boolean returnDelims)String 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.

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.

public String nextToken()String Returns the next token from this string tokenizer.

public int countTokens() Returns the number of tokens remaining in the string using the current delimiter set.

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

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.

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.

public String toString()String Converts this Date object to a String of the form: dow mon dd hh:mm:ss zzz yyyy

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

public boolean equals(Object obj)Object 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.

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.

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

Date and Time Patterns G Era designator Text ADText y Year Year 1996; 96Year M Month in year Month July; Jul; 07Month w Week in year Number 27Number W Week in month Number 2Number D Day in year Number 89Number d Day in month Number 10Number F Day of week in month Number 2Number E Day in week Text Tuesday; TueText a Am/pm marker Text PM HText

H Hour in day (0-23) Number 0Number h Hour in am/pm (1-12) Number 12Number k Hour in day (1-24) Number 24Number K Hour in am/pm (0-11) Number 0Number m Minute in hour Number 30Number s Second in minute Number 55Number S Millisecond Number 978Number z Time zone General time zone Pacific Standard Time; PST; GMT-08:00General time zone *Z Time zone RFC 822 time zoneRFC 822 time zone -0800

 "yyyy.MM.dd G 'at' HH:mm:ss z" 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

 "K:mm a, z" 0:08 PM, PDT  "yyyyy.MMMMM.dd GGG hh:mm aaa" July.04 AD 12:08 PM  "EEE, d MMM yyyy HH:mm:ss Z" Wed, 4 Jul :08:  "yyMMddHHmmssZ"

public SimpleDateFormat() Constructs a SimpleDateFormat using the default pattern and the default date format symbols for the default locale public SimpleDateFormat(String pattern)String Constructs a SimpleDateFormat using the given pattern and the default date format symbols for the default locale.

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

public Date parse(String text, ParsePosition pos)DateString ParsePosition 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.

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

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

Field public static final int JANUARY - DECEMBERJANUARY DECEMBER public static final int SUNDAY – SATURDAYSUNDAY SATURDAY

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

public static final int YEAR YEAR public static final int MONTHMONTH public static final int HOUR public static final int HOUR_OF_DAY

public static final int MINUTE public static final int SECOND public static final int MILLISECOND

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

methods public static Calendar getInstance()Calendar Gets a calendar based on the current time using the default time zone and locale.

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.

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.

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?

public final void setTime(Date date)Date Sets this Calendar's current time with the given Date. public final Date getTime()Date Gets this Calendar's current time.

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 ?

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