VASSAR CMPU 102 - Review of Java Fundamentals

Unformatted text preview:

Chapter 1Program StructurePackagesSlide 4Slide 5ClassesSlide 7Slide 8Data FieldsSlide 10MethodsMethod ModifiersSlide 13Slide 14How to Access Members of an ObjectLanguage BasicsCommentsIdentifiers and KeywordsVariablesPrimitive Data TypesSlide 21Slide 22ReferencesLiteral ConstantsSlide 25Named ConstantsAssignments and ExpressionsSlide 28Slide 29Assignments and ExpressionsSlide 31Slide 32ArraysSlide 34Slide 35Slide 36Slide 37Selection StatementsSlide 39Iteration StatementsSlide 41Slide 42Useful Java ClassesSlide 44Slide 45Slide 46Slide 47Java ExceptionsCatching ExceptionsSlide 50Slide 51Slide 52Slide 53Throwing ExceptionsText Input and OutputInputSlide 57Slide 58OutputSlide 60Slide 61File Input and OutputText FilesSlide 64Slide 65Slide 66Slide 67Slide 68Slide 69Slide 70Object SerializationSlide 72SummarySlide 74Slide 75Slide 76© 2006 Pearson Addison-Wesley. All rights reserved 1-1Chapter 1Review of Java Fundamentals© 2006 Pearson Addison-Wesley. All rights reserved 1-2Program Structure•Typical Java program consists of–User written classes–Java Application Programming Interface (API) classes•Java application–Has one class with a main method•Java program basic elements:–Packages–Classes–Data fields–Methods© 2006 Pearson Addison-Wesley. All rights reserved 1-3Packages•Provide a mechanism for grouping related classes•package statement–Indicates a class is part of a package•Java assumes all classes in a particular package are contained in same directory•Java API consists of many predefined packages© 2006 Pearson Addison-Wesley. All rights reserved 1-4Packages•import statement–Allows you to use classes contained in other packages•Package java.lang is implicitly imported to all Java code© 2006 Pearson Addison-Wesley. All rights reserved 1-5PackagesFigure 1-1Figure 1-1A simple Java Program© 2006 Pearson Addison-Wesley. All rights reserved 1-6Classes•Data type that specifies data and methods available for instances of the class•An object in Java is an instance of a class•Class definition includes–Optional subclassing modifier–Optional access modifier–Keyword class–Optional extends clause–Optional implements clause–Class body© 2006 Pearson Addison-Wesley. All rights reserved 1-7Classes•Every Java class is a subclass of either–Another Java class–Object class•new operator–Creates an object or instance of a class© 2006 Pearson Addison-Wesley. All rights reserved 1-8ClassesFigure 1-2Figure 1-2Components of a class© 2006 Pearson Addison-Wesley. All rights reserved 1-9Data Fields•Class members that are either variables or constants•Data field declarations can contain–Access modifiers–Use modifiers–Modules© 2006 Pearson Addison-Wesley. All rights reserved 1-10Data FieldsFigure 1-3Figure 1-3Modifiers used in data field declarations© 2006 Pearson Addison-Wesley. All rights reserved 1-11Methods•Used to implement operations•Should perform one well-defined task•Method modifiers–Access modifiers and use modifiers•Valued method–Returns a value–Body must contain return expression;© 2006 Pearson Addison-Wesley. All rights reserved 1-12Method ModifiersFigure 1-4Figure 1-4Modifiers used in a method declaration© 2006 Pearson Addison-Wesley. All rights reserved 1-13Methods•Syntax of a method declarationaccess-modifier use-modifiers return-typemethod-name (formal-parameter-list) {method-body}•Arguments are passed by value–Except for objects and arrays•A reference value is copied instead•Java 1.5 allows a method to have a variable number of arguments of the same type–Using the ellipses (three consecutive dots)© 2006 Pearson Addison-Wesley. All rights reserved 1-14Methods•Constructor–Special kind of method–Has the same name as the class and no return type–Executed only when an object is created•A class can contain multiple constructors© 2006 Pearson Addison-Wesley. All rights reserved 1-15How to Access Members of an Object•Data fields and methods declared public–Name the object, followed by a period, followed by member name•Members declared static–Use the class name, followed by a period, followed by member name© 2006 Pearson Addison-Wesley. All rights reserved 1-16Language Basics•Java application–Collection of classes•One class contains the main method•Java programs can also be written as applets© 2006 Pearson Addison-Wesley. All rights reserved 1-17Comments•Comment line–Begins with two slashes (//)–Continues until the end of the line•Multiple-line comment–Begins with /* and ends with */–Useful for debugging–Cannot contain another multiple-line comment•javadoc comments–Begins with /** and ends with */© 2006 Pearson Addison-Wesley. All rights reserved 1-18Identifiers and Keywords•Identifier–Sequence of letters, digits, underscores, and dollar signs–Must begin with either a letter or underscore–Used to name various parts of the program–Java distinguishes between uppercase and lowercase letters•Keywords–Java reserved identifiers© 2006 Pearson Addison-Wesley. All rights reserved 1-19Variables•Represents a memory location•Contains a value of primitive type or a reference•Its name is a Java identifier•Declared by preceding variable name with data typedouble radius; // radius of a sphereString name; // reference to a String object© 2006 Pearson Addison-Wesley. All rights reserved 1-20Primitive Data Types•Organized into four categories–Boolean–Character–Integer–Floating point•Character and integer types are called integral types•Integral and floating-point types are called arithmetic types© 2006 Pearson Addison-Wesley. All rights reserved 1-21Primitive Data TypesFigure 1-5Figure 1-5Primitive data types and corresponding wrapper classes© 2006 Pearson Addison-Wesley. All rights reserved 1-22Primitive Data Types•Value of primitive type is not considered an object•java.lang provides wrapper classes for each of the primitive types•Autoboxing–Automatically converts from a primitive type to the equivalent wrapper class•Auto-unboxing–Reverse process© 2006 Pearson Addison-Wesley. All rights reserved 1-23References•Data type used to locate an object•Java does not allow programmer to perform operations on the reference value•Location of object in memory can be assigned to a reference variable© 2006 Pearson Addison-Wesley. All rights reserved 1-24Literal Constants•Indicate particular values within a


View Full Document

VASSAR CMPU 102 - Review of Java Fundamentals

Download Review of Java Fundamentals
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 Review of Java Fundamentals 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 Review of Java Fundamentals 2 2 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?