1 The String Class Every character string is an object in Java, defined by the String class Every string literal, delimited by double quotation marks,

Slides:



Advertisements
Similar presentations
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
Advertisements

Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
References, Aliases, Garbage Collection and Packages Packages and Importing Classes Reading for this Lecture: L&L, Familiarize yourself with.
1 Character Strings and Variables Character Strings Variables, Initialization, and Assignment Reading for this class: L&L,
Chapter 2: Objects and Primitive Data Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Chapter 2: Objects and Primitive Data Classes and Objects String, Random, Math, NumberFormat, DecimalFormat and Wrapper Classes.
Outline Questions / Review Predefined Objects Variables Primitive Data Arithmetic Expressions Interactive Programs Decision Making Assignments.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer.
Program Statements Primitive Data Types and Strings.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
© 2006 Pearson Education Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer.
Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara.
1 Objects and Primitive Data  Now we can explore some more fundamental programming concepts  Chapter 2 focuses on: predefined objects primitive data.
_Introduction elsaddik CSI 1102 Introduction to Software Design Prof. Dr. Abdulmotaleb El Saddik Multimedia.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
CSC 1051 M.A. Papalaskari, Villanova University CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
© 2004 Pearson Addison-Wesley. All rights reserved September 7, 2007 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
1 Lecture 2 b declaration and use of variables b expressions and operator precedence b introduction to objects b class libraries b flow of control b decision-making.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 3.
CSE 1201 Object Oriented Programming Using Classes and Objects.
1 Chap. 3 Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L, 3.1 – 3.6.
1 Wrapper Classes  Sometimes we want to use a primitive type as an object, so there are wrapper classes that will help us.  In particular, we need to.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Collaboration Policy Programming Tests are individual work. You may not work with others in the class.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
3-1 Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as a type to declare an object reference.
Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara.
Programming in Java (COP 2250) Lecture 7 Chengyong Yang Fall, 2005.
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
CSCI 1100/1202 January 14, Abstraction An abstraction hides (or ignores) the right details at the right time An object is abstract in that we don't.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Using Classes and Objects We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: object creation and.
© 2006 Pearson Education Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Chapter 2: Objects and Primitive Data Lian Yu Department of Computer Science and Engineering Arizona State University Tempe, AZ
1 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used as a type to declare an object reference.
© 2004 Pearson Addison-Wesley. All rights reserved January 27, 2006 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Variables in Java A variable holds either
Formatting Output & Enumerated Types & Wrapper Classes
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Chapter 2: Objects and Primitive Data
Objects and Classes A class An object (the concept) (the realization)
Classes, Libraries & Packages
Chapter 2: Objects and Primitive Data
Chapter 2: Objects and Primitive Data
Objects and Primitive Data
Chapter 2: Objects and Primitive Data
Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference.
Chapter 2: Objects and Primitive Data
Packages & Random and Math Classes
Chapter 2: Objects and Primitive Data
Chapter 2: Objects and Primitive Data
Presentation transcript:

1 The String Class Every character string is an object in Java, defined by the String class Every string literal, delimited by double quotation marks, represents a String object The string concatenation operator (+) is used to append one string to the end of another It can also be used to append a number to a string A string literal cannot be broken across two lines in a program See Facts.java (page 56)Facts.java

2 String Concatenation The plus operator (+) is also used for arithmetic addition The function that the + operator performs depends on the type of the information on which it operates If both operands are strings, or if one is a string and one is a number, it performs string concatenation If both operands are numeric, it adds them The + operator is evaluated left to right Parentheses can be used to force the operation order See Addition.java (page 58)Addition.java

3 Escape Sequences What if we wanted to print a double quote character? The following line would confuse the compiler because it would interpret the second quote as the end of the string System.out.println ("I said "Hello" to you."); An escape sequence is a series of characters that represents a special character An escape sequence begins with a backslash character ( \ ), which indicates that the character(s) that follow should be treated in a special way System.out.println ("I said \"Hello\" to you.");

4 Escape Sequences Some Java escape sequences: See Roses.java (page 59)Roses.java Escape Sequence \b \t \n \r \" \' \\ Meaning backspace tab newline carriage return double quote single quote backslash

Creating Objects A variable either holds a primitive type, or it holds a reference to an object A class name can be used as a type to declare an object reference variable String title; No object has been created with this declaration An object reference variable holds the address of an object The object itself must be created separately

Creating Objects We use the new operator to create an object title = new String ("Java Software Solutions"); This calls the String constructor, which is a special method that sets up the object Creating an object is called instantiation An object is an instance of a particular class

Creating Objects Because strings are so common, we don't have to use the new operator to create a String object title = "Java Software Solutions"; This is special syntax that only works for strings Once an object has been instantiated, we can use the dot operator to invoke its methods title.length()

String Methods The String class has several methods that are useful for manipulating strings Many of the methods return a value, such as an integer or a new String object See the list of String methods on page 75 and in Appendix M See StringMutation.java (page 77)StringMutation.java

Class Libraries A class library is a collection of classes that we can use when developing programs There is a Java standard class library that is part of any Java development environment These classes are not part of the Java language per se, but we rely on them heavily The System class and the String class are part of the Java standard class library Other class libraries can be obtained through third party vendors, or you can create them yourself

Packages The classes of the Java standard class library are organized into packages Some of the packages in the standard class library are: Package java.lang java.applet java.awt javax.swing java.net java.util Purpose General support Creating applets for the web Graphics and graphical user interfaces Additional graphics capabilities and components Network communication Utilities

The import Declaration When you want to use a class from a package, you could use its fully qualified name java.util.Random Or you can import the class, then just use the class name import java.util.Random; To import all classes in a particular package, you can use the * wildcard character import java.util.*;

The import Declaration All classes of the java.lang package are automatically imported into all programs That's why we didn't have to explicitly import the System or String classes in earlier programs The Random class is part of the java.util package It provides methods that generate pseudo-random numbers We often have to scale and shift a number into an appropriate range for a particular purpose See RandomNumbers.java (page 82)RandomNumbers.java

Class Methods Some methods can be invoked through the class name, instead of through an object of the class These methods are called class methods or static methods The Math class contains many static methods, providing various mathematical functions, such as absolute value, trigonometry functions, square root, etc. temp = Math.cos(90) + Math.sqrt(delta);

The Keyboard Class The Keyboard class is NOT part of the Java standard class library It is provided by the authors of the textbook to make reading input from the keyboard easy Details of the Keyboard class are explored in Chapter 8 For now we will simply make use of it The Keyboard class is part of a package called cs1, and contains several static methods for reading particular types of data See Echo.java (page 86)Echo.java See Quadratic.java (page 87)Quadratic.java