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.

Slides:



Advertisements
Similar presentations
Lecture 6 Strings and more I/O COMP1681 / SE15 Introduction to Programming.
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Lecture 5 Strings and more I/O COMP1681 / SE15 Introduction to Programming.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
The Java String Type CSC 1401: Introduction to Programming with Java Week 7 – Lecture 1 Wanda M. Kunkle.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String.
1 Character Strings and Variables Character Strings Variables, Initialization, and Assignment Reading for this class: L&L,
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
String Escape Sequences
2.2 Information on Program Appearance and Printing.
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.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Primitive Types Java offers a number of primitive types eg.) int, short, long double, float char A variable which is declared as a primitive type stores.
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
Program Statements Primitive Data Types and Strings.
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,
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
Strings, output, quotes and comments
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Chapter 2 Variables.
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
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.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Introduction to Strings CSIS 1595: Fundamentals of Programming and Problem Solving 1.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Chapter 2 print / println String Literals Escape Characters Variables / data types.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
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.
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.
Chapter 2 1.What is the difference between print / println 2.What are String Literals 3.What are the Escape Characters for backslash, double quotataions,
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.
17-Mar-16 Characters and Strings. 2 Characters In Java, a char is a primitive type that can hold one single character A character can be: A letter or.
© 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 August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
CS 106A, Lecture 4 Introduction to Java
Chapter 2 Variables.
Crash course in the Java Programming Language
String class.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
OUTPUT STATEMENTS GC 201.
Escape Sequences What if we wanted to print the quote character?
Type Conversion, Constants, and the String Object
Examples of Primitive Values
Chapter 2 Part 2 Edited by JJ Shepherd
CMSC 202 Java Primer 2.
Chapter 2 Create a Chapter 2 Workspace Create a Project called Notes
String methods 26-Apr-19.
Primitive Types and Expressions
Chapter 2 Variables.
Strings in Java Strings in Java are also a reference type.
Presentation transcript:

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 store and process strings of characters.  The quoted string “Enter an integer.” is an example of a string.

String Constants and Variables (cont’d)  Variables can be declared to be of type String. E.g., String greeting;  Variables of type String can be assigned values using an assignment statement. E.g., greeting = “Hello!”;  Declarations and assignments can be combined. E.g., String greeting = “Hello!”;

Concatenation of Strings  Two strings can be connected using the + operator  Connecting two strings together to obtain a larger string is called concatenation.  Example: String greeting = “Hello”; String sentence; sentence = greeting + “ my friend”; System.out.println(sentence);  The above example outputs: Hello my friend

Concatenation of Strings (cont’d)  Any number of String objects can be concatenated using the + operator.  In fact, String objects can be concatenated to objects of any type using the + operator.  Example: String solution = “The answer is “; solution = solution + 42; System.out.println(sentence);  The above example outputs: The answer is 42;

Classes  In Java, a class is a type whose values are objects.  Objects are entities that store data and can take actions.  The actions that an object can take are called methods.  Most of the methods for the class String return, or produce, some value.

Classes (cont’d)  Objects of the class String store data consisting of strings of characters, such as “Hello”.  A method, like length(), returns a value, which in this case is the number of characters in the String object.  For example, “Hello”.length() returns 5, which can be stored in an int variable as follows: int n = “Hello”.length();

Classes (cont’d)  A method is called into action by writing a name for the object, followed by a dot, followed by the method name and ending with parentheses.  The act of calling a method into action is said to be invoking or calling the method  The object before the dot is known as the calling object.

Classes (cont’d)  Although a method can be called with a constant object, it is more common to use a variable as the calling object.  Example: String greeting = “Hello”; int n = greeting.length();  Information for the method invocation is given in the parentheses. There is nothing inside the parentheses in this case because the length method does not require additional information.

Classes (cont’d)  The information inside the parentheses following the method’s name is called the argument (or arguments).  Class types in Java are different from primitive types in that they have methods.  Primitive types are spelled using only lower case letters, but, by convention, class types are spelled with their first letter capitalized, as in String.

String Methods  length()  equal(Other_String)  equalsIgnoreCase(Other_String)  toLowerCase()  toUpperCase()  trim()  charAt(Position)

String Methods (cont’d)  substring(Start)  substring(Start,End)  indexOf(A_String)  indexOf(A_String,Start)  lastIndexOf(A_String)  compareTo(A_String)

String Methods (cont’d)  Many of the methods for the class String depend on counting positions in the string.  Positions are counted starting with 0 not with 1.  Example: In the string “Hi Mom”, ‘H’ is in position 0, ‘i’ is in position 1, the blank character is in position 2, etc.  A position is usually referred to as an index.

Escape Characters  In order to have a quotation mark ( “) appear in a string we must precede the quotation mark with a backslash ( \ ).  For example the statement: System.out.println ( “The word \”Java\” names a language, not just a drink!”); will print The word “Java” names a language, not just a drink!

Escape Characters (cont’d)  The combination, \”, is treated as a single character, not two characters.  It is called an escape sequence or an escape character.  Since the backslash is used for escape sequences, including a backslash in a string requires placing a double backslash, \\, where a single backslash is wanted.

Escape Characters (cont’d)  Similarly, to define a single quote mark ( ‘ ) as a character, an escape sequence is needed.  For example, char singleQuote = ‘\’’;  However, there is no problem using single quotes within strings.

The Java Escape Characters \” Double quote \’ Single quote \\ Backslash \n New line. Go to the beginning of the next line. \r Carriage return. Go to the begining of the current line. \t Tab. Add whitespace up to the next tab stop.