basic java syntax cse301 university of sunderland harry r erwin, phd
TRANSCRIPT
![Page 1: Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD](https://reader036.vdocument.in/reader036/viewer/2022082818/56649ebf5503460f94bc9fa5/html5/thumbnails/1.jpg)
Basic Java Syntax
CSE301University of Sunderland
Harry R Erwin, PhD
![Page 2: Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD](https://reader036.vdocument.in/reader036/viewer/2022082818/56649ebf5503460f94bc9fa5/html5/thumbnails/2.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082818/56649ebf5503460f94bc9fa5/html5/thumbnails/3.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082818/56649ebf5503460f94bc9fa5/html5/thumbnails/4.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082818/56649ebf5503460f94bc9fa5/html5/thumbnails/5.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082818/56649ebf5503460f94bc9fa5/html5/thumbnails/6.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082818/56649ebf5503460f94bc9fa5/html5/thumbnails/7.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082818/56649ebf5503460f94bc9fa5/html5/thumbnails/8.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082818/56649ebf5503460f94bc9fa5/html5/thumbnails/9.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082818/56649ebf5503460f94bc9fa5/html5/thumbnails/10.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082818/56649ebf5503460f94bc9fa5/html5/thumbnails/11.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082818/56649ebf5503460f94bc9fa5/html5/thumbnails/12.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082818/56649ebf5503460f94bc9fa5/html5/thumbnails/13.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082818/56649ebf5503460f94bc9fa5/html5/thumbnails/14.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082818/56649ebf5503460f94bc9fa5/html5/thumbnails/15.jpg)
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)