Introduction to Computing Using Java Java language basics K凶心 2008-20092a Michael Fung, Cs&E, The Chinese University of HK
2008-2009 2a Michael Fung, CS&E, The Chinese University of HK 1 Introduction to Computing Using Java Java Language Basics
Common Syntax/ Grammar Notations Arrow-bracketed item or Fill in this blank Without the arrows This is a place-holder DO NOT type the arrows in actual codel k Brackets and punctuations Type carefully and place in appropriate location K凶心 2008-20092a Michael Fung, CS&E, The Chinese University of HK 2
2008-2009 2a Michael Fung, CS&E, The Chinese University of HK 2 Common Syntax/ Grammar Notations Arrow-bracketed item – or – Fill in this blank WITHOUT the arrows – This is a place-holder – DO NOT type the arrows in actual code! Brackets and punctuations – { } ( ) [ ] , ; . – Type carefully and place in appropriate location
Quotes and Brackets Single Quote 單帝引號 * Double Quote 雙引號 *() Brackets,/ Parentheses括號/小括號 Square brackets 方括號/中括號 *{} Curly Brackets/ Braces花括號/大括號 *: Semi-colon 分號 * Dot/ Full-stop 點 Comma 逗號 K凶心 2008-20092a Michael Fung, CS&E, The Chinese University of HK 3
2008-2009 2a Michael Fung, CS&E, The Chinese University of HK 3 Quotes and Brackets ' Single Quote 單弔引號 " Double Quote 雙引號 ( ) Brackets/ Parentheses 括號/ 小括號 [ ] Square Brackets 方括號/ 中括號 { } Curly Brackets/ Braces 花括號/ 大括號 ; Semi-colon 分號 . Dot/ Full-stop 點 , Comma 逗號
ava Naming convention /Capitalization) Examples keyword class. if double. int. for. while packagename javax. swing, javaapplication 12 ClassName String. JOptionPane. Double FileName Hello world. java, JOption Pane class methodName0 showMessage Dialog(), parseInt() fieldName System. out, Drink Dispenser. coke Stock variableName numberOfStudents, body Weight CONSTANT JFrame HEIGHT. Math. PI K|心心 2008-20092a Michael Fung, CS&E, The Chinese University of HK
2008-2009 2a Michael Fung, CS&E, The Chinese University of HK 4 Java Naming Convention (Capitalization) Examples keyword class, if, double, int, for, while packagename javax.swing, javaapplication12 ClassName String, JOptionPane, Double FileName HelloWorld.java, JOptionPane.class methodName() showMessageDialog( ), parseInt( ) fieldName System.out, DrinkDispenser.cokeStock variableName numberOfStudents, bodyWeight CONSTANT JFrame.HEIGHT, Math.PI
Java Program Structure import .i import * class i Field (Data Member) ; Declarations (.. Method declarations K国 Michael Fung, CS&E, The Chinese University of HK
2008-2009 2a Michael Fung, CS&E, The Chinese University of HK 5 Java Program Structure Field (Data Member) Declarations Method Declarations import .; import .*; class { ; ; ( ... ) { } ( ... ) { } }
Java Program Examp import java.util. Vector import javax. swing. class CurrencyConverter double rateHktoEuro =0.0885: Field (Data Member) double rateHKtoUS =0.128; Declarations double convertEuro2us( double amoun tEuro / details skipped Method declarations double convertUS2HK( double amountS / details skipped K国 Michael Fung, CS&E, The Chinese University of HK 6
2008-2009 2a Michael Fung, CS&E, The Chinese University of HK 6 Java Program Example Field (Data Member) Declarations Method Declarations import java.util.Vector; import javax.swing.*; class CurrencyConverter { double rateHKtoEuro = 0.0885; double rateHKtoUS = 0.128; double convertEuro2US ( double amountEuro ) { // details skipped } double convertUS2HK ( double amountUS ) { // details skipped } }
Data Item declaration x Data items are used to store data Such as fields. local variables. constants t We give name(an identifier) to each data item e. g. Ma liu shui, Tai Po road, shatin is a piece of data This information is called CUHKaddress k Each data item must bear a type e.g. CUHKaddress is of type String(text) K凶心 2008-20092a Michael Fung, CS&E, The Chinese University of HK
2008-2009 2a Michael Fung, CS&E, The Chinese University of HK 7 Data Item Declaration Data items are used to store data. – Such as fields, local variables, constants, … We give name (an identifier) to each data item. – e.g. “Ma Liu Shui, Tai Po Road, Shatin.” is a piece of data. This information is called CUHKaddress. Each data item must bear a type. – e.g. CUHKaddress is of type String (text)
Identifier Names introduced by programmers in computer languages are called identifiers e Address he11。Wor1d One2Free just say hi Ring K凶心 2008-20092a Michael Fung, CS&E, The Chinese University of HK 8
2008-2009 2a Michael Fung, CS&E, The Chinese University of HK 8 Identifier Names introduced by programmers in computer languages are called identifiers. – e.g. Address hello_World One2Free j_u_s_t___2___say___hi Ring
Java identifiers t Valid components ABCDEFGHIUKLMNOPORSTUVWXYZ abcdefghijklmnopqrstuvwxyz 1234567890 underscore Space is disallowed x Must start with a letter or underscore e.g. 12Free is disallowed while One 2Free is okay x Case sensitive e.g. Hello is not equal to hello nor HellO K凶心 2008-20092a Michael Fung, CS&E, The Chinese University of HK
2008-2009 2a Michael Fung, CS&E, The Chinese University of HK 9 Java Identifiers Valid components – ABCDEFGHIJKLMNOPQRSTUVWXYZ – abcdefghijklmnopqrstuvwxyz – 1234567890 – _ “underscore” Space is disallowed. Must start with a letter or underscore. – e.g. 12Free is disallowed while One2Free is okay Case sensitive. – e.g. Hello is not equal to hello nor HellO
Choice of Identifiers t easy to understand and remember e.g. noofEnquiries, Trees, timeToLive name, address, count isok, Listi experimentl2 t May use multiple words(without space!) e.g. grade point average, myBirthday numberofstuder七s Dont be laz e.g. i,j,k,a b, c d e K凶心 2008-20092a Michael Fung, CS&E, The Chinese University of HK
2008-2009 2a Michael Fung, CS&E, The Chinese University of HK 10 Choice of Identifiers Easy to understand and remember. – e.g. noOfEnquiries, Trees, timeToLive, name, address, count, isOK, List, experiment12 May use multiple words (without space!). – e.g. grade_point_average, myBirthday, numberOfStudents Don’t be lazy! – e.g. i, j, k, a, b, c, d, e, …