Lecture 3 dataAndExpressions_4up

Lecture 3 dataAndExpressions_4up - Character Strings CMPT...

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

View Full Document Right Arrow Icon
CMPT 125: Lecture 3 Data and Expressions Tamara Smyth, [email protected] School of Computing Science, Simon Fraser University January 3, 2009 1 Character Strings A character string is an object in Java, defned by the class String . Because they are used so Frequently, Java allows the use oF string literals , delimited by double quotes ( " ). We will return to the String class, For now let’s look more closely at string literals. A string literal may include any valid character: or no characters at all: "" CMPT 125: Data and Expressions, Lecture 3 2 The print and println Methods Recall From the program WiseWords , we invoked the println method as Follows: System.out.println("No matter where you go . .."); System.out.println(". .. there you are."); System.out represents an output device, or fle, which by deFault is the monitor screen. The object’s name is out and it is stored in the System class. The method println prints a character string to the screen. Each piece oF data is sent to a method is called a parameter . The println takes a single parameter which is a character string. The print method is very similar to println but does not move to the beginning oF a new line when completed. CMPT 125: Data and Expressions, Lecture 3 3 String Concatenation A string literal may not span multiple lines. //The following statement will not compile System.out.println("It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of disbelief, it was the epoch of incredulity, . ..."); Use ‘+’ to append, or concatenate one string to another: IF you use an integer literal in a string concatenation, it will automatically convert 100 to “100”. System.out.println("This is probably one of the longest " + "sentences ever to open novel. " + "It uses over " + 100 + " words."); OUTPUT: This is probably one of the longest sentences ever to open a novel. It uses over 100 words. CMPT 125: Data and Expressions, Lecture 3 4
Background image of page 1

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

View Full DocumentRight Arrow Icon
Concatenation vs. Arithmetic Addition Of course, the ‘+’ operator is also used for arithmetic addition. The action performed by ‘+’ depends on the data type of the parameter on which it operates. CODE: public class AdditionDemo { //The difference between concatenation and //arithmetic addition System.out.println("2 and 2 concatenated: " + 2 + 2); System.out.println("2 and 2 added: " + (2 + 2)); OUTPUT: 2 and 2 concatenated: 22 2 and 2 added: 4 First call to println : performs concatenation since operators are executed left to right . Second call to println : the parenthesis groups two numeric operands, (2+2) , forcing this operation (addition) to happen ±rst. This result, 4, is then concatenated with the preceding string. CMPT 125: Data and Expressions, Lecture 3
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/07/2010 for the course CMPT 125 taught by Professor Diana during the Spring '08 term at Simon Fraser.

Page1 / 9

Lecture 3 dataAndExpressions_4up - Character Strings CMPT...

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

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