Basic Java Syntax

  • Uploaded by: www.jaganguys.com
  • 0
  • 0
  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Basic Java Syntax as PDF for free.

More details

  • Words: 737
  • Pages: 15
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

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.

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)

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.

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

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.

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.

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.

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.

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

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

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.

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

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)

Related Documents

Basic Java Syntax
November 2019 12
Basic Vs Crystal Syntax
November 2019 15
08 Basic Syntax Ii
November 2019 15
Basic Php Syntax
May 2020 7
Sql Syntax Basic
November 2019 18
Basic Java
November 2019 17