DOC PREVIEW
UMD CMSC 131 - Lecture Set 2: Starting Java

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 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 13 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 13 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 13 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1/21/20101Lecture Set 2:Starting Java1. Java Concepts2. Java Programming Basics3. User output4. Variables and types5. Expressions6. User input7. Uninitialized Variables0CMSC 131 Spring 2010Jan Plane and Ben Bederson(adapted from Bonnie Dorr)This Course: Intro to Procedural Programming using JavaWhy Java? Popular modern language Used in web, business, telecom applications Developed in 1990s, incorporates many features from earlier languages Object-orientation Garbage collection Portability of object code1CMSC 131 Spring 2010Jan Plane and Ben Bederson(adapted from Bonnie Dorr)1/21/20102Portability of Object Code? Object code is 2GL (assembly) / 1GL (machine code) Last time we said that 2GL / 1GL is architecture-specific How can Java have portable object code?Answer: Java Virtual Machine (JVM)2CMSC 131 Spring 2010Jan Plane and Ben Bederson(adapted from Bonnie Dorr)Java Virtual Machine Java includes definition of Java bytecode = “fake” machine code for Java Java compilers produce Java bytecode To run Java bytecode, must have bytecode interpreter (“Java Virtual Machine”) on client machine3.javaJavacompiler.classclientclientJVMJVMsource code object codeCMSC 131 Spring 2010Jan Plane and Ben Bederson(adapted from Bonnie Dorr)1/21/20103Facts about JVMs For efficiency, JVMs often compile bytecode into native machine code There are also “native” Java compilers (these compile Java directly to machine code)4CMSC 131 Spring 2010Jan Plane and Ben Bederson(adapted from Bonnie Dorr)Method Headers main is a method = “operation” Operations require operands = data to work on Operations return new data (result) Header gives information on form of operands, result for methodsFor main: Operand is collection of Strings Result is “void” (= unimportant) More later on “public”, “static”  Every program must have exactly one “main” method (where execution begins)5CMSC 131 Spring 2010Jan Plane and Ben Bederson(adapted from Bonnie Dorr)1/21/20104Output and Comments Output to console System.out.println System.out.print String Literals always use “quotation marks” Comments: explanations added by programmer ignored by the compiler read by other people looking at the code Two styles /* … */ // to end of line… Comments are essential for good programming!6CMSC 131 Spring 2010Jan Plane and Ben Bederson(adapted from Bonnie Dorr)Objects Bundles of data (“instance variables”) and methods (“functions”) Created using classes as “templates” We‟ll learn more later this semester7CMSC 131 Spring 2010Jan Plane and Ben Bederson(adapted from Bonnie Dorr)1/21/20105Java Program Organization Class Structure around which all Java programs are based A typical Java program consists of many classes Each class resides in its own file, whose name is based on the class‟s name The class is delimited by curly braces { … }.File name: Example1a.java:public class Example1a {… (contents of the class go here) …}A class consist of data (variables) and operations (methods)8CMSC 131 Spring 2010Jan Plane and Ben Bederson(adapted from Bonnie Dorr)Holding and calculating values variables declaration initialization assignment value use mathematical expressions calculated to take on a value based on values of literals and variables9CMSC 131 Spring 2010Jan Plane and Ben Bederson(adapted from Bonnie Dorr)1/21/20106Java Program Organization Methods Where most computation takes place Each method has a name, a list of arguments enclosed in (…), and body (collection of statements) in {…}public static void main( String[ ] args ) {… (contents of the main method go here) …} Variables Storage locations that program can operate on Variables can store data of different forms (integers, for example)int secondsPerMinute = 60; int minutesPerLecture = 50;10CMSC 131 Spring 2010Jan Plane and Ben Bederson(adapted from Bonnie Dorr)Java Program Organization Statements: Many different types Declarations – specify variable types (and optionally initialize)int x, y, z; // three integer variablesString s = “Howdy”; // a character string variableboolean isValid = true; // a boolean (true/false) variable Assignments – assign variables new valuesx = 13; Method invocation – call other methodsSystem.out.println( “Print this message“ ); Control flow – determine the order of statement execution. (These include if-then-else, while, do-while, for. More later.) Built-in Operators: For manipulating values (+, -, *, /, etc.) i.e. String Concatenation for output 11CMSC 131 Spring 2010Jan Plane and Ben Bederson(adapted from Bonnie Dorr)1/21/20107Built-in (Primitive) TypesType name Size (bytes)Integersbyte1short2int4long8Realsfloat4double8Otherchar2boolean112CMSC 131 Spring 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr)String Type Elements of String type are sequences of characters“abc” “Call me Ishmael” etc. String type is not built-in We will use it a lot Useful operation:  concatenation (+) “abc” + “def” is equivalent to “abcdef”13CMSC 131 Spring 2010Jan Plane and Ben Bederson(adapted from Bonnie Dorr)1/21/20108Writing Programs in Java1. EXPRESSIONS: computations that carry a value2. OPERATORS: symbols like +, *, -, etc.3. Statements end with a semicolon4. Types of statements:a) DECLARATION (where a variable is created)b) ASSIGNMENT (where a variable is given a value)c) METHOD INVOCATIONS (where another method is called)d) others - later5. You can put blank lines in almost anytime you want1. except not in the middle of an identifier or a keyword2. and except not in a set of quotation marks6. Proper indenting helps readability14CMSC 131 Spring 2010Jan Plane and Ben Bederson(adapted from Bonnie Dorr)Variables … … 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.15Variable Valuex 5CMSC 131 Spring 2010Jan Plane and Ben Bederson(adapted from Bonnie Dorr)1/21/20109Primitive 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


View Full Document

UMD CMSC 131 - Lecture Set 2: Starting Java

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 2: Starting Java
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 2: Starting Java 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 2: Starting Java 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?