JAVA Programming - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

JAVA Programming

Description:

Names are based on reverse URLs and are written in lowercase. ... Static dogCount variable is set to 1 before any instances of Dog are created ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 37
Provided by: temekori
Category:
Tags: java | dog | names | programming

less

Transcript and Presenter's Notes

Title: JAVA Programming


1
JAVA Programming
  • Review Part I

2
JAVAs Purpose
  • Write Once Run Anywhere (WORA)
  • Multiplatform apps written in other languages
    have many distribution versions because a
    different machine-executable version must exist
    for each targeted operated platform.
  • Java programs are compiled to one byte-code
    version which can be executed on any Java enabled
    system

3
Packages
  • Composed of source files that define classes and
    interfaces
  • Enable the organization of source files into
    related groups
  • Names are based on reverse URLs and are written
    in lowercase.
  • Names are hierarchical and correspond to the
    directory structure of the hard drive where the
    source files are stored.
  • Directory java
  • Subdirectory awt java.awt
  • Subdirectory lang java.lang

4
Java Source File Structure
  • Package declaration
  • Import declarations
  • Defined type (class or interface) declarations
  • Comments
  • / and / javadoc.exe utility
  • // single-line comments
  • / and / multi-line comments

5
The import declaration
  • Single-type import declares the fully qualified
    name of a type that is used by the source file
  • Tells the compiler where to look for the
    referenced type
  • import java.io.DataInputStream
  • Import-on-demand advises the Java compiler to
    search the package if it cannot find a referenced
    type within the current source file.
  • Only the types referenced in the source files
    code are included in the compiled code.
  • import java.awt.
  • Import statements must appear directly after the
    package statement or, if no package exists, be
    the first noncomment line in the source file.

6
The java.lang package
  • Automatically made available to the compiler
  • All code may access any of its types and methods
    without including an import statement

7
Defined Types
  • Type declaration and a body
  • Examples
  • Initialization blocks
  • Fields (data)
  • Methods (capabilities, behaviors)
  • Constructors
  • Extends clause signifies an object created with
    the type definition is a subtype of the extended
    type
  • Implements clause signals that an object created
    with the type definition will have the same
    behaviors as the implemented type and is a
    subtype of the implemented type(s).
  • Constructors and initializers are defined within
    a class but are not members of the class.
  • Constructors and initializers are not inherited
    by subclasses.

8
Modifiers
  • Set provisions on whether an object may be
    accessed or used.
  • public, private, static, abstract, final are the
    most commonly used modifiers
  • public and private modifiers are known as access
    modifiers as they allow or restrict interactions
    between objects of different types

9
Modifier meanings
  • public access to the type is unrestricted
  • private access is restricted to objects of the
    same type
  • static belongs to the enclosing type and is not
    implicitly tied to an instance of the enclosing
    type
  • abstract signifies that the type definition is
    incomplete and it cannot be used to create
    (instantiate) objects
  • No class may contain abstract methods unless the
    class itself is declared to be abstract.
  • An abstract method can not be declared private,
    static, native, or final final signifies the
    type cannot be modified or extended

10
Modifier private
  • Can only be accessed by other parts of the class
  • Classes, interfaces, and automatic (local method)
    variables can not be marked as private
  • class Dog
  • private int size 27
  • class TestDog
  • public static void main(String args)
  • Dog d new Dog()
  • System.out.println(d.size)

11
Modifier public
  • Can be accessed by any code that has a valid
    reference to the object of the type identified as
    public
  • A constructor of the class must be accessible to
    instantiate objects of a public class.
  • Cannot mark automatic (local method) variables as
    public

12
Modifier final
  • No further changes can be applied to the method,
    variable or class
  • A final method can not be overridden by a
    subclass
  • A final variables value cannot be changed once
    initialized.
  • A final class can not have any subclasses.
  • Constructors can not be marked as final because
    constructors are not inherited and can not be
    overridden.

13
Modifier static
  • Variables and methods belong to the class rather
    than a particular instance
  • Only need to have the class available
  • All static variables and methods are shared by
    all instances of a particular class.
  • Only one copy of the static variables and methods.

14
  • public class Dog
  • static int dogCount 1
  • public Dog()
  • dogCount
  • Static dogCount variable is set to 1 before any
    instances of Dog are created
  • Whenever a Dog instance is created, the Dog
    constructor runs and increments the dogCount
    static variable

15
  • public class Dog
  • static int dogCount 0
  • public static int getDogCount()
  • return dogCount
  • public Dog()
  • dogCount
  • Static method runs independent of any particular
    instance, even if no instances of that class have
    been created
  • Why does the aforementioned code work?

16
Accessing Static Variables
  • Must use the entire class name to access static
    variables and methods
  • int numofDogs Dog.getDogCount()
  • int numofDogs Dog.dogCount

17
Classes vs. Interfaces
  • A class may implement more than one interface but
    an interface may not implement anything.
  • Methods in interfaces do not include code
    statements that define how the behaviors will b
    executed

