1 Builtin-in Classes in java.lang and java.util 4 Wrapper Classes –Boolean, Character, Byte, Short, Integer –Long, Float, Double 4 String & StringBuffer.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

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.
1 Working with String Duo Wei CS110A_ Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Java Programming Strings Chapter 7.
Chapter 10 Review. Write a method that returns true is s1 and s2 end with the same character; otherwise return false. Sample Answer: public boolean lastChar(String.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Strings and Arrays The objectives of this chapter are:  To discuss the String class and some of its methods  To discuss the creation and use of Arrays.
Chapter 3 - Java Programming With Supplied Classes1 Chapter 3 Java Programming With Supplied Classes.
String class  Construct a string  String str = new String(“welcome”);  Char[] charr = {‘G’, ‘o’, ‘o’, ‘d’};  String mes = new String(charr);  A full.
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 ,
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
Fundamental Programming Structures in Java: Strings.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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 ,
Static Class Members Wrapper Classes Autoboxing Unboxing.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic.
The java.lang Package chapter 8 Java Certification Study Group February 2, 1998 Seth Ladd.
String Processing Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class? zDo you have questions.
Chapter 7: Characters, Strings, and the StringBuilder.
CHAPTER 9 Text Processing and More about Wrapper Classes Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
String Handling StringBuffer class character class StringTokenizer class.
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.
Package and Some Classes Declaration of Package Usage of Package Package of Java Language.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes.
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 String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 2- Using Java Built-in Classes Topic 2.4 Using Java Built-in Classes Produced by.
Wrapper Classes  Java offers a convenient way to incorporate, or wrap, a primitive data type into an object, for example, wrapping int into the class.
C OMMON M ISTAKES CSC Java Program Structure  String Methods.
Wrapper Classes Use wrapper objects in Collections when you can’t use primitive types Primitive TypeWrapper Class byteByte shortShort intInteger longLong.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
String & Exception. Lesson plan Class & Object String Exercise for midterm.
Chapter 10: Text Processing and More about Wrapper Classes
String and StringBuffer classes
Strings, Characters and Regular Expressions
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Strings.
University of Central Florida COP 3330 Object Oriented Programming
String and String Buffers
Chapter 10 Thinking in Objects
String Handling in JAVA
Modern Programming Tools And Techniques-I Lecture 11: String Handling
String and StringBuilder
Chapter 7: Strings and Characters
MSIS 655 Advanced Business Applications Programming
Unit-2 Objects and Classes
String and StringBuilder
Wrapper Classes The java.lang package contains wrapper classes that correspond to each primitive type: Primitive Type Wrapper Class byte Byte short Short.
String and StringBuilder
Java – String Handling.
Lecture 07 String Jaeki Song.
String and StringBuilder
CS2011 Introduction to Programming I Strings
Chapter 10 Thinking in Objects Part 2
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
In Java, strings are objects that belong to class java.lang.String .
Unit-2 Objects and Classes
Presentation transcript:

1 Builtin-in Classes in java.lang and java.util 4 Wrapper Classes –Boolean, Character, Byte, Short, Integer –Long, Float, Double 4 String & StringBuffer 4 Vector 4 LinkedList 4 Hashtable

2 Wrapper Classes Wrapper classPrimitive Booleanboolean Characterchar Bytebyte Shortshort Integerint Longlong Floatfloat Doubledouble

3 byte byteValue()byteValue double doubleValue()doubleValue float floatValue()floatValue int intValue()intValue longlongValue()longValue short shortValue()shortValue String toString()toString The Integer Class 4 Convert to different types 4 Parse integer strings static Integer decode(String s)decode static int parseInt(String s)parseInt static int parseInt(String s, int radix)parseInt static Integer valueOf(String s)valueOf static Integer valueOf(String s, int radix)valueOf static StringtoBinaryString(int value)toBinaryString static StringtoHexString(int value)toHexString static String toOctalString(int value)toOctalString static StringtoString(int value)toString static StringtoString(int value, int radix)toString

4 Using the Integer Class public class TestStack { public static void main(String[] args){ Stack s = new Stack(); int i; s.push(new Integer(1)); s.push(new Integer(2)); i = ((Integer)s.pop()).intValue(); System.out.println(s.pop()); }

5 The Character Class char charValue()charValue static int digit(char c, int radix)digit static intgetNumericValue(char c)getNumericValue static boolean isDigit(char c)isDigit static boolean isLetter(char c)isLetter static boolean isLetterOrDigit(char c)isLetterOrDigit static boolean isLowerCase(char c)isLowerCase static boolean isSpaceChar(char c)isSpaceChar static boolean isUpperCase(char c)isUpperCase static char toLowerCase(char c)toLowerCase String toString()toString static char toUpperCase(char c)toUpperCase

6 The String Class  Defined in java.lang.  All string literals are immutable instances of String. –Important: Identical literals have the same object reference. 4 Useful methods include: –charAt, equals, length, startsWith, indexOf, toLowerCase, etc.

7 String Comparisons  Several ways to compare two String objects: –compareTo, equals, equalsIgnoreCase.  Never use == to test for content equality between strings: –This only gives reference equality.

8 The Immutability of Strings  We often construct new strings out of existing strings, e.g., using +. 4 Because String objects are immutable, we always obtain a new instance: –String noun = "dog"; –noun += "s";  Similarly with concat, etc.

9 Parsing Strings public int countSpaces(String s){ int count = 0; // Look for the first. int index = s.indexOf(' '); // indexOf returns -1 on failure. while(index >= 0){ // We found one. count += 1; // Look for the next just after the last one. index = s.indexOf(' ',index+1); } return count; }

10 Strings from Primitive Values 4 Direct assignment is illegal: –String s = 32; // Forbidden. 4 Implicit type conversion is common: –String s = ""+num;  Via the static valueOf method: –String s = String.valueOf(num);

11 The StringBuffer Class 4 String-like objects that are mutable.  Used for building a String out of multiple pieces. 4 Size is dynamic and unlimited.

12 java.lang.StringBuffer StringBuffer append(xxx arg)StringBuffer where xxx can be any primitive type or objet type StringBuffer insert(int offset, xxx obj)StringBuffer int capacity() char charAt(int index) int length() void setCharAt(int index,char ch) StringBufferStringBuffer reverse()

13 Formatting an Integer public String formatInt(int number,int width){ // Create space for the full width. StringBuffer formattedInt = new StringBuffer(width); // Append a string version of the number. formattedInt.append(number); // How many extra spaces are required? int spaces = width-formattedInt.length(); for(int i = 0; i < spaces; i++){ formattedInt.insert(0," "); } return formattedInt.toString(); }