UMD CMSC 131 - Lecture Set #3: Java Expressions (5 pages)
Previewing pages 1, 2 of 5 page document View the full content.Lecture Set #3: Java Expressions
Previewing pages 1, 2 of actual document.
View the full content.View Full Document
Lecture Set #3: Java Expressions
0
0
83 views
- Pages:
- 5
- School:
- University of Maryland, College Park
- Course:
- Cmsc 131 - Object-oriented Programming I
Object-oriented Programming I Documents
-
14 pages
-
19 pages
-
Lecture 13: Libraries and Encapsulaton
14 pages
-
18 pages
-
Lecture 12: Runtime Management
3 pages
-
Lecture 16: Method Overloading
14 pages
-
Lecture Set 5: Design and Classes
16 pages
-
Lecture 21: Algorithms and Design
7 pages
-
Lecture 10: Static Methods and Variables
4 pages
-
30 pages
-
Lecture 17: Ternary Operator, Switch, Break, Continue
10 pages
-
Lecture 17: Ternary Operator, Switch, Break, Continue
10 pages
-
Lecture 14: Libraries and Encapsulaton
15 pages
-
5 pages
-
Lecture Set #13: Args Array and Commenting Review
2 pages
-
9 pages
-
Lecture 6: If-Else-If and Loops
23 pages
-
2 pages
-
Lecture 15: Review of Aliasing & Mutability, Floating Point Calculations
7 pages
-
17 pages
-
6 pages
-
Lecture 15: Intra-Class Method Calling
12 pages
-
9 pages
-
7 pages
-
18 pages
-
13 pages
-
2 pages
-
Lecture 6: If-Else-If and Loops
26 pages
-
16 pages
-
12 pages
-
Lecture 18: Implicit Promotion
28 pages
-
12 pages
-
Lecture 9: Objects and Classes in Java
7 pages
-
Lecture Set 4: More About Methods and More About Operators
7 pages
-
Lecture Set #7: Exceptions & Mutability Issues
6 pages
-
Lecture 22: More on Array Operations
13 pages
-
13 pages
-
26 pages
-
5 pages
-
Lecture Set #16: Command Line Running Issues & Comments
3 pages
-
4 pages
-
Lecture 21: Algorithms and Design
19 pages
-
18 pages
-
Lecture Set #14: Algorithms and Design
6 pages
-
7 pages
-
7 pages
-
20 pages
-
3 pages
-
6 pages
-
6 pages
-
Lecture 10: Objects and Classes in Java
18 pages
-
19 pages
-
Lecture Set 5: Design and Classes
15 pages
-
Lecture Set #14: Algorithms and Design
8 pages
-
8 pages
-
18 pages
-
5 pages
-
Lecture 9: Objects and Classes in Java
20 pages
-
22 pages
-
Lecture 10: Static Methods and Variables
11 pages
-
2 pages
-
12 pages
-
10 pages
-
Lecture 9: Introduction to Objects and Classes
20 pages
-
Lecture Set 4: More About Methods and More About Operators
11 pages
-
12 pages
-
Lecture 12: Static Methods and Variable
10 pages
-
7 pages
-
Lecture Set #15: Two-Dimensional Arrays
3 pages
-
22 pages
-
Lecture 17: Implicit Promotion
14 pages
-
5 pages
-
17 pages
-
Lecture Set #11: Polymorphism Introduction
4 pages
-
19 pages
-
Lecture Set #3: Java Expressions
14 pages
-
Lecture Set 6: Static Methods & Variables and Exceptions
6 pages
-
3 pages
-
Lecture 12: Runtime Management
7 pages
-
7 pages
-
19 pages
-
Lecture 8: Introduction to the Heap and Garbage Collection
5 pages
-
Lecture 8: Introduction to the Heap and Garbage Collection
15 pages
-
Lecture Set 6: Static Methods & Variables and Exceptions
9 pages
-
13 pages
-
Lecture Set #10: Two-Dimensional Arrays
5 pages
-
9 pages
-
6 pages
-
8 pages
-
17 pages
-
16 pages
-
Lecture 15: Review of Aliasing & Mutability, Floating Point Calculation
3 pages
-
14 pages
-
14 pages
-
14 pages
-
3 pages
-
Lecture Set 4: Evaluation Order
6 pages
Sign up for free to view:
- This document and 3 million+ documents and flashcards
- High quality study guides, lecture notes, practice exams
- Course Packets handpicked by editors offering a comprehensive review of your courses
- Better Grades Guaranteed
Lecture Set 3 Java Expressions Last time 1 Basics of Java programs Today 1 Variables and types 2 Expressions in Java 3 User input CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr Variables are named storage locations Variable x Value 5 Recall that memory is a sequence of bits Question How much memory to allocate for a variable s value Answer A variable must have a type specifying how much storage to allocate CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 1 Recall Java Built in Types Integers Reals Other Type name byte short int long float double char boolean Size bytes 1 2 4 8 4 8 2 1 CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 2 1 Primitive Data Types In Detail Integer Types byte 1 byte short 2 bytes int 4 bytes long 8 bytes Floating Point Types float 4 bytes double 8 bytes Other types boolean 1 byte char 2 bytes Range 128 to 127 Range 32 000 to 32 000 Range 2 billion to 2 billion Range 9 quintillion to 9 quintillion 3 4x1038 to 3 4x1038 7 digits of precision 1 7x10308 to 1 7x10308 15 digits of prec true false A single Unicode character CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 3 Primitive Type Constants Constants are also called literals Integer types byte short int long optional sign and digits 0 9 12 1 234 0 1234567 Same as above but followed by L or l 1394382953L Floating point types double Two allowable forms Decimal notation 3 14159 Avoid this lowercase L It looks too much like the digit 1 234 421 0 0042 43 0 Scientific notation use E or e for base 10 exponent 3 145E5 3 145 x 105 314500 0 1834 23e 6 1834 23 x 10 6 0 00183423 float Same as double but followed by f or F 3 14159F 43 2f Note By default integer constants are int unless L l is used to indicate they are long Floating constants are double CMSC unless is used to indicate they are float 131 F f Spring 2007 4 Jan Plane adapted from Bonnie Dorr Character and String Constants Char constants Single character in single quotes including Letters and digits A B C a b c 0 1 9 Punctuation symbols except and backslash Escape sequences see below String constants 0 or more characters in double quotes Escape sequences Allows inclusion of special characters double quote single quote backslash n new line character start a new line t tab character Examples char x x contains a single quote String s1 Hi there s1 contains Hi there String s2 C WINDOWS s2 contains C WINDOWS CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 5 2 Common Numeric Operators Arithmetic operators Unary negation Addition subtraction Multiplication division x x y x y x y x y Division between integer types truncates to integer 23 4 5 x y returns the remainder of x divided by y 23 4 3 Division with real types yields a real result 23 0 4 0 5 75 Comparison operators Equality inequality x y x y Less than greater than x y x y Less than or equal greater than or equal x y x y These comparison operators return a boolean value true or false CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 6 Common String Operators String Concatenation The operator concatenates joins two strings Go Terps GoTerps When a string is concatenated with another type the other type is first evaluated and converted into its string representation 8 4 degrees 32degrees Note Concatenation does not add any space 1 2 5 35 String Comparison Strings have special comparison functions s equals t returns true if s and t have the same characters s compareTo t compares strings lexicographically dictionary order result 0 result 0 result 0 if s precedes t if s is equal to t if s follows t dilbert compareTo dogbert 1 which is 0 Both functions are case sensitive CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 7 Example 2 Basic Types Demonstration of primitive types and also the String type Note that you can declare many different variables with one statement public class Example2 public static void main String args int i1 i2 i3 double f1 7 3 f2 9 4 boolean b1 b2 char c String s i1 7 i2 3 i3 i1 i2 5 2 f1 3 1415927 b1 true b2 f2 f1 c X s Hello there my friend System out println i3 i3 System out println b1 b1 System out println b2 b2 System out println c c System out println s s CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 8 3 User Input in Java We ve done output System out what about input Java 5 0 includes the Scanner class feature Can use Scanner to create scanner objects Scanner objects convert user input into data To use Scannner need to import a library import java util Scanner CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 9 Example5 java import java util Scanner public class Example5 public static void main String args int i double d String s Include the definition of the Scanner utility Scanner sc new Scanner System in Create new scanner object to read from keyboard System out print Enter an integer i sc nextInt System out print Enter a floating point value Input an integer Input a double d sc nextDouble System out print Enter a string Input a string up to white space s sc next System out println Here is what you entered System out println i System out println d System out println s CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 10 Scanner Class Details To create a scanner object new Scanner input source Operations Input source can be keyboard System in files etc Object must be assigned to a variable e g sc nextBoolean nextByte nextDouble nextFloat nextInt nextLong nextShort next Returns sequence of characters up to next whitespace nextLine Returns sequence of characters up to next carriage return Returns value of indicated type reports error if type mismatch space carriage return tab etc CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 11 4 Debugging Java Programs Types of errors Compile time caught by Eclipse Java compiler Run time appear during program execution Syntax errors typos etc Type errors misuse of variables Division by 0 Wrong outputs because of mistakes in programming Eclipse helps catch compile time errors Red error Yellow warning CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 12 Example3 java find the errors public class Example3 public static void main String args int x 7 int y 12 double d 72 33 boolean b true char c String s x y d b c s y 24 17 3 x 17 cow Here is something weird x y CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 13 5
View Full Document