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

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

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

Unformatted text preview:

1CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)Lecture Set #3:Java ExpressionsLast time:1. Basics of Java programsToday:1. Variables and types2. Expressions in Java3. User inputCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)1Variables …… are named storage locations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 typespecifying how much storage to allocate.5xValueVariableCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)2Recall Java Built-in TypesOtherRealsIntegers1boolean2char8double4float8long4int2short1byteSize (bytes)Type name2CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)3Primitive Data Types In DetailInteger Types:byte 1 byte Range: -128 to +127short 2 bytes Range: -32,000 to +32,000int 4 bytes Range: -2 billion to +2 billionlong 8 bytes Range: -9 quintillion to +9 quintillionFloating-Point Types:float 4 bytes -3.4x1038to 3.4x1038, 7 digits of precisiondouble 8 bytes -1.7x10308 to 1.7x10308, 15 digits of prec.Other types:boolean 1 byte true, falsechar 2 bytes A single (Unicode) characterCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)4Primitive-Type ConstantsConstants are also called literalsInteger types:byteshort optional sign and digits (0-9): 12 -1 +234 0 1234567 intlong Same as above, but followed by ‘L’ or ‘l’: -1394382953LFloating-point types:double Two allowable forms:Decimal notation: 3.14159 -234.421 0.0042 -43.0Scientific notation: (use E or e for base 10 exponent)3.145E5 = 3.145 x 105 = 314500.01834.23e-6 = 1834.23 x 10-6= 0.00183423float Same as double, but followed by ‘f’ or ‘F’: 3.14159F -43.2fNote: By default, integer constants are int, unless ‘L’/‘l’ is used to indicate they are long. Floating constants are double, unless ‘F’/‘f’ is used to indicate they are float.Avoid this lowercase L. It looks too much like the digit ‘1’CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)5Character 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 \n new-line character (start a new line)\’ single quote \t tab character\\ backslashExamples: char x = ’\’’; → (x contains a single quote)String s1=”\”Hi there!\””; → s1 contains ”Hi there!”String s2= ”C:\\WINDOWS”; → s2 contains C:\WINDOWS3CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)6Common Numeric OperatorsArithmetic operators:Unary negation: -xAddition/subtraction: x+y x-yMultiplication/division: 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 >= yThese comparison operators return a boolean value: true or false.CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)7Common 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” (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 if s precedes tresult == 0 if s is equal to tresult > 0 if s follows t“dilbert”.compareTo( “dogbert” ) →→→→ -1 (which is < 0)Note: Concatenation doesnot add any spaceBoth functions are case-sensitive.CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)8Example 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);}}4CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)9User 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 2007Jan Plane (adapted from Bonnie Dorr)10Example5.javaimport java.util.Scanner;public class Example5 {public static void main(String[] args) {int i;double d;String s;Scanner sc = new Scanner(System.in);System.out.print("Enter an integer: ");i = sc.nextInt();System.out.print("Enter a floating point value: ");d = sc.nextDouble();System.out.print("Enter a string: ");s = sc.next();System.out.println("Here is what you entered: ");System.out.println(i);System.out.println(d);System.out.println(s);}}Create new scanner object to read from keyboardInput an integerInput a doubleInput a string (up to white space)Include the definition of the Scanner utilityCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)11Scanner 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 sequence of characters up to next whitespace(space, carriage return, tab, etc.)nextLine() Returns sequence of characters up to next carriage returnReturns value of indicated type


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 2 2 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?