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

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

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

Unformatted text preview:

1 21 2010 Lecture Set 2 Starting Java 1 2 3 4 5 6 7 Java Concepts Java Programming Basics User output Variables and types Expressions User input Uninitialized Variables CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 0 This Course Intro to Procedural Programming using Java Why 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 code CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 1 Portability 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 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 2 1 1 21 2010 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 machine JVM source code object code client java Java compiler class CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr JVM client 3 Facts 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 CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 4 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 methods For 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 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 5 2 1 21 2010 Output 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 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 6 Objects Bundles of data instance variables and methods functions Created using classes as templates We ll learn more later this semester CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 7 Java 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 CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 8 3 1 21 2010 Holding and calculating values variables declaration initialization assignment value use mathematical expressions calculated to take on a value based on values of literals and variables CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 9 Java 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 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 10 Java Program Organization Statements Many different types Declarations specify variable types and optionally initialize int x y z three integer variables String s Howdy a character string variable boolean isValid true a boolean true false variable Assignments assign variables new values Method invocation call other methods Control flow determine the order of statement execution These include if then else while do while for More later x 13 System out println Print this message Built in Operators For manipulating values etc i e String Concatenation for output CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 11 4 1 21 2010 Built in Primitive Types Integers Reals Other Type name byte short int long float double char boolean Size bytes 1 2 4 8 4 8 2 1 CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 12 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 CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 13 Writing Programs in Java EXPRESSIONS computations that carry a value OPERATORS symbols like etc Statements end with a semicolon Types of statements 1 2 3 4 a b c d You can put blank lines in almost anytime you want 5 1 2 6 DECLARATION where a variable is created ASSIGNMENT where a variable is given a value METHOD INVOCATIONS where another method is called others later except not in the middle of an identifier or a keyword and except not in a set of quotation marks Proper indenting helps readability CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 14 5 1 21 2010 Variables are named storage locations Variable x Value 5 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 type specifying how much storage to allocate CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 15 Primitive Data Types In Detail Integer Types byte 1 byte short 2 bytes int 4 bytes long 8 bytes Floating Point Types float 4 bytes double 8 bytes Other types boolean 1 byte char 2 bytes Range 128 to 127 Range 32 000 to 32 000 Range 2 billion to 2 billion Range 9 quintillion to 9 quintillion 3 4x1038 to 3 4x1038 7 digits of precision 1 7x10308 to 1 7x10308 15 digits of prec true false A single Unicode character CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 17 Primitive Type Literals Constants are also called literals Integer types byte short int long optional sign and digits 0 9 12 1 234 0 1234567 Same as above but followed by L or l 1394382953L Floating point types Avoid this lowercase L It looks too much like the digit 1 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 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?