Introduction to Java - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Introduction to Java

Description:

Java is just one of a number of languages designed to occupy the intersection ... it slower than native code, but faster than interpreted scripts like PHP or Perl. ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 16
Provided by: harry66
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Java


1
Introduction to Java
  • COM379 (Part-Time)
  • University of Sunderland
  • Harry R Erwin, PhD

2
Java
  • Java is just one of a number of languages
    designed to occupy the intersection between
    object-orientation and C.
  • Other such languages include
  • C
  • C
  • Objective C

3
Assumptions
  • It is assumed here that you know C or C, or
    some Algol-based language.
  • This lecture discusses the differences between
    C/C and Java based on Flanagan, 2002, Java in a
    Nutshell, 4th edition.
  • For more detail, see the Sun introductory
    tutorials and Flanagan.

4
Preprocessor
  • Java has no preprocessor
  • No macros.
  • No analogs of define, include, or ifdef.
  • No header files
  • No conditional compilation
  • assert was added as a language statement in Java
    1.4.0.

5
Global Variables
  • None, nada.
  • Packages contain classes. Classes contain fields
    and methods. Methods contain local variables.
  • To simulate a global variable, use a public
    static member field of some class. For examples
    of how to do this, look at the Arrays, Math, or
    Collections classes.

6
Primitive Types
  • All the primitive types in Java have
    well-defined, machine- independent sizes and
    properties. Learn them.
  • These include
  • boolean
  • char
  • byte
  • short
  • int
  • long
  • float
  • double

7
Horses for Courses
  • Primitive types lack methods and cannot be stored
    in collections that expect some sort of object.
  • Each primitive type has a corresponding class
    (with useful methods) that provides instances
    that can be stored in a collection.
  • Booleanboolean
  • Characterchar
  • Bytebyte
  • Shortshort
  • Integerint
  • Longlong
  • Floatfloat
  • Doubledouble

8
Pointers
  • There are no programmer-accessible pointers in
    Java.
  • Classes and arrays in Java are reference types.
    Java manages the underlying pointers.
  • There is no way to convert from a reference to a
    primitive object like you can treat a C pointer
    as an integer type.
  • You cannot use a reference to access a private
    member attribute.

9
Objects
  • Reference types inherit from the class Object.
    Object provides a number of methods, including
  • Class getClass()
  • String toString()
  • boolean equals(Object o) // by value
  • int hashcode() // also by value
  • Object clone()
  • toString(), equals(), hashcode(), and clone()
    should usually be overridden if a class uses them.

10
Strings
  • A String is an object like a C string, not an
    array like a C string.
  • A String is constant once it is created. If you
    want to change a String, give the name a new
    value.
  • Among other ways, Strings can be created by the
    toString operator applied to an object, by
    setting the object equal to a literal (String
    name data), and by concatenation using and
    .

11
Garbage Collection
  • Java manages the heap. When a reference type
    object goes out of scope, it gets marked for
    later clean-up.
  • You dont need to delete or return any storage.
  • Cleanup happens at the convenience of the Java
    runtime environment. You can suggest that the
    time is right by calling System.gc(), but that is
    only a suggestion. This is why Java is unsuitable
    for real-time applications, even though it was
    designed for embedded systems.
  • To create an object of a reference type (array or
    class instance), you usually use the new operator.

12
Syntax
  • Variables may be declared anywhere. The variable
    name is in scope in the local block from the
    point of declaration. Reference types are set to
    null (non-existent) until they are given a
    value. Primitive types have a default value.
  • Forward references within a class definition are
    usually OK, but not within method code. Within a
    method, local variables must be in scope before
    they are used.
  • Method overloading is allowed. The argument type
    list is part of the method signature.
  • No operator overloading (except for the String
    class, which has and defined).

13
None of the Following are Available
  • No goto statement
  • No structs (use class)
  • No unions
  • No enums (use object constants)
  • No bitfields
  • No typedefs
  • No method pointers (use functors)
  • No variable-length argument lists

14
Write Once/Run Anywhere
  • Java is designed to be architecture-independent.
  • The compiler will convert your codefiles into
    class files that can be executed anywhere.
  • I run it under MacOS X the Suns run it under
    Solaris, and Windows also runs it.
  • That makes it slower than native code, but faster
    than interpreted scripts like PHP or Perl.
  • This scares M.

15
Summary
  • Java is based on C and C, but is not an
    extension of either. Assuming Java is C with
    classes will lead you into problems.
  • Know the similarities and the differences!
Write a Comment
User Comments (0)
About PowerShow.com