New version page

VASSAR CMPU 125 - Review of Java Fundamentals

Documents in this Course
Load more
Upgrade to remove ads
Upgrade to remove ads
Unformatted text preview:

Chapter 1Review of Java FundamentalsLecture 2Jenny WalterFall 2008Language Basics• Java application– Collection of classes• One class contains the main method = Point of entryfor program execution• Java programs can also be written asapplets, in which case they do not have amain methodClasses• Simply stated, a class defines a data type• Defines data fields and methods available forinstances of the class• An object in Java is an instance of a class• Class definition line includes– Optional access modifier (public or private)– Keyword class– Optional extends clause– Optional implements clause– Class body: The part inside the curly braces that containsall data fields and method definitionsClass Structurepackage helloprogram;/* * File: * ----------------------- * This program displays "hello, world" on the screen. */import*;import acm.program.*;public class HelloProgram extends GraphicsProgram { public HelloProgram() {this.start(); this.add( new GLabel("hello, world",100,75)); }}import statements (opt)constructorheader comment (opt)class definition linepackage name (opt)Class HierarchyEvery Java class is a subclass of either– Another Java class (uses keywordextends)– The Object class: Class at the root ofJava class hierarchy; every classextends Object implicitlynew operatorCreates an object or instance of a classPackages (aka libraries)Mechanism for grouping related classespackage statementType this statement outside class curly braces toindicate a class is part of a packageJava assumes all classes in a particularpackage are contained in the same directoryJava API consists of many predefinedpackagesImporting Packages• import statement– Allows use of classes contained in packageswithout the inconvenient “fully qualified name”– Included outside class curly braces, usually attop of file• Package java.lang is implicitly importedto all Java code• Other commonly imported packages include– java.util,, java.awt, javax.swing–, acm.programData Fields• Class members that are either variables orconstants• Declared within class curly braces, but notwithin the curly braces of any method.• Data field declarations can contain– Access modifiers (public, private, …)– Use modifiers (static)Local Variables• Declared within method curly braces.• Not visible (or usable) outside methodbraces.• Local variable declarations contain noaccess or use modifiers.• Never need to use a dot operator as aprefix to a local variable.Methods• Implement tasks• Declared within class curly braces• Each method should perform one well-defined taskand no more.• Method modifiers– Access modifiers and use modifiers• Void method– Returns nothing but has a side effect• Valued method– Returns a value– Body must contain return expression;– Return type must be declared in method definition(method header line)Side Effects• Any value returned from a method must beexplicitly specified in a return statement• Side effects include actions such as:– Input and Output (I/O)– Changing values of instance variables– Calling other methodsMethods• Syntax of a method declaration:access-modifier use-modifiers return-typemethod-name (formal-parameter-list){method-body}• Multiple methods of the same name can be defined– method overloading– methods with same names must have different number ortype of parameters• Primitive type arguments are passed by value– For objects and arrays, a reference value is copied insteadMethods• Syntax of a method call: <objectName or ClassName>.method-name(argument-list)• Method calls are analogous to messages being sentbetween objects or classes• Method calls should always be preceded by theobject or class receiving the message followed bythe "dot" operator• I encourage you to use the keyword "this" toprecede the dot operator when calling an inheritedmethod or an instance methodMethods• Constructor– Special kind of method– Has the same name as the class, no return type,no modifiers– Executed only when an object is created• A class can contain multiple constructors(overloaded constructors)How to Access public Membersof an Object• Data fields and methods not declared static– Use the object name, followed by a period,followed by member name• Data fields and methods declared static– Use the class name, followed by a period,followed by member namepublic vs. private• Public class members of Class X areaccessible by any class Y that has privilegeto access class X• Private class members are accessible onlywithin the class curly bracesStatic Class Members• Technically, data fields and methodsdeclared with use modifier static are"class members"• Static members are invoked independentlyof any instance of the class, using the classname followed by the dot operator, followedby the member nameComments• Comment line– Begins with two slashes (//)– Continues until the end of the line• Multiple-line comment– Begins with /* and ends with */– Useful for debugging; "commenting out" code– You can't nest multiple-line comments• javadoc comments– Begin with /** and end with */Identifiers and Keywords• Identifier– Sequence of letters, digits, underscores, anddollar signs (no other symbols allowed)– Must begin with either a letter or underscore– Used to name members of the program– Java distinguishes between uppercase andlowercase letters• Keywords– Java reserved identifiersNaming Conventions• keywords, variable names, and method names startwith a lower case letter and every word after thefirst is capitalized.• Class names differ only in that they start with acapital letter• Constants are written in all capital letters withunderscores separating words.• If you adhere to these naming conventions, yourcode will be much easier for other experiencedprogrammers to understandJava Keywords (Reserved Words)abstractbooleanbreakbytecasecatchcharclassconstcontinuedefaultdodoubleelseextendsfalsefinalfinallyfloatforgotoifimplementsimportinstanceofintinterfacelongnativenewnullpackageprivateprotectedpublicreturnshortstaticstrictfpsuperswitchsynchronizedthisthrowthrowstransienttruetryvoidvolatilewhileVariable• Name for a memory location• Contains a value of primitive type or a reference• Its name is a Java identifier• Declared by preceding variable name with datatypedouble radius; // radius of a sphereString

View Full Document
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...

Join to view Review of Java Fundamentals and access 3M+ class-specific study document.

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.


By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?