DOC PREVIEW
UMBC CMSC 341 - Java

This preview shows page 1-2-15-16-31-32 out of 32 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC 341Sun’s Naming ConventionsCommentsThe final modifierPackagesPackages (cont.)Slide 7Slide 8Fields and MethodsAccess ControlAccess Control for ClassesClassesClasses (cont.)Instance and Local VariablesStatic VariablesInstance vs. Static MethodsPass By Value or By Reference?ConstructorsConstructors (cont.)Expressions and Control FlowControl Flow Constructs (cont.)Slide 22Example ClassExample Class (cont.)Slide 25Slide 26Example Driver ProgramExceptionsException HierarchyHandling the Exception ExamplePassing up the ExceptionPassing up the Exception ExampleCMSC 341Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions8/3/2007UMBC CMSC 341 Java 22Sun’s Naming ConventionsClasses and InterfacesStringBuffer, Integer, MyDateIdentifiers for methods, fields, and variables_name, getName, setName, isName, birthDatePackagesjava.lang, java.util, proj1ConstantsPI, MAX_NUMBER8/3/2007UMBC CMSC 341 Java 23CommentsJava supports three types of comments.C style /* multi-liner comments */C++ style // one liner commentsJavadoc/**This is an example of a javadoc comment. These comments can be converted to part of the pages you see in the API.*/8/3/2007UMBC CMSC 341 Java 24The final modifierConstants in Java are created using the final modifier.final int MAX = 9;Final may also be applied to methods in which case it means the method can not be overridden in subclasses.Final may also be applied to classes in which case it means the class can not be extended or subclassed as in the String class.8/3/2007UMBC CMSC 341 Java 25PackagesOnly one package per file.Packages serve as a namespace in Java and create a directory hierarchy when compiled.Classes are placed in a package using the following syntax in the first line that is not a comment.package packagename;package packagename.subpackagename;8/3/2007UMBC CMSC 341 Java 26Packages (cont.)Classes in a package are compiled using the –d option. On the following slide, you will find the command to compile the code from the Proj1/src directory to the Proj1/bin directory.8/3/2007UMBC CMSC 341 Java 27Packages (cont.)It is common practice to duplicate the package directory hierarchy in a directory named src and to compile to a directory named bin.javac –d ../bin proj1/gui/Example.javaProj1proj1proj1srcbinguiguiExample.javaExample.classThe following command is run from the src directory:8/3/2007UMBC CMSC 341 Java 28Packages (cont.)By default, all classes that do not contain a package declaration are in the unnamed package.The fully qualified name of a class is the packageName.ClassName.java.lang.StringTo alleviate the burden of using the fully qualified name of a class, people use an import statement found before the class declaration.import java.util.StringBuffer;import java.util.*;8/3/2007UMBC CMSC 341 Java 29Fields and MethodsIn Java you have fields and methods. A field is like a data member in C++.Method is like a member method in C++. Every field and method has an access level. The public, private, and protected keywords have the same functionality as those in C++.publicprotectedprivate (package)8/3/2007UMBC CMSC 341 Java 210Access ControlprivatedefaultprotectedpublicModifierSame classSame packageSubclassUniverse8/3/2007UMBC CMSC 341 Java 211Access Control for ClassesClasses may have either public or package accessibility.Only one public class per file.Omitting the access modifier prior to class keyword gives the class package accessibility.8/3/2007UMBC CMSC 341 Java 212ClassesIn Java, all classes at some point in their inheritance hierarchy are subclasses of java.lang.Object, therefore all objects have some inherited, default implementation before you begin to code them.String toString()boolean equals(Object o)8/3/2007UMBC CMSC 341 Java 213Classes (cont.)Unlike C++ you must define the accessibility for every field and every method. In the following code, the x is public but the y gets the default accessibility of package since it doesn’t have a modifier.publicint x;int y;8/3/2007UMBC CMSC 341 Java 214Instance and Local VariablesUnlike C++ you must define everything within a class. Like C++, variables declared outside of method are instance variables and store instance or object data. The lifetime of the variable is the lifetime of the instance. variables declared within a method, including the parameter variables, are local variables. The lifetime of the variable is the lifetime of the method.8/3/2007UMBC CMSC 341 Java 215Static VariablesA class may also contain static variables and methods.Similar to C++…Static variables store static or class data, meaning only one copy of the data is shared by all objects of the class.Static methods do not have access to instance variables, but they do have access to static variables.Instance methods also have access to static variables.8/3/2007UMBC CMSC 341 Java 216Instance vs. Static MethodsStatic methodshave static as a modifier,can access static data,can be invoked by a host object or simply by using the class name as a qualifier.Instance methodscan access static data,can access instance data of the host object,must be invoked by a host object,contain a this reference that stores the address of host object.8/3/2007UMBC CMSC 341 Java 217Pass By Value or By Reference?All arguments are passed by value to a method. However, since references are addresses, in reality, they are passed by reference, meaning…Arguments that contain primitive data are passed by value. Changes to parameters in method do not effect arguments.Arguments that contain reference data are passed by reference. Changes to parameter in method may effect arguments.8/3/2007UMBC CMSC 341 Java 218ConstructorsSimilar to C++, Java will provide a default (no argument) constructor if one is not defined in the class.Java, however, will initialize all fields (object or instance data) to their zero values as in the array objects.Like C++, once any constructor is defined, the default constructor is lost unless explicitly defined in the class.8/3/2007UMBC CMSC 341 Java 219Constructors (cont.)Similar to C++, constructors in Javahave no return value,have the same name as the class,initialize the data,and are typically overloaded.Unlike C++, a Java constructor can call another constructor using a call to a this method as the first line of


View Full Document

UMBC CMSC 341 - Java

Download Java
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 Java 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 Java 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?