Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println 10. Testing of output is restricted to System.out.print and System.out.println String literals & Concatenation 8. String concatenation + is part of the AP Java subset Escape sequences: produce backslash, double quotes, new line 9. The escape sequences inside strings \\, \", \n are part of the AP Java subset. Data types 1, 7. primitive types int, double, and boolean Variables and assignment 4 Arithmetic Expressions 2, 3, 4, 5, 6 Order of precedence 2. Objects & Classes 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); 1234 127 System.out.println("12" + (3 + 4)); System.out.println(12 + 3 + 4); System.out.println(12 + "3" + 4); System.out.println(12 + 3 + "4"); 19 1234 154 Check Printing String literals with concatenation What will the following print? System.out.println(“This will print “ + 3 + 2 + 0); This will print 320 System.out.println(“This will print “ + (3 + 2 + 0)); This will print 5 System.out.println(3 + 2 + 0 + “ will print”); System.out.println(“This will print “ + 2 * 6); 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 \n \\ \” tab will tab 5 spaces 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. TYPE SIZE RANGE int whole numbers 2147483648 2147473647 double fractional numbers ±4.94065645841246544E-324; ±1.79769313486231570E+308 boolean {true, false} Holds two values: {true, false} 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; d= a/b+c; 20/6 + 3 =+6.0; 20/6 3 =6 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 data type variable name int total; int count, temp, result; Multiple variables can be created in one declaration 19 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; When a variable is referenced in a program, its current value is used See PianoKeys.java 20 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."); } } 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 total initially 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) 21 //******************************************************************** // 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."); } • }