CS110- Lecture 3 Feb 7, 2005 Agenda Character Strings String Concatenation Escape Sequence Variables and Assignment Primitive Data Types Program to demonstrate above concepts 6/30/2016 CS110-Spring 2005, Lecture 3 1 Character Strings Java provides the ability to use a string literal, delimited by double quotation characters. Examples: “Today is Monday” “100 Morrissey Blvd. “ “” A string literal can contain any valid characters , including numeric digits, punctuation and other special characters. 6/30/2016 CS110-Spring 2005, Lecture 3 2 Character Strings System.out.println(“Welcome to CS110”); Object dot message (information) Message from Welcome: println(“Welcome to CS110”) Method 6/30/2016 System.out object Parameter CS110-Spring 2005, Lecture 3 3 Character Strings: String Concatenation A String literal cannot span multiple lines in a program. // following statement will give compile // time error System.out.println(“Welcome to CS110:Introduction to Computing”); String concatenation operator (+) is used to append one string to the end of another. 6/30/2016 CS110-Spring 2005, Lecture 3 4 Character Strings: String Concatenation System.out.println(“Welcome to ” + “CS110:Introduction to Computing”); We may chose to send no information to println method. e.g. System.out.println(); System.out.println(“Total students :”+ 24); Number not a String (no double quotes) 6/30/2016 CS110-Spring 2005, Lecture 3 5 Character Strings: String Concatenation Strings can be concatenated with numbers. The number is automatically converted to String and then the two Strings are concatenated. + operator is also used for arithmetic addition. Results depends on the types of data on which it operates. If both operands are numbers then arithmetic addition is performed. If either or both operands are Strings then String concatenation is performed. 6/30/2016 CS110-Spring 2005, Lecture 3 6 Character Strings: String Concatenation System.out.println(“No. of students :”+ 2 + 4); System.out.println(“No. of students :”+ (2 + 4)); 6/30/2016 CS110-Spring 2005, Lecture 3 7 Character Strings: Escape Sequences Suppose I want to print “ System.out.println(“””); //Wrong Compiler will be confused Java defines several escape sequences to represent special characters. 6/30/2016 CS110-Spring 2005, Lecture 3 8 Character Strings: Escape Sequences An escape sequence begins with the backslash character (\) which indicates that the character or characters that follow should be interpreted in a special way. System.out.println(“Welcome to” + “CS110:\nIntroduction to Computing”); 6/30/2016 CS110-Spring 2005, Lecture 3 9 Variables Variables store values. You must tell the compiler the kind of value you want to store, and the name of the place you will store it Declaration syntax: type name; type tells the compiler what kind of value you want to store name is the identifier you choose so that you can refer to the variable elsewhere in the program. Declaration may (but need not) initialize value. 6/30/2016 CS110-Spring 2005, Lecture 3 10 Variables Examples: int i = 10; int i, j; int i = 10, j = 20; Naming convention: Method and variable names start lower case: i, j, main, println. Internal words capitalized. E.g. noOfStudents A method’s variables are declared … inside the method where they are used near where they are first used (our convention) or at the top of the method (another convention). 6/30/2016 CS110-Spring 2005, Lecture 3 11 Variables public class Welcome { public static void main(String[] args) { System.out.println(“CS110:”); int noOfStudents = 24; System.out.println(“Total:”+ noOfStudents); } When a variable is referenced } the value currently stored in it is used. 6/30/2016 CS110-Spring 2005, Lecture 3 12 The Assignment Statement public class Welcome { public static void main(String[] args) { System.out.println(“CS110:”); int noOfStudents = 24; System.out.println(“Total:”+ noOfStudents); System.out.println(“CS210:”); noOfStudents = 15; // Assignment statement System.out.println(“Total:”+ noOfStudents); } } 6/30/2016 CS110-Spring 2005, Lecture 3 13 The Assignment Statement A variable can store one value of its declared type. A new value overwrites the old value. Example (primitive types) noOfStudents int noOfStudents = 24; 24 noOfStudents = 15; noOfStudents 15 assignment operator 6/30/2016 CS110-Spring 2005, Lecture 3 14 The Assignment Statement x=y means assign the value of y to the memory location x public class AssignmentDemo { public static void main(String[] args) { int y = 6; // declare y, initialize value int x = 3; // declare x, initialize value x = y; } } 6/30/2016 CS110-Spring 2005, Lecture 3 15 The Assignment Operator (=) int y = 6; y 6 int x = 3; x 3 x = y; y 6 x 6 x = y is not the same as y = x This makes mathematicians unhappy 6/30/2016 CS110-Spring 2005, Lecture 3 16 Constants Constants are similar to variables except that they hold a particular value. Their value doesn’t change. You precede a variable declaration with the reserved word final. The compiler will produce a error message if you attempt to change the value. The only valid place to change their value is in the initial assignment. e.g.: final int MAX_CAPACITY = 100; 6/30/2016 CS110-Spring 2005, Lecture 3 17 Primitive data types Every variable must have a data type. int, float, boolean, ... (Java keywords) Just eight of them, built into language Four categories: Integers (byte, short, int, long) Decimals (float, double) Character (char) Boolean (boolean) 6/30/2016 CS110-Spring 2005, Lecture 3 18 Integers byte, short, int, long There are maximum and minimum values. They are signedboth positive and negative values can be stored in them. 6/30/2016 Type Storage Min value Max (bits) value byte 8 -128 short 16 -32768 32767 int 32 long 64 CS110-Spring 2005, Lecture 3 -231 -263 127 231-1 263-1 19 Decimals Type Storag Min value Max e (bits) value float, double There are maximum float 32 and minimum values. double 64 They are signedboth positive and negative values can be stored in them. 6/30/2016 CS110-Spring 2005, Lecture 3 ----- ----- ----- ----- 20 Characters char Storage (16 bits) They are unsigned. Character literal is expressed in single quotes. E.g.: char c = ‘a’; 6/30/2016 CS110-Spring 2005, Lecture 3 21 Boolean boolean Just two values true and false A boolean value cannot be converted to any other data type, nor can any data type be converted to a boolean value. Mostly used in tests E.g.: boolean done = false; // we are still not done :) 6/30/2016 CS110-Spring 2005, Lecture 3 22 Reference types String greeting; //What is the data type of greeting? greeting is of String data type Welcome welcome ; //What is the data type of welcome? welcome is of Welcome data type Data Types Primitive data types (int, float…) 6/30/2016 Reference data types (String, Welcome…) CS110-Spring 2005, Lecture 3 23 Reference types There are as many classes as you care to invent. (not limited to 8) Some of them are predefined in Java Standard library (String, …..). int i; Primitive Data Type Variable i is of type int String greeting; Reference Data Type 6/30/2016 CS110-Spring 2005, Lecture 3 Variable greeting is an object of type String 24 Reading Exercise Section 2.4-2.6 6/30/2016 CS110-Spring 2005, Lecture 3 25