Introduction to Java 15-121 Fall 2020 Margaret Reid-Miller
Academic Integrity • You must do your own work. • Discussion with other students is limited to clarifying the assignments or at a high non- code level (pictures). • If you need help, ask the course staff or me (see the Staff page for office hours). • You should never look at another person’s program nor allow another student to look at your program (except recitation labs). Fall 2020 15-121 (Reid-Miller) 2
What if I cheat on a assignment? • Record a negative score on the assignment. • Reduce your final semester letter grade. • Prevent you from dropping, withdrawing, or changing to pass/fail. • Send letters to Student Affairs, your advisor, various deans, etc. • May result in long-term suspension. Fall 2020 15-121 (Reid-Miller) 4
Life happens So I give you some late days. How many late days in total? 4 How many late days can you use for a single assignment? 2 What is the penalty for turning an assignment late when you run out of late days? No credit, so always submit something on time Fall 2020 15-121 (Reid-Miller) 5
Quizzes • When is our first quiz? Tomorrow • How often do we have a quiz? Every Thursday • How many quizzes will be counted? 10 (1-2 will be dropped) • Can your arrange to make up a quiz? You can’t, but each is worth only 0.5% Fall 2020 15-121 (Reid-Miller) 6
Java vs Python • Program Translation: • Java is compiled , Python is interpreted • Java syntax: • Uses braces {} to group statements (indentation is not semantic, but recommended) • Most statements end in semi-colons. • Java programs: • Often have several classes, each in a file with same name. • Java programs start with the method main . 15-121 (Reid-Miller) 7
There is no required textbook. • I will post related readings, however, from a free on-line text book Introduction to Programming Using Java by David J. Eck that you may find helpful for your understanding. • You are welcome to use other books on data structures using Java. Fall 2020 15-121 (Reid-Miller) 8
Today • Data types (primitive/reference) • Basic Operators • Variable declaration • Strings • Before next lecture, review and compare syntax of LetterCounter1.java and LetterCounter1.py Fall 2020 15-121 (Reid-Miller) 9
Java requires that you declare the type of each variable before it is used. • You also need to declare the type of the expression return from a method Basic types: (true or false) boolean in t (integer) (floating point decimal) double (sequence of characters) String … return type of a method that returns no value void Java checks that the types match when you compile. • Compiler error messages often indicate a type mismatch. Fall 2020 15-121 (Reid-Miller) 10
Primitive Data Types • Java has two categories of data: • primitive data (e.g., number, character) • object data (programmer created types) • There are 8 primitive data types: byte, short, int, long, float, double, char, boolean • Primitive data are only single values; they have no special capabilities • You cannot define new primitive data types. Fall 2020 15-121 (Reid-Miller) 11
Common Basic Types Type Description Example of Literals integers (whole 42 , 60634 , -8 , 0 int numbers) 0.039 , -10.2 , 4.2E+72 real numbers double single characters 'a' , 'B' , '&', '6' char logical values true , false boolean "" , "a" , "ab6&" List of characters String Fall 2020 15-121 (Reid-Miller) 12
Mostly use int and double Type Storage Range of Values 8 bits -128 to 127 byte 16 bits -32,768 to 32,727 short 32 bits -2,147,483,648 to 2,147,483,647 int -9x10 18 to 9x10 18 64 bits long ± 10 -45 to ± 10 38 , 7 significant digits 32 bits float ± 10 -324 to ± 10 308 , 15 significant digits 64 bits double Fall 2020 15-121 (Reid-Miller) 13
Recall • What is 2 10 in base 10, approximately? 10 3 • 2 20 ? 1 million • 2 30 ? 1 billion • 2 32 ? 4 billion Fall 2020 15-121 (Reid-Miller) 14
Note the limited range of int Type Storage Range of Values 8 bits -128 to 127 byte 16 bits -32,768 to 32,727 short 32 bits -2 billion to 2 billion, approximately int -9x10 18 to 9x10 18 64 bits long ± 10 -45 to ± 10 38 , only 7 significant digits! 32 bits float ± 10 -324 to ± 10 308 , 15 significant digits 64 bits double Fall 2020 15-121 (Reid-Miller) 15
Basic Operators Type Description Operators + , - , * , / , % , ++ , -- integers int + , - , * , / real numbers double + , - ( int arithmetic) single characters char == , != , < , > , <= , >= relational boolean ! , && , || logical + (concatenation) Strings String Fall 2020 15-121 (Reid-Miller) 16
Before you can use a variable, you must declare its type. • You can declare a variable only once in a method. • Examples: int numDimes; camelCase is the double length; Java convention char courseSection; boolean done; String lastName; Fall 2020 15-121 (Reid-Miller) 17
Declaring Variables • Declaring a variable instructs the compiler to set aside a portion of memory large enough to hold data of that type. int count; double length; count length • No value has be put in memory yet. That is, the variable is undefined . Fall 2020 15-121 (Reid-Miller) 18
Assignment Statements • An assignment statement stores a value into a variable's memory location: <variable> = <expression>; • The type of the variable and expression must match . • The first assignment to a variable initializes it. 3 count = 3; count length = 72.3 + 2.0; length 74.3 Fall 2020 15-121 (Reid-Miller) 19
Assignment statements shortcuts int n; n = 0; int n = 0; n = n + 1; n += 1; n++; n = n - 1; n -= 1; n--; Other assignment operators: *=, /=, %= Fall 2020 15-121 (Reid-Miller) 20
Strings https://docs.oracle.com/javase/8/docs/api/java/lang/String.html • String is not a primitive type, but a class/type • String variables store references to memory addresses • Strings are immutable ! That is, you cannot change a string, but you can assign a new string to a variable. • A string cannot span more than one line: "Not a valid WRONG! String literal” How do you include a newline or single quote in a string? Fall 2020 15-121 (Reid-Miller) 21
As with Python, Java has escape sequences Escape sequence: a two-character sequence that represent a single special character. Sequence Meaning tab character \t newline character \n double quote \ " single quote \' \\ backslash character System.out.println( " What \ " character\ " is this \\? " ); Fall 2020 15-121 (Reid-Miller) 22
An object of type String is a sequence of (unicode) characters. • When we declare a variable of type String , it does not create an object. founder null String founder; • To create an object we use the new operator: founder = new String("Carnegie"); initializes its state • Strings have a shortcut way of creating them: String founder2 = "Mellon"; Fall 2020 15-121 (Reid-Miller) 23
Object vs Primitive Data • A primitive variable holds an actual value: count 15121 int count = 15121; • An object variable holds a reference (address) to the object. String founder = new String("Carnegie"); a35f3cd object data "Carnegie" founder a35f3cd length() object methods reference: computer substring() … generated address A String object 24 Fall 2020 15-121 (Reid-Miller)
String Length method header int length() • int indicates that the data type of the value the method returns. • Returns the number of characters in this string. • Example: String founder = "Carnegie"; int numChar = founder.length(); object method dot operator Fall 2020 15-121 (Reid-Miller) 25
Getting a single character char charAt(int index) Returns the character at a specified index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 C a r n e g i e M e l l o n Example: String school = "Carnegie Mellon"; char firstChar = school.charAt(0); WARNING: You cannot assign a char to an object of type String without first converting the char to a String object ! e.g. , String initial = "" + firstChar; Fall 2020 15-121 (Reid-Miller) 26
An example of method overloading, Substrings where the number of parameters differs. String substring(int startIndex, int endIndex) String substring(int startIndex) Returns a new string consisting of the substring starting at startIndex ( inclusive ) and ending at endIndex (exclusive) or, if one parameter, to the last character. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 C a r n e g i e M e l l o n Example: String school = "Carnegie Mellon"; String founder = school.substring(0, 8); String founder2 = school.substring(9); Fall 2020 15-121 (Reid-Miller) 27
Replacing Characters String replace(char oldChar, char newChar) Returns a new String object resulting from replacing every occurrence of oldChar with newChar . • The original String object is unchanged. (Strings are immutable !) Example: String founder = "Carnegie"; OUTPUT: System.out.println( CarnEgiE founder.replace('e', 'E')); Carnegie System.out.println(founder); Fall 2020 15-121 (Reid-Miller) 28
Recommend
More recommend