JAVAGoals for JavaGoals for Java (cont.)Java Virtual MachineHow it WorksJava combines benefits ofOther benefits of JavaThree levels of SecurityJava doesn’t haveJava and the WebJava classesInheritance in JavaJava coding conventionsClasses in JavaVariables in JavaA simple Java appletA simple Java applet (cont.)Slide 18Lifetime of an AppletPart of the Java Class HierarchySubclassing and InheritanceSubclassing ExampleInterfaces in JavaInterface ExampleInterface Example (cont.)Slide 26Abstract class v.s. Interface classExceptional ConditionsApproaches for Handling Exceptional ConditionsI. Each method handles its own exceptional conditionsSlide 31II. A low level class/method handles exceptional conditionsII. A low level class/method handles exceptional conditionsIII. Methods return status information so that client methods can respond to exceptional conditionsExceptions were added to languages to help with error processingA method can catch an exception and specialize its responseException Handling MechanismsException Handling Mechanisms (cont.)Exceptions in JavaHandling ExceptionsHandling multiple exceptionsTry/Catch StatementFinding Exception HandlersMultiple Levels of PropagationExplanationThrowing ExceptionsTypes of Java ExceptionsSummary of Exception HandlingConcurrent SystemThreads in JavaDefining the behavior of a threadDefining thread behavior: Subclassing ThreadDefining thread behavior: Implementing runnableWhen should you implement Runnable?Life Cycle of a Thread in JavaSynchronizing Threads in JavaSynchronizing Threads in Java (cont.)Producer/Consumer ExampleProducer/Consumer Example (cont.)Slide 60Concurrency SummaryDillon: CSE470: JAVA, EXCEPTIONS & THREADS 1JAVADeveloped at SUN by James Gosling with support from Bill JoyNet-based languageDescended from Oak»platform independent»object oriented»smallThe Java Tutorial: http://java.sun.com/docs/books/tutorialDillon: CSE470: JAVA, EXCEPTIONS & THREADS 2Goals for JavaSimple»easy to learn–based on C/C++»smallObject oriented»single inheritanceDistributed»libraries supply protocolsRobuststrongly typedsafe pointer modelSecurePlatform independentvirtual machinePortableno implementation dependent data typesDillon: CSE470: JAVA, EXCEPTIONS & THREADS 3Goals for Java (cont.)Compiled and interpretedMultithreadedDynamicDillon: CSE470: JAVA, EXCEPTIONS & THREADS 4Java Virtual MachineCompiler translates Java into J-codeStack-based virtual machine (JVM)No undefined or platform specific data typesOther languages can be translated to J-codeInterpreter is lightweight, easy to implement»use widely available language, like CJ-code is highly optimizedJ-code can be compiled on the flyDillon: CSE470: JAVA, EXCEPTIONS & THREADS 5How it WorksThe JVM may be»an interpreter»an applet viewer»a web browser»part of an OSClasses could be loaded from filesystem or over a networkJVMs use an environment variable, CLASSPATH, to find byte code (.class files) to executeJava sourceJava compiler (javac)J-code (JVM byte codes)Other classes Native codeJava interpreter (a JVM)Platform independente.g., AWTPlatform dependentDillon: CSE470: JAVA, EXCEPTIONS & THREADS 6Java combines benefits ofA strongly typed, OO languageFlexibility of an interpreted language»Lisp, Perl, TclA smalltalk virtual machine with security protection»Java byte code verifier reduces runtime checksPackage structure for organizing classes into subsystemsDillon: CSE470: JAVA, EXCEPTIONS & THREADS 7Other benefits of JavaException handlingSupport for multi-threading»Based on Hoare’s monitorsHighly optimized»Easy debugging–make debugging statements dependent on a constant value, which programmer sets when done debugging–compiler automatically removes unexecutable statementsDillon: CSE470: JAVA, EXCEPTIONS & THREADS 8Three levels of SecuritySecurity manager»controls access to system resources»highlight windows of untrusted applicationsClass loader»restricts classes loaded from network to interact with classes from the same locationVerifier»checks that incoming classes can’t forge pointers, violate access permissions, over/under flow operator stack, etc.»ensures type safetyDillon: CSE470: JAVA, EXCEPTIONS & THREADS 9Java doesn’t haveMacros and preprocessor»mostly used for platform dependenciesOperator overloading, except for +(Very many) automatic type coercionsPointer arithmetic»references are a higher level type and can only point to class objects, not to class methodsExplicit memory management »provides automatic garbage collectionDillon: CSE470: JAVA, EXCEPTIONS & THREADS 10Java and the WebA web browser can incorporate a JVM and run Java applets as executable codeLife of an applet»Loaded by a web browser and asked to initialize itself»Informed each time it is displayed and hidden»Informed when it is no longer neededSecurity manager prevents applet from accessing system resources or interacting with outside applicationsDillon: CSE470: JAVA, EXCEPTIONS & THREADS 11Java classesClass is the basic computation unit»encapsulates data and associated operations»found and loaded dynamically as needed22 architecture specific classes: “gateway to the real world”»networking, windowing, filesystem, etc.Rest of Java is written in JavaDillon: CSE470: JAVA, EXCEPTIONS & THREADS 12Inheritance in JavaSingle inheritance hierarchyMultiple inheritance of interfaces»Interface specifies the operations but not the implementations»A class can “implement” multiple interfacesDillon: CSE470: JAVA, EXCEPTIONS & THREADS 13Java coding conventionsClass names begin with upper case lettersVariables and method names begin with lower case lettersConstants are all upper caseSeparate words with uppercase letter instead of underscorese.g. aVariableName AClassName aMethodName ACONSTANTDillon: CSE470: JAVA, EXCEPTIONS & THREADS 14Classes in JavaDefine an abstract data type»operations called methods»data called variables or fieldsMany class instances or objects can exist»each instance may have a different state–e.g., different values for variables»all instances have the same methodsArranged in a hierarchy»each class has a unique superclass (parent)»subclass (child) can add or modify methods or variables of the superclassDillon: CSE470: JAVA, EXCEPTIONS & THREADS 15Variables in JavaMaintain state of a class
View Full Document