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

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

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

Unformatted text preview:

1CMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)Lecture Set 2:Starting Java1.Java Concepts2.Java Programming Basics3.User output4.Variables and types5.Expressions6.User inputCMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)1This 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 codeCMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)2Portability 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 2008Jan Plane (adapted from Bonnie Dorr)3Java 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 machine.javaJavacompiler.classclientclientJVMJVMsource code object codeCMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)4Facts about JVMsFor efficiency, JVMs often compile bytecodeinto native machine codeThere are also “native” Java compilers (these compile Java directly to machine code)CMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)5Method 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)3CMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)6Output 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!CMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)7ObjectsBundles of data (“instance variables”) and methods (“functions”)Created using classes as “templates”We’ll learn more later this semesterCMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)8Java 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: Example1.java:public class Example1a {… (contents of the class go here) …}A class consist of data (variables) and operations (methods)4CMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)9example1bvariablesdeclarationinitializationassignmentvalue usemathematical expressionscalculated to take on a valuebased on values of literals and variablesCMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)10Java 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;CMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)11Java 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 output5CMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)12Built-in (Primitive) TypesOtherRealsIntegers1boolean2char8double4float8long4int2short1byteSize (bytes)Type nameCMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)13String 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” = “abcdef”CMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)14Writing 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 anytime you want6.Proper indenting helps readability6CMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)15Variables …… 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 2008Jan Plane (adapted from Bonnie Dorr)16Recall Java Built-in TypesOtherRealsIntegers1boolean2char8double4float8long4int2short1byteSize (bytes)Type nameCMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)17Primitive 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) character7CMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)18Primitive-Type ConstantsConstants are also called literalsInteger types:byteshort optional sign and digits (0-9): 12 -1 +234 0


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?