Lecture 3 dataAndExpressions_4up

Lecture 3 dataAndExpressions_4up - Character Strings CMPT...

Info icon This 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, defined 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: "I like green @#($*& eggs and ham!" 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 file, 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
Image of page 1

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

View Full Document Right 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 first. This result, 4, is then concatenated with the preceding string. CMPT 125: Data and Expressions, Lecture 3 5 Escape Sequences An escape sequence can be used to represent a character that would otherwise cause compilation error.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern