1 Character Strings and Variables Character Strings Variables, Initialization, and Assignment Reading for this class: L&L, 2.1-2.2.

Slides:



Advertisements
Similar presentations
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Advertisements

Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Basic Syntax: Data & Expressions
2 Data and Expressions Software Solutions Lewis & Loftus java
Chapter 2 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
ECE122 L2: Program Development February 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 2 Program Development.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
© 2004 Pearson Addison-Wesley. All rights reserved1-1 Intermediate Java Programming Lory Al Moakar.
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 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Outline Questions / Review Predefined Objects Variables Primitive Data Arithmetic Expressions Interactive Programs Decision Making Assignments.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Program Statements Primitive Data Types and Strings.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyCopyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
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,
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 2 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Chapter 2 Data and Expressions Part One. © 2004 Pearson Addison-Wesley. All rights reserved2-2/29 Data and Expressions Let's explore some other fundamental.
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.
Chapter 2 Data and Expressions Java Software Solutions Foundations of Program Design 1.
Chapter 2: Data and Expressions String and String Concatenation Escape Sequences Variables Primitive Date Types Expressions Interactive Programs.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Data and Expressions Let's explore some other fundamental programming concepts Chapter 2 focuses on: –character strings –primitive data –the declaration.
Chapter 2 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
Chapter 2 print / println String Literals Escape Characters Variables / data types.
2-1 Character Strings A string of characters can be represented as a string literal by putting double quotes around the text: Examples: "This is a string.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
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 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights.
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.
Chapter 2 Data and Expressions 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights.
Chapter 2: Objects and Primitive Data Lian Yu Department of Computer Science and Engineering Arizona State University Tempe, AZ
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
© 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.
Object Oriented Programming Idea Computer program may be seen as comprising a collection of objects Object Fundamental entity in a JAVA program Used to.
Chapter 02 Data and Expressions.
Web Programming Chapter 1 : Introduction to Java Programming
Intermediate Java Programming
Chapter 2 Data and Expressions
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
CSC 1051 – Data Structures and Algorithms I
Multiple variables can be created in one declaration
Chapter 2 Data and Expressions
Escape Sequences What if we wanted to print the quote character?
Data and Expressions Part One
Chapter 2 Data and Expressions.
Chapter 2 Create a Chapter 2 Workspace Create a Project called Notes
Chapter 2 Data and Expressions.
Chapter 2: Objects and Primitive Data
CSC 1051 – Data Structures and Algorithms I
Instructor: Alexander Stoytchev
Java Programming Presented by Dr. K. SATISH KUMAR,
Chapter 2: Objects and Primitive Data
Presentation transcript:

1 Character Strings and Variables Character Strings Variables, Initialization, and Assignment Reading for this class: L&L,

2 Character Strings A string of characters can be represented as a string literal by putting double quotes around the text: Examples: "This is a string literal." "123 Main Street" "X" Every character string is an object in Java, defined by the String class Every string literal represents a String object

3 The println Method In the Lincoln program from Chapter 1, we invoked the println method to print a character string The System.out object represents a destination (the monitor screen) to which we can send output System.out.println ("Whatever you are, be a good one."); object method name information provided to the method (parameters)

4 The print Method The System.out object provides another method The print method is similar to the println method, except that it does not start the next line Therefore any parameter passed in a call to the print method will appear on the same line See Countdown.java (page 63)Countdown.java System.out.print (“Three… ”); System.out.print (“Two… ”); Prints as: Three… Two…

5 String Concatenation The string concatenation operator (+) is used to append one string to the end of another "Peanut butter " + "and jelly" 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 65)Facts.java System.out.println(“We present the following facts for your ” + “extracurricular edification”); NOTE: No ‘;’ here

6 String Concatenation The + operator is also used for arithmetic addition The function that it 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, but parentheses can be used to force the order See Addition.java (page 67)Addition.java System.out.println(“24 and 45 concatenated: ” ); Prints as: 24 and 45 concatenated: 2445

7 Escape Sequences What if we want to print the quote character itself? The following line would confuse the compiler because it would interpret the two pairs of quotes as two strings and the text between the strings as a syntax error: System.out.println ("I said "Hello" to you."); An escape sequence is a series of characters that represents a special character Escape sequences begin with a backslash character ( \ ) System.out.println ("I said \"Hello\" to you."); A String Syntax Error A String

8 Escape Sequences Some Java Escape Sequences See Roses.java (page 68)Roses.java System.out.println(“Roses are red,\n\tViolets are blue,\n” + Prints as: Roses are red, Violets are blue, Escape Sequence \b \t \n \r \" \' \\ Meaning backspace tab newline carriage return double quote single quote backslash

9 Variables A variable is a name for a location in memory A variable must be declared by specifying the variable's name and the type of information that it will hold Multiple variables can be created in one declaration: int total; int count, temp, result; data type variable name

10 Variable Initialization A variable can be given an initial value in the declaration When a variable is referenced in a program, its current value is used See PianoKeys.java (page 70)PianoKeys.java Prints as: A piano has 88 keys. int sum = 0; int base = 32, max = 149; int keys = 88; System.out.println(“A piano has ” + keys + “ keys.”);

11 Assignment An assignment statement changes the value of a variable The assignment operator is the = sign The expression on the right is evaluated and the result is stored as the value of the variable on the left The value previously stored in total is overwritten You can only assign a value to a variable that is consistent with the variable's declared type See Geometry.java (page 71)Geometry.java total = 55;