Java 1 - 3-8

Embed Size (px)

Citation preview

  • 7/27/2019 Java 1 - 3-8

    1/7

    Accessibility ModifiersAre used with the definition of a class, it's methods,

    instance and class variables to specify which other

    objects can access those types. E.g. ' public class Jim {

    private int a; public int getA() { return a; } }'

    Accessor Methods Are used to request information from an object wheninvoked (e.g. values of instance variables), but should

    not change the objects state.

    Actual Arguments Term used to refer to the actual valuespassed as arguments to a method when it is

    invoked on an object. E.g. 'abird.fly(10, 12);'

    Assembly Code Uses several letters (mnemonic symbols) to representregisters and opcodes, rather than raw machine code

    which is entirely numeric. More human readable and can

    help with understanding / memory during development.

    Assignment Statement Gives values to variables in the form 'variable = expression;'.Expression must deliver a value that can be stored in the variable(otherwise, compiler error). The '=' in Java does not represent

    equivalence but is an assignment operator.

    boolean(George Boole - Mathematical Logic) primitive data type that only

    has two possible values, true or false, which can be the result of a

    logical expression. Often used with accessor functions that are

    named as if they ask a question, e.g. 'public boolean isFlying() {

    return false; }' .

    bytecode Java compiles source code (.java) to itsintermediate generic machine code called

    bytecode (.class).

    Class Extension Extending a class is part of a strategy to increase codere-use and reduce the amount of code to be

    maintained (don't repeat yourself).

    Class HierarchyDiagram showing inheritance relationships between

    classes. In UML classes are shown as named boxes

    with subclasses showing lines with arrows pointing to

    their superclass.

    Class Naming Convention Start with upper-case letter, then lower-case. However, each newword within the class name should start with and upper-case letter(camel). E.g. 'JonAttribute'. Conceptually classes are usually

    named using one or more nouns...because they define objects !.

  • 7/27/2019 Java 1 - 3-8

    2/7

  • 7/27/2019 Java 1 - 3-8

    3/7

    Identifiers and case-sensitivity Java identifiers are case-sensitive. So thisshould b taken into account when naming

    identifiers.

    Information HidingWhere access to object instance variables is restricted by only allowi ng the use of methods whichrestrict use of an object to how the author intended rather than it being able to ma ke use ofimplementation details. I n this way, a maintai ning programmer ca n change the internals of aclass (e.g. instance varia bles, method bodies)without changing the external interface(method names and arg uments) and even add to it !. Changes are therefore loca lised and the rest

    of the software system that uses the class ca n remain as i t was.

    InheritanceA relationship between classes and a key part of OOs approach to code re-use

    An inheriting class will be similar to it's super-class but is effectively extending

    and specialising the super-classes data structure and possible operations. It

    can add data to that inherited from the super-class, as well as adding to and

    modifying inherited methods from the super-class.

    Instance variablesStandard term for variable that belong to each instance of an object

    sometimes also ca lled member variables. These are declared within

    a class definition. These are opposed to local variables declared

    within a method body, or class variables which are static and

    belong to the class rather than being instantiated for each object.

    InterpreterAn interpreter can be written, relatively easily, for

    any platform - it translates bytecode to native

    machine code at run-time so that it can be executed

    on that computer.

    Invoking a method Whereby java code requests that a given operation(ie. method) is performed on a given object which is

    then executed. E.g. 'object.aMethod();'

    Invoking a Method on a Reference

    Variable

    It is possible to invoke a method on a given object instance of a

    class by using a reference variable assigned to that object and a '.'.

    E.g. 'abird.fly();' would invoke the method 'fly' on the object

    referenced by 'abird'. Note that this is possible because the method

    is defined by the objects class type, or one of i t's super-classes.

    Java AppletsJava pr ograms that can be run within a java-enabled browser by embedding them in HTML(which references the Applet ex ecutable). Were i nitiall y used to ma ke dynamic web content(HTML is la rgely about displaying passi ve data, forms aside). Actuall y download and run theapplica tion over a network rather than having to install it. Are often digitally signed (to verifywho wrote them - security) or run in a sandbox environment so that they can't access or changethe host computer (security).

    Java Applications Standalone java programs which require the JRE(Java Runtime Environment), as opposed to Applets

    which require a Java-enabled browser.

    Java Class A structure that defines both the stored data and program code thatis associated with a group of similar objects (common properties).Ie. common state structure (not state itself, the values can vary)

    and operations used by a group of objects.

  • 7/27/2019 Java 1 - 3-8

    4/7

    Java DeclarationSpecify the type and then the identifier(s). E.g. 'int a, b, c;'. This

    causes the Java system to allocate 3 memory locations and could

    either be undefined (locals) or set to their default (instance

    variables). Declarations can be combined with assignment

    statements (see later).

    JDK Java (Software) Development Kit. Containscompiler, interpreter, javadoc processor,

    debugger, disassembler, appletviewer.

    Keyword 'class' Used to define a class. Roughly of the form 'class[Identifier] { [Data declarations] [Method Definitions] }'.

    Note that data can be declared and not defined (in this

    case it will take default values ie. null/zero).

    Keyword 'extends' Used to define an inheritance relationship between asubclass and its superclass. In the form 'class A

    extends B {}' when the class is defined.

    Keyword 'new'Used to create an object instance for a given class. E.g. 'Bird abird =

    new Bird();' will a llocate memory for the data required by an object

    of class type 'Bird'. this statement then assigns the address of this

    data to the reference variable 'abird'. In this way, 'abird' references

    the new Bird object.

    Keyword 'return'Used within the body of a method that returns a value. Therefore it mus

    be followed by an expression that returns a value that is of the type

    declared in the method header. E.g. 'public int birdWeight() { return

    birdWeight; }' would be fine, as long as the variable identifier

    'birdWeight' was declared with the primitive data type 'int'.

    Keywords and case-sensitivity Java keywords are always in lower-

    case, e.g. 'class, for, public, switch'.

    main methodThe entry/starting point for a J ava prog ram a nd defined in the manifest. Clearly needed becauseobject methods need to be invoked from somewhere. Hence a cla ss is defined that has a mainmethod, which will be the first method to be called. Of the form 'publi c static void mai n (String[]args) {}'. Note that the method is static, it is a cl ass method. Ie. an object of that class doesn't neeto exist. The String a rray contains the command line a rguments passed by the OS.

    Method ArgumentsUsed to supply information as part of the request for

    an operation to be invoked on an object. Are

    contained within brackets, e.g 'object.aMethod('a', 1)'.

    Methods can have any(?) number of arguments.

    Method Naming Convention Similar to variable, lower-case for first letter, thenupper-case for start of new words. Note that they

    usually start with a verb. E.g. 'increaseSpeed'.

  • 7/27/2019 Java 1 - 3-8

    5/7

    Method Return ValuesDefine what type of value a method will return. Can be set to the

    keyword 'void' if the method doesn't return a value. Usually of the

    form '[accessibility modifier] [return-type] [method-name] (

    [arguments] )'. Followed by either a ';' for an abstract method, or

    the method body, enclosed curly-bracket delimiters '{... }'.

    Method Signature Includes the name of the method along with thenumber, order and type of its arguments. Basically

    what is required to invoke the method.

    Multiple Inheritance Not possible in Java or C# as opposed toC++. Can be approximated by the use of

    'Interfaces' and inner-classes.

    Mutator MethodA type of method which, when invoked on an object,

    will change the objects state (ie. the value of the data

    associated with the object that defines it - instance

    variables).

    OverloadingWhere classes in a hierarchy may have more than one method with

    the same name although their signatures must vary by the number

    and/or types of its arguments. The method implementation

    chosen to execute therefore depends on the types given on

    invocation.

    Overloading and Return Type A method can't be overloaded by having two methods with the samargument pattern and name but different return types. At the pointof invocation, this will not be enough information for the compiler

    to decide which method to invoke. Compile error !.

    Overloading with different argument

    pattern AND different return type.

    For most programming languages that support

    method overloading (Java, C#, C++, ...), if the

    parameter types are different, then the return types

    can also be different.

    OverridingWhere subclass can modify the body of the methods that it inherits

    from its superclass (but not the method signatures, which should

    remain the same, including the return type). Simply involves re-

    defining the method in the sub-class using the same signature and

    return type.

    Pattern of arguments Describes the number and type of formal argumentsdefined, or actual arguments used. Pattern depends

    on the number and type of arguments (and order).

    Portability Describes the way that one program can run on manydifferent platforms. Could be ported by hand, re-

    compiled for each platform, or interpreted to native-

    code at run-time.

  • 7/27/2019 Java 1 - 3-8

    6/7

    Primitive Data TypesSimple types such as integer, floating point, boolean,

    character as opposed to complex 'object' types which

    may be made up of many primitive types and even

    other objects.

    Reference VariablesDeclared by using any cla ss identifier, these can be used to refer to a n object instancebut aren'tobjects in themselves but references (internally they contain memory addresses to the objectslocation, something like smar t pointers and are involved in ref-counting for garba ge collection)E.g. 'Bird abird;' would allocated a memory location for a reference to an object of class type'Bird'. To define the reference variabl e we need to assign it to an i nstantiated object (either an

    existing object, or in combination wi th the 'new' operator to create a new object).

    StateRefers to the value of data associated with an object

    at any one point in time. Object state can be defined

    by it's instance variables (and class variables.?). State

    can change.

    State change In OO, usually an objects state shouldonly be changed by invoking a method.

    SubclassTerm used to refer to a class further down the inheritance hierarchy

    from the inherited class (super-class). Ie. used to refer to the class

    that is inheriting. In UML, the arrow would point from the sub-

    class to its inherited super-class. Subclasses extend their

    superclass.

    Superclass Term used to refer to a class further up the inheritancehierarchy from an inheriting class (sub-class). In UML th

    super-class is pointed to by its inheriting sub-classes.

    Superclasses are extended by their subclasses.

    Superclass ConstructorThe most basic situation for this is that a superclass

    constructor is called automatically before the

    subclass constructor. (there are ways of having finer

    control overwhich constructor).

    superclass constructorIf a constructor does not explicitly invoke a superclass constructor, the

    Java compiler automatically inserts a call to the no-argument constructor

    of the superclass. If the super class does not have a no-argument

    constructor, you will get a compile-time error. Object does have such a

    constructor, so if Object i s the only superclass, there is no problem.

    this and constructors It is possible to call one constructor from anotherwithin a class using 'this(..);' but it must be at the

    beginning of the constructor.

    UML Unified Modelling Language

  • 7/27/2019 Java 1 - 3-8

    7/7

    Using return valuesWhen invoking a method with a return value on an object, the

    return value will be discarded once the statement is complete unless

    it is assigned to a variable of the appropriate type. For example 'int

    birdResult = abird.birdWeight();' would assign the value of the

    method return value to the variable identified by 'birdResult;'.

    Variable Naming Convention Start with a lower-case letter but then useupper-case for the start of any new word

    in the name (camel). E.g. 'speedMeasure'