System development with Java Lecture 2 Rina ZvielGirshin

  • Slides: 36
Download presentation
System development with Java Lecture 2 Rina Zviel-Girshin @ASC

System development with Java Lecture 2 Rina Zviel-Girshin @ASC

Errors • A program can have three types of errors: • Syntax and semantic

Errors • A program can have three types of errors: • Syntax and semantic errors – called compiletime errors • Run-time errors – occur during program execution • Logical errors Rina Zviel-Girshin @ASC 2

Errors • Compile-time errors occur during program compilation and an executable version of the

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. Rina Zviel-Girshin @ASC 3

Java Syntax • To write without syntax mistakes you have to know Java syntax.

Java Syntax • To write without syntax mistakes you have to know Java syntax. Syntax - Syntax 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. Rina Zviel-Girshin @ASC 4

Java Syntax • • Case-sensitive Semi-colon (; ) is line terminator Curly braces ({,

Java Syntax • • Case-sensitive Semi-colon (; ) is line terminator Curly braces ({, }) used for block structure Several keywords Rina Zviel-Girshin @ASC 5

Comments There are two kinds of comments: • /* text */ A traditional comment:

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. Rina Zviel-Girshin @ASC 6

Comments do not nest. • /* and */ have no special meaning in 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. Rina Zviel-Girshin @ASC 7

Identifiers An identifier is: üan unlimited-length sequence of Java letters and Java digits, üthe

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 Rina Zviel-Girshin @ASC 8

Unicode • Letters and digits may be drawn from the entire Unicode character set

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, üKorean üPractically all languages Rina Zviel-Girshin @ASC 9

Identifiers examples • Uppercase and lowercase are different. All the following are different identifiers:

Identifiers examples • Uppercase and lowercase are different. All the following are different identifiers: MY my My m. Y my 1 Examples of identifiers are: String i 3 is. Letter. Or. Digit מונה MAX_VALUE Rina Zviel-Girshin @ASC 10

Keywords The following are reserved words called keywords and cannot be used as identifiers:

Keywords The following are reserved words called keywords and cannot be used as identifiers: abstract boolean break byte byvalue cast catch char class const continue default do double else extends false finally float for future generic goto if implements import inner instanceof interface long native new null operator outer package private protected public rest return short static super switch Rina Zviel-Girshin @ASC synchronized this throws transient true try var void volatile while 11

True, False, Null While true and false might appear to be keywords, they are

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. Rina Zviel-Girshin @ASC 12

Literals A literal is a source code representation of a value of: • a

Literals A literal is a source code representation of a value of: • a primitive type, • the String type, • the null type Kinds of Literals: Integer. Literal Floating. Point. Literal Boolean. Literal Character. Literal String. Literal Null. Literal Rina Zviel-Girshin @ASC 13

Integer Literals An integer literal may be expressed in: • decimal (base 10), •

Integer Literals An integer literal may be expressed in: • decimal (base 10), • hexadecimal (base 16), • octal (base 8) Rina Zviel-Girshin @ASC 14

Hexadecimal numeral • A hexadecimal numeral consists of the leading characters 0 x or

Hexadecimal numeral • A hexadecimal numeral consists of the leading characters 0 x or 0 X 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. Hex. Digit 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 Rina Zviel-Girshin @ASC 15

Octal numeral • An octal numeral consists of a digit 0 followed by one

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. Rina Zviel-Girshin @ASC 16

Examples of int literals: 02 0372 0 x. Dada. Cafe 1996 0 x 00

Examples of int literals: 02 0372 0 x. Dada. Cafe 1996 0 x 00 FF • the same number in decimal octal hexadecimal: 3 03 0 x 3 15 017 0 x. F Rina Zviel-Girshin @ASC 17

Zero • 0 is always considered to be a decimal numeral. • The numerals

Zero • 0 is always considered to be a decimal numeral. • The numerals 0, 00, and 0 x 0 all represent exactly the same integer value – zero value. Rina Zviel-Girshin @ASC 18

Floating-Point Literals • A floating-point literal has the following parts: • • a whole-number

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. Rina Zviel-Girshin @ASC 19

Floating-Point Literals • A floating-point literal is of type float if it is suffixed

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: 1 e 1 f 2. f. 3 f 0 f 3. 14 f 6. 022137 e+23 f Examples of double literals: 1 e 12. . 30. 03. 14 Rina Zviel-Girshin @ASC 20

Boolean Literals • The boolean type has two values, represented by the literals true

Boolean Literals • The boolean type has two values, represented by the literals true and false • A boolean literal is always of type boolean. Boolean. Literal is one of: true false Rina Zviel-Girshin @ASC 21

Character Literals • A character literal is expressed as a character or an escape

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 u 0027 -. • A character literal is always of type char. Examples of char literals: 'a‘ '%‘ 't‘ '\‘ 'u 03 a 9‘ 'u. FFFF‘ '177' Rina Zviel-Girshin @ASC 22

Unicode Character Type • A char value stores a single character from the Unicode

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. Rina Zviel-Girshin @ASC 23

Part of the Unicode Set 0 x 0021 … 0 x 3041 … 0

Part of the Unicode Set 0 x 0021 … 0 x 3041 … 0 x 05 B 0 … 0 x 77 CD … … Rina Zviel-Girshin @ASC 24

ASCII • The ASCII character set is still the basis for many other programming

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. Rina Zviel-Girshin @ASC 25

Part of the ASCII Set 0 x 21 … … 0 x. B 9

Part of the ASCII Set 0 x 21 … … 0 x. B 9 Rina Zviel-Girshin @ASC 26

String Literals • A string literal consists of zero or more characters enclosed in

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 Rina Zviel-Girshin @ASC 27

Escape Sequences for Character and String Literals • The character and string escape sequences

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. Rina Zviel-Girshin @ASC 28

Escape Sequences for Character and String Literals Escape Sequence:  b /* u 0008:

Escape Sequences for Character and String Literals Escape Sequence: b /* u 0008: backspace BS */ t /* u 0009: horizontal tab HT */ n /* u 000 a: linefeed LF */ f /* u 000 c: form feed FF */ r /* u 000 d: carriage return CR */ " /* u 0022: double quote " */ ' /* u 0027: single quote ' */ /* u 005 c: backslash */ Rina Zviel-Girshin @ASC 29

The Null Literal • The null type has one value, the null reference, represented

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. Rina Zviel-Girshin @ASC 30

Separators The following nine ASCII characters are the separators (punctuators): ( ) { }

Separators The following nine ASCII characters are the separators (punctuators): ( ) { } [ ] ; , . Rina Zviel-Girshin @ASC 31

Operators The following 37 tokens are the operators, formed from ASCII characters: = >

Operators The following 37 tokens are the operators, formed from ASCII characters: = > < ! ~ ? : == <= >= != && || ++ -- + * / & | ^ % << >> >>> += -= *= /= &= |= ^= %= Rina Zviel-Girshin @ASC 32

Programming style • Java is a free-format language. • There are no syntax rules

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. Rina Zviel-Girshin @ASC 33

Programming style • Some advises: • Put one statement per line. • Use indentation

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. Rina Zviel-Girshin @ASC 34

Style Example Bad: Better: public class Stam { public static void main(String args[]){ //

Style Example Bad: Better: public class Stam { public static void main(String args[]){ // style example – outputs “Hello!” System. out. println("Hello!“); }} public class Hello { public static void main(String args[]) { System. out. println("Hello!"); } } Rina Zviel-Girshin @ASC 35

Any Questions? Rina Zviel-Girshin @ASC 36

Any Questions? Rina Zviel-Girshin @ASC 36