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)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)2CMSC 131 Spring 2010Jan Plane and Ben Bederson(adapted from Bonnie Dorr)1/21/20102Java 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)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)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/20103Output 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)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)8CMSC 131 Spring 2010Jan Plane and Ben Bederson(adapted from Bonnie Dorr)1/21/20104Holding 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)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;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/20105Built-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)Writing 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)1/21/20106Variables … … 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)Primitive 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
View Full Document