Chapter2 - 1 Data and Expressions Let's explore some...

Info iconThis preview shows pages 1–5. 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 Document Right Arrow Icon

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

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

Unformatted text preview: 1 Data and Expressions ¡ Let's explore some other fundamental programming concepts ¡ Chapter 2 focuses on: ¢ character strings ¢ primitive data ¢ the declaration and use of variables ¢ expressions and operator precedence ¢ data conversions ¢ accepting input from the user 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 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 2 2.1 – 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) 2.1 – 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 ¡ Therefore anything printed after a print statement will appear on the same line 3 2.1 – Countdown.java //******************************************************************** // Countdown.java Java Foundations // // Demonstrates the difference between print and println. //******************************************************************** public class Countdown { //----------------------------------------------------------------- // Prints two lines of output representing a rocket countdown. //----------------------------------------------------------------- public static void main (String args) { System.out.print ("Three... "); System.out.print ("Two... "); System.out.print ("One... "); System.out.print ("Zero... "); System.out.println ("Liftoff!"); // appears on first output line System.out.println ("Houston, we have a problem."); } } 2.1 – 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 4 2.1 – Facts.java //******************************************************************** // Facts.java Java Foundations // // Demonstrates the use of the string concatenation operator and the // automatic conversion of an integer to a string. //******************************************************************** public class Facts { //----------------------------------------------------------------- // Prints various facts....
View Full Document

{[ snackBarMessage ]}

Page1 / 26

Chapter2 - 1 Data and Expressions Let's explore some...

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

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