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 languageUsed in web, business, telecom applicationsDeveloped in 1990s, incorporates many features from earlier languagesObject-orientationGarbage collectionPortability 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-specificHow can Java have portable object code?Answer: Java Virtual Machine (JVM)CMSC 131 Spring 2007Jan 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 2007Jan 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 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 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)CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)7Comments? Comments: explanations added by programmerTwo styles/* … */// to end of line…Comments are essential for good programming!CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)8ObjectsBundles 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 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)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 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 2007Jan Plane (adapted from Bonnie Dorr)12Java 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.)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 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 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