DOC PREVIEW
UMD CMSC 131 - Lecture Set #3: Java Expressions

This preview shows page 1-2-3-4-5 out of 14 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 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 Avoid this lowercase L It looks too much like the digit 1 double Two allowable forms Decimal notation 3 14159 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 F f 2007 is used to indicate they are float 131 Spring Jan Plane adapted from Bonnie Dorr 4 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 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 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 System out print Enter an integer Create new scanner object to read from keyboard i sc nextInt System out print Enter a floating point value d sc nextDouble System out print Enter a string s sc next Input an integer Input a double Input a string up to white space 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 Input source can be keyboard System in files etc Object must be assigned to a variable e g sc Operations nextBoolean nextByte nextDouble nextFloat nextInt nextLong nextShort next Returns value of indicated type reports error if type mismatch Returns sequence of characters up to next whitespace space carriage return tab etc nextLine Returns sequence of characters up to next carriage return CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 11 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


View Full Document

UMD CMSC 131 - Lecture Set #3: Java Expressions

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture Set #3: Java Expressions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Set #3: Java Expressions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Set #3: Java Expressions and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?