Ch2-Data&Expressions - 1 Java Software Solutions...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Java Software Solutions Chapter 2 - Data and Expressions 1 Miguel A. Labrador Department of Computer Science & Engineering labrador@csee.usf.edu http://www.csee.usf.edu/~labrador Dr. Miguel A. Labrador Topics • Character Strings and escape sequences • Variables • Primitive data types • Expressions 2 Copyright© Dr. Miguel A. Labrador 2 Copyright© Dr. Miguel A. Labrador Data and Expressions– 2 • Data conversions • Interactive Programs • Lab Assignment Character Strings and Escape Sequences 3 2 Character Strings • A string of characters can be represented as a string literal by putting double quotes around the text: • Examples: 4 Copyright© Dr. Miguel A. Labrador 4 Copyright© Dr. Miguel A. Labrador Data and Expressions– 4 "This is a string literal." "123 Main Street" "X" The println Method • In the Lincoln program, 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 5 Copyright© Dr. Miguel A. Labrador 5 Copyright© Dr. Miguel A. Labrador Data and Expressions– 5 System.out.println ("Whatever you are, be a good one."); object method name information provided to the method (parameters) The print Method • The System.out object provides another service as well • The print method is similar to the println method, except that it does not advance to the next line 6 Copyright© Dr. Miguel A. Labrador 6 Copyright© Dr. Miguel A. Labrador Data and Expressions– 6 • Therefore anything printed after a print statement will appear on the same line 3 Countdown.java public class Countdown { //----------------------------------------------------------------- // Prints two lines of output representing a rocket countdown. //----------------------------------------------------------------- public static void main (String args) { 7 Copyright© Dr. Miguel A. Labrador 7 Copyright© Dr. Miguel A. Labrador Data and Expressions– 7 { System.out.print ("Three... "); System.out.print ("Two... "); System.out.print ("One... "); System.out.print ("Zero... "); System.out.println ("Liftoff!"); System.out.println ("Houston, we have a problem."); } } 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 8 Copyright© Dr. Miguel A. Labrador 8 Copyright© Dr. Miguel A. Labrador Data and Expressions– 8 It can also be used to append a number to a string • A string literal cannot be broken across two lines in a program String Concatenation // Strings can be concatenated into one long string System.out.println ("We present the following facts for your " + "extracurricular edification:"); 9 Copyright© Dr. Miguel A. Labrador 9 Copyright© Dr. Miguel A. Labrador Data and Expressions– 9 // A numeric value can be concatenated to a string System.out.println ("Year in which Leonardo da Vinci invented " + "the parachute: " + 1515); System.out.println ("Speed of ketchup: " + 40 + " km per year"); 4...
View Full Document

Page1 / 24

Ch2-Data&Expressions - 1 Java Software Solutions...

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

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