Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD.

Similar presentations


Presentation on theme: "Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD."— Presentation transcript:

1 Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD

2 Notes We will explore basic C syntax that is reused in Java. We will also have one or two lectures on the new stuff in Java 5.0. Today we will examine some differences between Java 1.4 and C++

3 Java Java is just one of several languages designed to occupy the intersection between object-orientation and C. Other such languages include: –C++ –C# –Objective C

4 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 5.0 based on Flanagan, 2005, Java in a Nutshell, 5th edition. For more detail, see the Sun introductory tutorials and Flanagan’s book.

5 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.

6 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.

7 Primitive Types All the primitive types in Java have well-defined, machine- independent sizes and properties. Learn them cold. I guarantee there will be an exam question of some sort worth 10 marks on them. These include: –boolean –char –byte –short –int –long –float –double

8 ‘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

9 Pointers There are no programmer-accessible pointers in Java. Classes and arrays (and interfaces) 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. Know the three basic reference types!

10 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() …; These are always available for a reference type. toString(), equals(), hashcode(), and clone() should usually be overridden if a class uses them.

11 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 +=.

12 Garbage Collection Java manages memory (heap or free store). When a reference type object goes out of scope, it gets marked for later clean-up. You never 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.

13 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 that you need to memorize. 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).

14 None of the Following are Available: No goto statement No structs (use classes) No unions No enums (use object constants, changed for Java 5.0) No bitfields No typedefs No method pointers (use functors) No variable-length argument lists (changed for Java 5.0)

15 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$.

16 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 serious problems. My exam questions are designed to be nearly impossible to answer based only on a knowledge of C/C++. Know the similarities and the differences!


Download ppt "Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD."

Similar presentations


Ads by Google