15
Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD

Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD

Embed Size (px)

Citation preview

Page 1: Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD

Basic Java Syntax

CSE301University of Sunderland

Harry R Erwin, PhD

Page 2: Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD

Key Resources to Help You Learn Java

• You need to own and read Flanagan, Java in a Nutshell, 5th edition. My lectures are not enough to teach you Java.

• You need access to Flanagan’s other Nutshell books.

• You need an up-to-date Java 1.5 (or newer) compiler and run-time environment.

• You need access to eclipse 3.1.1• You need to explore the Sun tutorials http://java.sun.com/docs/books/tutorial/index.html

Page 3: Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD

Unicode

• Java is written in the Unicode character set.

• Unicode characters are stored in 16 bits (some Far Eastern languages require 21) and can represent almost all written languages.– A– å– π

• Java programs can be written in ASCII or Latin-1 characters, which most text editors support.

• Unicode can be used anywhere in a Java program.

Page 4: Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD

Comments

• Single-line comments: //…………..• Multi-line comments: /*…..*/• Doc comments:

– Begin with /**….– Provide embedded documentation– End with */– Worth investigating further, because they allow you to write self-documenting codefiles. This is an easy way to meet some of the project requirements.

– Extended in Java 5 (discussed in two weeks)

Page 5: Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD

Scope of Names

• The ‘scope’ of a declaration is the region of the program within which the entity can be referred to using a simple name.

• Types must be imported or declared to be in scope.

• Members have class or interface scope.• Method parameters have method scope.• Local variables have block scope.• When a name is hidden by another name, you must provide the full name. Sometimes (method parameters, for example) there is no full name you can use.

Page 6: Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD

Identifiers

• Begin with a letter, underscore (_), or currency symbol (€$¥£).

• Contain any number of letters, numbers, underscores, and currency symbols.

• Avoid the currency symbols as compilers use them.

• Remember Unicode. The following are legal identifiers:– π– Ö

Page 7: Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD

Primitive Data Types

– boolean– char– short– byte– int– long– float– double

• Have machine-independent formats and default values. Know them! I usually ask a question on them in the TCT or exam.

Page 8: Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD

String

• Contains Unicode text• Constant—unlike C and C++ strings.• A class, not a primitive type• Literals consist of anything between a pair of double quotes. To include a double quote in a string, use \”

• Supports operator overloading (+, +=). This is the only place in Java where operator overloading takes place.

Page 9: Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD

Type Conversions

• Integer to floating point are automatic.

• char to/from integer and floating point

• Widening conversions (e.g., short to long) are safe.

• Narrowing conversions are dangerous. You need to understand why. I usually ask a question on this in the TCT or exam.

Page 10: Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD

Classes and Arrays

• Reference types.• Created using the new operator

– new type(args); // an object– new type[dim]; // 1-D array– new type[dim1][dim2] etc; //larger arrays

• A class or array object is null (does not exist) until it is initialized.

• Interfaces are reference types, too.• Discussed in the next lecture.

Page 11: Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD

Operators

• Generally as in C or C++. See the earlier lecture.

• Typecasting is handled by (type)var; , not type(var);!

Page 12: Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD

Statements

• Generally as in C/C++. See the earlier lecture.

• Additional:– synchronized (threads)– throw (an exception)– try/catch/finally (to handle exceptions)

– All the new Java 5 stuff (discussed in two weeks).

Page 13: Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD

Methods

• Correspond to functions in C or C++, but are always associated with a class.

• Defined by their signature:– Name– Arguments– Return type (differs from C++, may be void)– Checked exceptions thrown– Method modifiers: public, static, abstract, final, native, private, protected, synchronized.

Page 14: Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD

Method Modifiers

• public—can be called from anywhere• static—defined at the class, not instance level

• abstract—must be defined by a subclass to be called

• final—no changes allowed• native—calls native code• private—hidden from other classes• protected—visible in same package and subclasses

• synchronized—supports multithreading

Page 15: Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD

Summary

• Java is not C or C++• Everything is either a primitive type or a reference type.

• Primitive types have default values, while reference types do not exist until they are initialized.

• Java smells like C and C++, but a language that smells may not be to your liking… 8)