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

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

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

Unformatted text preview:

CMSC 131 Spring 2007Bonnie Dorr (adapted from Rance Cleaveland)Lecture Set 2:Starting JavaThis set has Java Concepts and Java Programming BasicsCMSC 131 Spring 2007Jan 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 2007Jan 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)CMSC 131 Spring 2007Jan 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 2007Jan 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 2007Jan Plane (adapted from Bonnie Dorr)5example1a/* This is a very basic Java program to get things started. * Notice the difference between println and print * The things inside the quotation marks are called "String literals" */public class Example1a {public static void main (String args[]) { //where the program startsSystem.out.println("Hello World!");System.out.print("Or maybe I should say: ");System.out.println("Goodbye World!");}}CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)6Method 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)CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)7Comments? Comments: explanations added by programmerTwo styles/* … */// to end of line…Comments are essential for good programming!CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)8ObjectsBundles of data (“instance variables”) and methods (“functions”)Created using classes as “templates”We’ll learn more later this semesterCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)9Java 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)CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)10example1b/* Have you ever noticed that your dryer eats your socks? * This example illustrates variables and the assignment* operator (=) * Note that each variable is "declared" just ONCE! */public class Example1b {public static void main(String args[]) {int numberOfSocks;numberOfSocks = 27; // An odd number of socks??? Crazy.System.out.print("The number of socks at the beginning is: ");System.out.println(numberOfSocks);System.out.println("OK, I'm going to put them in the dryer...");int socksLostInDryer = numberOfSocks / 3; // I lose about a third each timenumberOfSocks = numberOfSocks - socksLostInDryer;System.out.print("The number of socks lost was: ");System.out.println(socksLostInDryer);System.out.print("The number of socks is now: "); System.out.println(numberOfSocks);}}CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)11Java 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 2007Jan Plane (adapted from Bonnie Dorr)12Java 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.)CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)13example1c/* This example illustrates "concatenation" of strings, and* shows how Java automatically converts values into strings */public class Example1c {public static void main(String[] args) {System.out.println("My " + "name " + "is " + "Fred.");int secondsPerMinute = 60;int minutesPerLecture = 50;int secondsPerLecture = secondsPerMinute * minutesPerLecture;System.out.println("There are " + secondsPerLecture + " seconds in a lecture.");}}CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)14Built-in (Primitive) TypesOtherRealsIntegers1boolean2char8double4float8long4int2short1byteSize (bytes)Type nameCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)15String 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 2007Jan Plane (adapted from Bonnie Dorr)16Example 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


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?