18
  • public class InterfaceExample implements Runnable
    (
  • public void run( )
  • The interface Runnable includes on method, run()
  • Any class which implements the Runnable interface
    must declare a run method. Failure to do so will
    result in a compile error.

19
The main() method
  • public static void main(String args)
  • Modifiers public and static are required
  • Return type void is required and must appear
    directly before the method name main()
  • Parameter type String is required

20
Accessing Arrays and Array Elements
  • Array variables are reference variables
  • When they are declared as fields, they are
    initialized to null
  • When declared in methods, they are not
    initialized.
  • Array declarations do not create array elements.
    These are created either through the use of the
    new operator or an initialization block
  • int arrayA new int5
  • String arrayB Welcome,to,Java
  • Array elements are always initialized to the
    default value of their declared type.
  • Once an array is initialized, it may not be
    resized.
  • An arrays length is stored as a property and may
    be accessed using arrayName.length

21
Legal Array Declarations
  • int arraya
  • int arraya
  • int arraya
  • int arraya
  • int arraya null
  • int arrayb new int0
  • int arraya new int 1,2,3,4,5
  • An array initialization statement may combine the
    new operator and an initialization block.
  • An initialization block may not be used once an
    array has been initialized using the new operator

22
String Literals
  • String variables hold a reference to a String
    literal object
  • String objects are immutable (once created,
    cannot be changed)
  • Compiler optimizes the storage of string objects
  • A string created using the new operator is not a
    String literal.
  • The new operator always generates a unique object
    and returns a reference to that object

23
Array Types
  • Arrays can be created of any data type or object.
  • Arrays are declared by adding brackets either
    before or after the variable name
  • int testScores
  • If the array will hold references to String
    objects, then you just add brackets to the class
    name String
  • String students

24
Constructing an Array
  • int testScores or int testScores
  • testScores new int12
  • Creates an array object on the heap that will
    hold 12 elements of type integer and assigned to
    the previously delcared array variable named
    testScores
  • int testScores new int12
  • Animal pets or Animal pets
  • pets new Animal5
  • Creates an array object on the heap that will
    hold 5 Animal object references and assign it to
    the previously declared array variable named pets
  • Animal pets new Animal 5

25
Assigning Values to an Array
  • pets0 new Animal()
  • pets1 new Animal()
  • pets2 new Animal()
  • Two elements in the array will have null
    references.

26
Declaring, Constructing, Initializing
  • int testScores 5,22,89
  • is the same as
  • int testScores new int3
  • testScores 0 5
  • testScores1 22
  • testScores 2 89

27
Modifier abstract
  • Associated with inheritance
  • A concrete subclass must be made in order to make
    use of the abstract class
  • Abstract classes can never be instantiated.
  • Abstract classes can contain non-abstract
    methods.
  • If a class contains abstract methods, the class
    must be declared abstract.
  • Abstract classes must not be marked as private or
    final.
  • Constructors, instance variables, static
    variables, static methods, and automatic
    variables can not be marked as abstract.

28
Constructor
  • Knows how to build the object defined by the
    class
  • May only be declared in class bodies as an error
    is caused if they are declared in an interface
    body
  • Class may have more than one constructor,
    provided the types of the parameters are
    different or given in a different order.
  • If a class definition does not include a
    constructor declaration, the compiler will
    provide a default constructor or default no-arg
    ctor.
  • The name of a constructor must be the same as the
    simple type name of the class in which it is
    declared.
  • A constructor declared with a return type is
    treated as a method, not a constructor.
  • The body of a constructor may contain a return
    statement such as return, but it may not return a
    value.

29
Constructors
  • If you dont create a constructor, a default
    constructor (no-arg) will be automatically be
    inserted by the compiler
  • If you want a no-arg constructor and you have
    created other constructors, you must create one.
  • Every constructor must have as its first line
    either a call to an overloaded constructor or a
    call to the super constructor.
  • A call to a super constructor can be no-arg or
    include arguments passed to the super constructor.

30
Method Overloading
  • Using the same name for two different methods
  • The argument list must be varied.
  • Overloaded methods allow you to vary the return
    type.
  • public class Dog
  • public void change(int size, String name)
  • public void change(int Size, String name, int
    numofDots)
  • The change method is overloaded and the argument
    lists are different.

31
Method Overriding
  • Overriding methods can not change the return type
  • When a method is inherited from a superclass and
    the subclass wants to change the method
    implementation, the subclass code must define a
    method which matches the overriden method.

32
Part I - Review Questions
  • Will the following source file compile without
    error?
  • import java.io.
  • package questions
  • import java.lang.
  • class ClassA
  • Will the following source file compile if the
    source filename is saved as Q36601_2.java?
  • package questions
  • class Q36601_A
  • public class Q36601_2

33
  • Identify the error, if any, in the following
    array statements.
  • int myList a,b,c
  • int myList (5,8,2)
  • int myList 4,9,7,0
  • int myScores
  • char myChars
  • Dog myDogs7
  • What would be the output of the following code?
  • public class Test
  • public static void main(String args)
  • String names new String 5
  • System.out.println(names2)

34
  • What would be the output of the following code?
  • public class Test
  • public static void main(String args)
  • Dog theDogs new Dog3
  • System.out.println(theDogs20.toString())
  • Would the following code compile?
  • public class Dog
  • private int dogCount 0
  • public static int getDogCount()
  • return dogCount

35
  • What will be the output produced by the following
    statements?
  • Boolean b new Boolean(true)
  • System.out.println(b.equals(true)
  • What will be the result of compiling and running
    the following code snippet?
  • int nSquares 1,4,9,16,25,36,49,81
  • int nIndex 0
  • if (-nIndex 0 nSquaresnIndex 10)
  • System.out.println(Ping)
  • else
  • System.out.println(Pong)

36
  • What is the output of the following program?
  • public class ChangeTest
  • public static void main(String s )
  • final int Odds 1,3
  • SwapThem(Odds)
  • System.out.println(Odds0 , Odds1)
  • static void SwapThem (int ar )
  • int i ar0
  • ar0 ar1
  • ar1 I
  • 10. Does the final modifier in 9 produce an
    error? Why or why not?
Write a Comment
User Comments (0)
About PowerShow.com