Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 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 http://java.sun.com/docs/books/tutorial/index.html

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

4 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)

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

6 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: –π –Ö

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

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

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

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

11 Operators Generally as in C or C++. See the earlier lecture. Typecasting is handled by (type)var;, not type(var);!

12 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).

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

14 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

15 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)


Download ppt "Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD."

Similar presentations


Ads by Google