220 likes | 395 Vues
This chapter delves into the concepts of printing and concatenating strings in Java. It explores the use of `System.out.println` and `System.out.print` for displaying output, alongside the mechanics of string literals and the concatenation operator (+). Readers will learn how to properly format strings with numbers, understand the impact of parentheses on operations, and utilize escape sequences for advanced printing needs. It provides practical examples and exercises to solidify the learning, making it essential for beginners in Java programming.
E N D
println & print System.out.println(“ Will print this statement and then return to a new line”); System.out.print( “Will print this statement and stay on the same line” ); System.out.println(“ I’m on the line with the statement above.”);
// ************************************************************************ // PrintPrintln.java Author: Gail Chapman // This program demonstrates Println and Print // *********************************************************************** public class PrintPrintln { public static void main(String[]args) { System.out.println("Will print this statement and then return to a new line"); System.out.print( "Will print this statement and stay on the same line" ); System.out.println(" I’m on the line with the statement above."); } } Output: Will print this statement and then return to a new line Will print this statement and stay on the same line I’m on the line with the statement above.
String literals & Concatenation String refers to a string of characters that make up words. String is a class. It is not a primitive data type. Every String literal is an object. String literals are words in quotation marks. “This is a String literal”;
Concatenation The concatenation operator used with Strings is the plus sign + The + sign is also used for arithmetic operations. The + sign joins: Strings and other Strings (“This is a String ” + “ and another String”);
Strings & Numbers Need to be careful to get correct output from numbers. System.out.println(“We want to add “ + 10 + 15); What is the output: The compiler starts at the left encounters the String and reads everything after it as a String. It will perform String concatenation. Output: We want to add 1015 10 and 15 are printed as if they are String literals.
Strings & Numbers The plus sign is also used as the addition operator. System.out.println(36 + 4 + “ is 36 + 4”); The numbers will be added together because they are at the beginning before the String. So it will perform addition. Output: 40 is 36 + 4.
Strings and Numbers You can use parentheses to force addition with the + sign. System.out.println(“36 + 5 = “ + (36+5)); Parentheses have highest precedence so it performs that operation first. Output: 36 + 5 = 40
What will print? System.out.println("12" + 3 + 4); System.out.println("12" + (3 + 4)); System.out.println(12 + 3 + 4); System.out.println(12 + "3" + 4); System.out.println(12 + 3 + "4"); 1234 127 19 1234 154
Check Printing String literals with concatenation What will the following print? System.out.println(“This will print “ + 3 + 2 + 0); System.out.println(“This will print “ + (3 + 2 + 0)); System.out.println(3 + 2 + 0 + “ will print”); System.out.println(“This will print “ + 2 * 6); This will print 320 This will print 5 5 will print This will print 12
public class ConcatenationRules { public static void main(String[]args) { System.out.println("This will not add the numbers 10 + 15 together " + 10 + 15 ); System.out.println(36 + 4 + " will be the sum of 36 + 4"); System.out.println("This will add 36 + 4 because of the ( ) " + (36+4)); } } Output: This will not add the numbers 10 + 15 together 1015 40 will be the sum of 36 + 4 This will add 36 + 4 because of the ( ) 40 >
Escape sequences Page 60 • Used with Strings to produce a newline, slash or quotation marks. \t tab will tab 5 spaces \n newline will go to a new line \\ will print \ will print one slash \” will print double quotes
Escape Sequences //escape characters public class Escape { public static void main(String[]args) { System.out.println("A slash n \nis used to go to a new line\n"); System.out.println("A slash quotation mark \"will\" print a double quote"); System.out.println("double slash \\ will print one backslash."); System.out.println("A slash t twill tab.\n\tName\t\tAge\t\tDOB"); System.out.println("A slash one quotation \'will\' produce single quote."); } }
PRIMITIVE DATA TYPES: Information in program is stored in variables. Data type is the type of data stored in the variable. There are 8 Primitive data types in Java.
Memory Storage • In order to reserve space in memory for the variable, the computer has to know what type of data it will be. • Declare = to tell what type of data the variable will hold and its name • Initialize = assign the variable a value using the = sign
Declare is to tell what type of data the variable will hold and its name • the computer needs to know how much memory to store for that variable int number; double money; boolean done; You cannot use a variable until you declare the type Initalize is to assign the variable a value using the = sign number = 4; money = 7.50; done = false ; Can Initialize & Declare at same time int number = 37; double money = 28.42; boolean done = true;
data type with operations • when you use 2 integers in an operation the result is an integer • 20/6 + 3 = 20/6 = 3 + 3 = 6 • When you use a double as one of the operands then the result is a double • 20/6.0 + 3 = 20/6.0 = 3.333 + 3 = 6.333
Using variables to store the answer Storing that information in a variable • integers can be stored in an int or a double int a = 20; int b = 6 int c = 3; int d; double d; 20/6 + 3 = 6.0; d = a / b + c ; 20/6 + 3 = 6
data type variable name Variables • A variable is a name for a location in memory • A variable must be declared by specifying the variable's name and the type of information that it will hold int total; int count, temp, result; Multiple variables can be created in one declaration
Variables • A variable can be given an initial value in the declaration. Called initializing. int sum = 0; int base = 32, max = 149; double amount = 47.52; boolean done = false; public class PianoKeys { //----------------------------------------------------------------- // Prints the number of keys on a piano. //----------------------------------------------------------------- public static void main (String[] args) { int keys = 88; System.out.println ("A piano has " + keys + " keys."); } } • When a variable is referenced in a program, its current value is used • See PianoKeys.java
Assignment • An assignment statement changes the value of a variable • The assignment operator is the = sign int total = 55; total = 47; • The expression on the right is evaluated and the result is stored in the variable on the left • The value that was in totalinitially is overwritten • You can assign only a value to a variable that is consistent with the variable's declared type could not say total = 47.50 because total is declared as an int; • See Geometry.java (page 70)
//********************************************************************//******************************************************************** // Geometry.java Author: Lewis/Loftus/Cocking // // Demonstrates the use of an assignment statement to change the // value stored in a variable. //******************************************************************** public class Geometry { //----------------------------------------------------------------- // Prints the number of sides of several geometric shapes. //----------------------------------------------------------------- public static void main (String[] args) { int sides = 7; // declaration with initialization System.out.println ("A heptagon has " + sides + " sides."); sides = 10; // assignment statement System.out.println ("A decagon has " + sides + " sides."); sides = 12; System.out.println ("A dodecagon has " + sides + " sides."); } • }