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 languageUsed in web, business, telecom applicationsDeveloped in 1990s, incorporates many features from earlier languagesObject-orientationGarbage collectionPortability 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-specificHow can Java have portable object code?Answer: Java Virtual Machine (JVM)2CMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)3Java Virtual MachineJava includes definition of Java bytecode = “fake” machine code for JavaJava compilers produce Java bytecodeTo 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 JVMsFor efficiency, JVMs often compile bytecodeinto native machine codeThere are also “native” Java compilers (these compile Java directly to machine code)CMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)5Method Headersmain is a method = “operation”Operations require operands = data to work onOperations return new data (result)Header gives information on form of operands, result for methodsFor main:Operand is collection of StringsResult 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 CommentsOutput to consoleSystem.out.printlnSystem.out.printString Literals always use “quotation marks”Comments: explanations added by programmerignored by the compilerread by other people looking at the codeTwo styles/* … */// to end of line…Comments are essential for good programming!CMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)7ObjectsBundles 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 OrganizationClassStructure around which all Java programs are basedA typical Java program consists of many classesEach class resides in its own file, whose name is based on the class’s nameThe 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)9example1bvariablesdeclarationinitializationassignmentvalue usemathematical expressionscalculated to take on a valuebased on values of literals and variablesCMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)10Java Program OrganizationMethodsWhere most computation takes placeEach 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) …}VariablesStorage locations that program can operate onVariables 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 OrganizationStatements: Many different typesDeclarations – 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) variableAssignments – 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 TypeElements of String type are sequences of characters“abc” “Call me Ishmael” etc.String type is not built-in We will use it a lotUseful 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 locationsRecall that memory is a sequence of bitsQuestion: 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 ConstantsConstants are also called literalsInteger types:byteshort optional sign and digits (0-9): 12 -1 +234 0
View Full Document