Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Chapter 1 Getting Started Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
 Finishing Chapter 1  This week : Chapter 2  Getting our home environment configured  Working on Assignment 1  See the website for more details! 
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
CS102--Object Oriented Programming
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
Chapter 1 Getting Started Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.2 Expressions and Assignment Statement.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
The Class String. String Constants and Variables  There is no primitive type for strings in Java.  There is a class called String that can be used to.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
String Escape Sequences
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
CMSC 202 Java Primer. Version 9/09Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
The Java Programming Language
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Chapter 2: Using Data.
Comp 248 Introduction to Programming Chapter 1 - Getting Started Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started.
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started.
Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Chapter 3 Boolean Expressions Section 3.2 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 1 Getting Started Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Chapter 1 Getting Started Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CMSC 202 Java Primer. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
CMSC 202 Java Primer 1. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Java-02 Basic Concepts Review concepts and examine how java handles them.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 1 Getting Started
Chapter 6 JavaScript: Introduction to Scripting
Chapter 1 Getting Started
String class.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Program Style Console Input and Output
Introduction to C++ Programming
Chapter 2 Part 2 Edited by JJ Shepherd
CMSC 202 Java Primer 2.
elementary programming
Anatomy of a Java Program
Chapter 1 Getting Started
The Data Element.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Primitive Types and Expressions
The Data Element.
Instructor: Alexander Stoytchev
Review of Java Fundamentals
Presentation transcript:

Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String

© 2006 Pearson Addison-Wesley. All rights reserved1-2 The Class String There is no primitive type for strings in Java The class String is a predefined class in Java that is used to store and process strings Objects of type String are made up of strings of characters that are written within double quotes –Any quoted string is a constant of type String "Live long and prosper." A variable of type String can be given the value of a String object String blessing = "Live long and prosper.";

© 2006 Pearson Addison-Wesley. All rights reserved1-3 Concatenation of Strings Concatenation: Using the + operator on two strings in order to connect them to form one longer string –If greeting is equal to "Hello ", and javaClass is equal to "class", then greeting + javaClass is equal to "Hello class" Any number of strings can be concatenated together When a string is combined with almost any other type of item, the result is a string –"The answer is " + 42 evaluates to "The answer is 42"

© 2006 Pearson Addison-Wesley. All rights reserved1-4 Classes, Objects, and Methods A class is the name for a type whose values are objects Objects are entities that store data and take actions –Objects of the String class store data consisting of strings of characters The actions that an object can take are called methods –Methods can return a value of a single type and/or perform an action –All objects within a class have the same methods, but each can have different data values

© 2006 Pearson Addison-Wesley. All rights reserved1-5 Classes, Objects, and Methods Invoking or calling a method: a method is called into action by writing the name of the calling object, followed by a dot, followed by the method name, followed by parentheses –This is sometimes referred to as sending a message to the object –The parentheses contain the information (if any) needed by the method –This information is called an argument (or arguments)

© 2006 Pearson Addison-Wesley. All rights reserved1-6 String Methods The String class contains many useful methods for string-processing applications –A String method is called by writing a String object, a dot, the name of the method, and a pair of parentheses to enclose any arguments –If a String method returns a value, then it can be placed anywhere that a value of its type can be used String greeting = "Hello"; int count = greeting.length(); System.out.println("Length is " + greeting.length()); –Always count from zero when referring to the position or index of a character in a string

© 2006 Pearson Addison-Wesley. All rights reserved1-7 Some Methods in the Class String (Part 1 of 8)

© 2006 Pearson Addison-Wesley. All rights reserved1-8 Some Methods in the Class String (Part 2 of 8)

© 2006 Pearson Addison-Wesley. All rights reserved1-9 Some Methods in the Class String (Part 3 of 8)

© 2006 Pearson Addison-Wesley. All rights reserved1-10 Some Methods in the Class String (Part 4 of 8)

© 2006 Pearson Addison-Wesley. All rights reserved1-11 Some Methods in the Class String (Part 5 of 8)

© 2006 Pearson Addison-Wesley. All rights reserved1-12 Some Methods in the Class String (Part 6 of 8)

© 2006 Pearson Addison-Wesley. All rights reserved1-13 Some Methods in the Class String (Part 7 of 8)

© 2006 Pearson Addison-Wesley. All rights reserved1-14 Some Methods in the Class String (Part 8 of 8)

© 2006 Pearson Addison-Wesley. All rights reserved1-15 String Indexes

© 2006 Pearson Addison-Wesley. All rights reserved1-16 Escape Sequences A backslash ( \ ) immediately preceding a character (i.e., without any space) denotes an escape sequence or an escape character –The character following the backslash does not have its usual meaning –Although it is formed using two symbols, it is regarded as a single character

© 2006 Pearson Addison-Wesley. All rights reserved1-17 Escape Sequences

© 2006 Pearson Addison-Wesley. All rights reserved1-18 String Processing A String object in Java is considered to be immutable, i.e., the characters it contains cannot be changed There is another class in Java called StringBuffer that has methods for editing its string objects However, it is possible to change the value of a String variable by using an assignment statement String name = "Soprano"; name = "Anthony " + name;

© 2006 Pearson Addison-Wesley. All rights reserved1-19 Character Sets ASCII: A character set used by many programming languages that contains all the characters normally used on an English-language keyboard, plus a few special characters –Each character is represented by a particular number Unicode: A character set used by the Java language that includes all the ASCII characters plus many of the characters used in languages with a different alphabet from English

© 2006 Pearson Addison-Wesley. All rights reserved1-20 Naming Constants Instead of using "anonymous" numbers in a program, always declare them as named constants, and use their name instead public static final int INCHES_PER_FOOT = 12; public static final double RATE = 0.14; –This prevents a value from being changed inadvertently –It has the added advantage that when a value must be modified, it need only be changed in one place –Note the naming convention for constants: Use all uppercase letters, and designate word boundaries with an underscore character

© 2006 Pearson Addison-Wesley. All rights reserved1-21 Comments A line comment begins with the symbols //, and causes the compiler to ignore the remainder of the line –This type of comment is used for the code writer or for a programmer who modifies the code A block comment begins with the symbol pair /*, and ends with the symbol pair */ –The compiler ignores anything in between –This type of comment can span several lines –This type of comment provides documentation for the users of the program

© 2006 Pearson Addison-Wesley. All rights reserved1-22 Program Documentation Java comes with a program called javadoc that will automatically extract documentation from block comments in the classes you define –As long as their opening has an extra asterisk ( /** ) Ultimately, a well written program is self- documenting –Its structure is made clear by the choice of identifier names and the indenting pattern –When one structure is nested inside another, the inside structure is indented one more level

© 2006 Pearson Addison-Wesley. All rights reserved1-23 Comments and a Named Constant