Introduction to Java - University of Sunderland

Download Report

Transcript Introduction to Java - University of Sunderland

Introduction to Java
COM379 (Part-Time)
University of Sunderland
Harry R Erwin, PhD
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
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.
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.
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.
Primitive Types
• All the primitive types in Java have well-defined, machineindependent sizes and properties. Learn them.
• These include:
–
–
–
–
–
–
–
–
boolean
char
byte
short
int
long
float
double
‘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.
• Boolean—boolean
• Character—char
• Byte—byte
• Short—short
• Integer—int
• Long—long
• Float—float
• Double—double
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.
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.
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 +=.
Garbage Collection
• Java manages the heap. When a reference type object goes
out of scope, it gets marked for later clean-up.
• You don’t 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.
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).
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
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$.
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!