system development with java lecture 2. rina zviel-girshin @asc2 errors a program can have three...
TRANSCRIPT
![Page 1: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/1.jpg)
System development with Java
Lecture 2
![Page 2: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/2.jpg)
Rina Zviel-Girshin @ASC 2
Errors
• A program can have three types of errors:• Syntax and semantic errors – called compile-
time errors• Run-time errors – occur during program
execution• Logical errors
![Page 3: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/3.jpg)
Rina Zviel-Girshin @ASC 3
Errors
• Compile-time errors occur during program compilation and an executable version of the program is not created.
• Run-time errors occur during program execution and cause abnormal program termination.
• Logical errors occur during program execution and produce incorrect results.
![Page 4: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/4.jpg)
Rina Zviel-Girshin @ASC 4
Java Syntax
• To write without syntax mistakes you have to know Java syntax.
SyntaxSyntax -
the study of the patterns of formation of sentences and phrases from words and of the rules for the formation of grammatical sentences in a language.
![Page 5: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/5.jpg)
Rina Zviel-Girshin @ASC 5
Java Syntax
• Case-sensitive• Semi-colon (;) is line terminator• Curly braces ({,}) used for block structure• Several keywords
![Page 6: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/6.jpg)
Rina Zviel-Girshin @ASC 6
Comments
There are two kinds of comments: • /* text */ A traditional comment: all the text from
the ASCII characters /* to the ASCII characters */ is ignored.
• // text An end-of-line comment: all the text from the ASCII characters // to the end of the line is ignored.
![Page 7: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/7.jpg)
Rina Zviel-Girshin @ASC 7
Comments
Comments do not nest. • /* and */ have no special meaning in comments
that begin with //. • // has no special meaning in comments that begin
with /* or /**.
As a result, the text: /* this comment /* // /** ends here: */is a single complete comment.
![Page 8: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/8.jpg)
Rina Zviel-Girshin @ASC 8
Identifiers
An identifier is:an unlimited-length sequence of Java letters
and Java digits, the first of which must be a Java letter.
An identifier cannot have the same spelling (Unicode character sequence) as:
• a keyword, • boolean literal, • the null literal
![Page 9: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/9.jpg)
Rina Zviel-Girshin @ASC 9
Unicode
• Letters and digits may be drawn from the entire Unicode character set (The character set that uses 16 bit per character).
• Identifier can be written in most writing scripts in use in the world today, including:
Hebrew,Chinese, Japanese, KoreanPractically all languages
![Page 10: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/10.jpg)
Rina Zviel-Girshin @ASC 10
Identifiers examples
• Uppercase and lowercase are different.
All the following are different identifiers:
MY my My mY my1
Examples of identifiers are:
String i3
isLetterOrDigit מונה
MAX_VALUE
![Page 11: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/11.jpg)
Rina Zviel-Girshin @ASC 11
KeywordsThe following are reserved words called keywords
and cannot be used as identifiers:
abstractbooleanbreakbytebyvaluecasecastcatchcharclassconstcontinue
defaultdodoubleelseextendsfalsefinalfinallyfloatforfuturegeneric
gotoifimplementsimportinnerinstanceofintinterfacelongnativenewnull
operatorouterpackageprivateprotectedpublicrestreturnshortstaticsuperswitch
synchronizedthisthrowthrowstransienttruetryvarvoidvolatilewhile
![Page 12: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/12.jpg)
Rina Zviel-Girshin @ASC 12
True, False, Null
While true and false might appear to be keywords, they are technically Boolean literals.
Similarly, while null might appear to be a keyword, it is technically the null literal.
![Page 13: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/13.jpg)
Rina Zviel-Girshin @ASC 13
Literals A literal is a source code representation of a value of:
• a primitive type,• the String type,• the null type
Kinds of Literals: IntegerLiteral FloatingPointLiteral
BooleanLiteral CharacterLiteralStringLiteralNullLiteral
![Page 14: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/14.jpg)
Rina Zviel-Girshin @ASC 14
Integer Literals
An integer literal may be expressed in:• decimal (base 10), • hexadecimal (base 16), • octal (base 8)
![Page 15: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/15.jpg)
Rina Zviel-Girshin @ASC 15
Hexadecimal numeral
• A hexadecimal numeral consists of the leading characters 0x or 0X followed by one or more hexadecimal digits.
• It can represent a positive, zero, or negative integer. • Hexadecimal digits with values 10 through 15 are
represented by the letters a through f or A through F, respectively.
HexDigit is one of:
0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F
![Page 16: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/16.jpg)
Rina Zviel-Girshin @ASC 16
Octal numeral
• An octal numeral consists of a digit 0 followed by one or more of the digits 0 through 7.
• It can represent a positive, zero, or negative integer.
• Octal numerals always consist of two or more digits.
![Page 17: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/17.jpg)
Rina Zviel-Girshin @ASC 17
Example
Examples of int literals:
02 0372
0xDadaCafe 1996
0x00FF00FF • the same number in decimal octal hexadecimal:
3 03 0x3
15 017 0xF
![Page 18: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/18.jpg)
Rina Zviel-Girshin @ASC 18
Zero
• 0 is always considered to be a decimal numeral.• The numerals 0, 00, and 0x0 all represent exactly
the same integer value – zero value.
![Page 19: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/19.jpg)
Rina Zviel-Girshin @ASC 19
Floating-Point Literals• A floating-point literal has the following parts:
• a whole-number part, • a decimal point (represented by an ASCII period character), • a fractional part, • an exponent (is indicated by a letter e or E followed by an
optionally signed integer),
• and a type suffix. • At least one digit, in either the whole number or
the fraction part, and either a decimal point, an exponent, or a float type suffix are required. All other parts are optional.
![Page 20: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/20.jpg)
Rina Zviel-Girshin @ASC 20
Floating-Point Literals
• A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d.
Examples of float literals:
1e1f 2.f .3f 0f 3.14f 6.022137e+23f
Examples of double literals:
1e12..30.03.14
![Page 21: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/21.jpg)
Rina Zviel-Girshin @ASC 21
Boolean Literals
• The boolean type has two values, represented by the literals true and false
• A boolean literal is always of type boolean.
BooleanLiteral is one of:
true false
![Page 22: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/22.jpg)
Rina Zviel-Girshin @ASC 22
Character Literals
• A character literal is expressed as a character or an escape sequence, enclosed in single quotes.
• The single-quote, or apostrophe, character is \u0027 - .
• A character literal is always of type char.
Examples of char literals:
'a‘ '%‘ '\t‘ '\\‘ '\u03a9‘
'\uFFFF‘ '\177'
![Page 23: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/23.jpg)
Rina Zviel-Girshin @ASC 23
Unicode Character Type
•A char value stores a single character from the Unicode character set.
•The values range is between 0 and 65535.
•The amount of memory it requires: 16 bit or 2 bytes.
•The Unicode character set uses 16 bits per character.
![Page 24: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/24.jpg)
Rina Zviel-Girshin @ASC 24
Part of the Unicode Set0x3041 …
0x05B0 …
0x77CD …
0x0021 …
…
![Page 25: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/25.jpg)
Rina Zviel-Girshin @ASC 25
ASCII
• The ASCII character set is still the basis for many other programming languages.
• The ASCII character set uses 8 bits (one byte) per character.
• ASCII is a subset of Unicode.
![Page 26: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/26.jpg)
Rina Zviel-Girshin @ASC 26
Part of the ASCII Set
0x21 …
…0xB9
![Page 27: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/27.jpg)
Rina Zviel-Girshin @ASC 27
String Literals • A string literal consists of zero or more characters
enclosed in double quotes. • A string literal is always of type String.
Examples of string literals: "" // the empty string
"This is a string" // a string containing 16 characters
"This is a " + // actually a string-valued constant expression,
"two-line string" // formed from two string literals
![Page 28: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/28.jpg)
Rina Zviel-Girshin @ASC 28
Escape Sequences for Character and String Literals
• The character and string escape sequences allow for the representation of some non-graphic characters as well as the single quote, double quote, and backslash characters in character literals and string literals.
![Page 29: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/29.jpg)
Rina Zviel-Girshin @ASC 29
Escape Sequences for Character and String Literals
Escape Sequence:\ b /* \u0008: backspace BS */\ t /* \u0009: horizontal tab HT */\ n /* \u000a: linefeed LF */\ f /* \u000c: form feed FF */\ r /* \u000d: carriage return CR */\ " /* \u0022: double quote " */\ ' /* \u0027: single quote ' */\ \ /* \u005c: backslash \ */
![Page 30: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/30.jpg)
Rina Zviel-Girshin @ASC 30
The Null Literal
• The null type has one value, the null reference, represented by the literal null, which is formed from ASCII characters.
• A null literal is always of the null type.
![Page 31: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/31.jpg)
Rina Zviel-Girshin @ASC 31
Separators
The following nine ASCII characters are the separators (punctuators):
( ) { } [ ] ; , .
![Page 32: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/32.jpg)
Rina Zviel-Girshin @ASC 32
Operators
The following 37 tokens are the operators, formed from ASCII characters:
= > < ! ~ ? :
== <= >= != && || ++
-- + - * / & |
^ % << >> >>> += -=
*= /= &= |= ^= %=
![Page 33: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/33.jpg)
Rina Zviel-Girshin @ASC 33
Programming style
• Java is a free-format language. • There are no syntax rules about how the program
has to be arranged on a page. You can write entire program in one line.
• But as a matter of good programming style, you should lay out your program on the page in a way that will make its structure as clear as possible.
![Page 34: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/34.jpg)
Rina Zviel-Girshin @ASC 34
Programming style
• Some advises:• Put one statement per line. • Use indentation to indicate statements that
are contained inside control structures. • Write comments.• Give your variables names that make sense.
![Page 35: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/35.jpg)
Rina Zviel-Girshin @ASC 35
Style ExampleBad:
public class
Stam
{ public static void
main(String args[]){System.out.println("Hello!“);}}
Better:
// style example – outputs “Hello!”
public class Hello
{
public static void main(String args[])
{
System.out.println("Hello!");
}
}
![Page 36: System development with Java Lecture 2. Rina Zviel-Girshin @ASC2 Errors A program can have three types of errors: Syntax and semantic errors – called](https://reader035.vdocuments.net/reader035/viewer/2022062718/56649e915503460f94b971e9/html5/thumbnails/36.jpg)
Rina Zviel-Girshin @ASC 36
Any Questions?