02_Data_and_Expressions

02_Data_and_Expressions - 2. Data and Expressions...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Slide 2 - 1 COMP 1210 – Fundamentals of Computing I 2. Data and Expressions • Objectives - when we have completed this chapter, you should be familiar with: character strings & escape sequences variables and assignment primitive data if and if-else statements expressions and operator precedence Accepting standard input from the user data conversions
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Slide 2 - 2 COMP 1210 – Fundamentals of Computing I 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.“ "Pat Doe, 123 Main Street“ "7" • Every character string is an object in Java, defined by the String class • Every string literal represents a String object
Background image of page 2
Slide 2 - 3 COMP 1210 – Fundamentals of Computing I The println Method • Recall that the println method prints a character string • The System.out object is an output stream corresponding to a display destination (the monitor screen) object method name information provided to the method (a parameter) System.out.println ( "War Eagle from the Auburn Plains!" ); Q
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Slide 2 - 4 COMP 1210 – Fundamentals of Computing I The print Method • The print method in the system.out object is similar to the println method, except that it does not advance to the next line • Therefore anything printed after a print statement will appear on the same line • See CountOff.java
Background image of page 4
Slide 2 - 5 COMP 1210 – Fundamentals of Computing I String Concatenation • The string concatenation operator (+) appends one string to the end of another "Peanut butter " + "and jelly" • It can also append a number to a string • A string literal cannot be broken across two lines in a program • See ConcatenationExample1
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Slide 2 - 6 COMP 1210 – Fundamentals of Computing I String Concatenation • The + operator also used for arithmetic addition if both operands are numeric 5 + 10 15 • If one or both operands is a string, + performs string concatenation 5 + " years" "5 years" "years: " + 5 "years: 5" • The + operator is evaluated left to right, but parentheses can be used to force the order • See ConcatenationExample2 (Experiment with String expressions in the interactions pane in jGRASP)
Background image of page 6
Slide 2 - 7 COMP 1210 – Fundamentals of Computing I Escape Sequences • What if we wanted to print a quote character? • The following line would cause a compile-time error - it would interpret the second quote as the end of the string System.out.println ( "I said " Hello " to you." ); • An escape sequence represents a special character • An escape sequence begins with a backslash character ( \ ) System.out.println ( "I said \"Hello\" to you." ); Q
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Slide 2 - 8 COMP 1210 – Fundamentals of Computing I Escape Sequences • Some Java escape sequences: • Use \r\n together to move to the next line • See EscapeSeq.java Escape Sequence \b \t \n \r \" \' \\ Meaning backspace tab newline or line feed (LF) carriage return (CR) double quote single quote backslash
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 52

02_Data_and_Expressions - 2. Data and Expressions...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online