DOC PREVIEW
UMD CMSC 131 - Lecture 3: Starting Java

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

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

Unformatted text preview:

9/6/2006 CMSC 131 Fall 2006Rance Cleaveland©2006 Univeristy of MarylandLecture 3:Starting JavaLast time:1. Programming languages2. Eclipse and CVSToday:1. CVS and project submission2. Basics of Java ProgramsCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland1This 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 Fall 2006Rance Cleaveland©2006 University of Maryland2Portability 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 Fall 2006Rance Cleaveland©2006 University of Maryland3Java 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 Fall 2006Rance Cleaveland©2006 University of Maryland4Facts 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 Fall 2006Rance Cleaveland©2006 University of Maryland5/* This is a very basic Java programto get things started. */public class Example1 {// main is where the program startspublic static void main(String args[]) {int secondsPerMinute = 60;int minutesPerLecture = 50;int totalSeconds = secondsPerMinute * minutesPerLecture;System.out.println("There are " + totalSeconds+ " seconds in a lecture.");}}A Sample Java ProgramExample1.javaCommentsClass declarationCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland6Comments? Class? Comments: explanations added by programmer Two styles /* … */ // to end of line… Comments are essential for good programming! Classes Classes are key components of Java programs Class: a collection of data and associated operations data: “instance variables” operations: “methods”One method in this program: “main”CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland7/* This is a very basic Java programto get things started. */public class Example1 {// main is where the program startspublic static void main(String args[]) {int secondsPerMinute = 60;int minutesPerLecture = 50;int totalSeconds = secondsPerMinute * minutesPerLecture;System.out.println("There are " + totalSeconds+ " seconds in a lecture.");}}Java Program (cont.)Example1.javaMethod headerStatementsCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland8Method 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 Fall 2006Rance Cleaveland©2006 University of Maryland9Statements? Basic commands in a program Two kinds of statements in this program Variable declarationsint secondsPerMinute = 60; Introduce a named storage location (“secondsPerMinute”) Define form of data that can be stored (“int” = integer) (Optional) Give initial value (“60”) Method invocationSystem.out.println("There are" ...); Calls to methods in objects = “class instances” Method is “println” in subobject “out” within object “System”CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland10Objects? Bundles of data (“instance variables”) and methods Created using classes as “templates” We’ll learn more later this semesterCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland11Executing the Program/* This is a very basic Java programto get things started. */public class Example1 {// main is where the program startspublic static void main(Stringargs[]) {int secondsPerMinute = 60;int minutesPerLecture = 50;int totalSeconds = secondsPerMinute* minutesPerLecture;System.out.println("There are " + totalSeconds+ " seconds in a lecture.");}}ValueVariablesecondsPerMinute 60minutesPerLecture 50totalSeconds 3000There are 3000 seconds in a lecture.CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland12Java 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 Example1 {… (contents of the class go here) …}A class consist of data (variables) and operations (methods)CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland13Java 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 Fall 2006Rance Cleaveland©2006 University of Maryland14Java 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 Fall 2006Rance Cleaveland©2006 University of Maryland15Types Two references to the “form” of values Method headers Variables Type: the form of


View Full Document

UMD CMSC 131 - Lecture 3: 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 3: 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 3: 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 3: 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?