《LTPJChapter》由会员分享,可在线阅读,更多相关《LTPJChapter(22页珍藏版)》请在金锄头文库上搜索。
1、Learn To Program Java,1,Chapter 02,Basic Building Blocks,Learn To Program Java,2,Basic Building Blocks,Words, numbers, punctuation marks and special symbols form the basic building blocks of the English language. Java too has its own set of building blocks and rules. Vocabulary and character sets Pr
2、imitive data types Identifiers Reserved words Comments,Learn To Program Java,3,Vocabulary and character sets,Morning good Tan Mr. Good morning Mr. Tan. Java vocabulary is made up of letters, digits and special symbols (such as +, -, *, /, etc.). Grouped together following the syntax rules of Java.,L
3、earn To Program Java,4,A Simple Java Program,class Chap2Print2Numbers public static void main (String args) /* Comment: prints 2 numbers */ int firstNumber = 5; int secondNumber = 10; System.out.println(“The 1st number is “ + firstNumber); System.out.println(“The 2nd number is “ + secondNumber); ,Th
4、e output of the about code is: The 1st number is 5 The 2nd number is 10,Learn To Program Java,5,Variables,A named storage location in the computers memory that stores a value of a particular type for use by program. Example of variable declaration: DataType variableName ; int myAge, cellPhone; doubl
5、e salary; char tempChar; The data type can either be built-in primitive types (e.g. int, double, char) object classes, or reference data types (e.g. String, BufferedReader),Learn To Program Java,6,Primitive Data Type,Data types that have been pre-defined in Java.,Learn To Program Java,7,Primitive Da
6、ta Type,boolean - true or false. example: boolean isStart = true; boolean isEnd = false; char characters example: char tempChar1 = A; char tempChar2 = ; char tempChar3 = ; refer to table 2.2, page 24 for the escaped characters,Learn To Program Java,8,Primitive Data Type,int - integers (octal, decima
7、l or hexadecimal). example: int num1 = 036; / octal, base 8 int num2 = 30; / decimal, base 10 int num3 = 0x1E; / hexadecimal, base 16 Octal: 0,1,2,3,4,5,6,7 Decimal: 0,1,2,3,4,5,6,7,8,9 Hexadecimal: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,Learn To Program Java,9,Primitive Data Type,float - decimal number wi
8、th an optional decimal point and followed by an optional exponent. example: float dec1 = 15; float dec2 = 15.5f; float dec3 = 3.5E2f; double - decimal number with an optional decimal point and followed by an optional exponent. example: double dec1 = 15; double dec2 = 15.5; double dec3 = 1E2;,Learn T
9、o Program Java,10,Reference Data Type (concept of objects in Chapter 6),null - represents an invalid or uncreated object.,All escape sequences that are applicable to characters can be embedded within String literals.,Learn To Program Java,11,Data Type,Data type,Primitive Data type,Reference Data typ
10、e,byte, int, short, long,float,double,char,boolean,class,interface,array,Learn To Program Java,12,Primitive Data Type,Integer.MAX_VALUE Integer.MIN_VALUE Float.MAX_VALUE Float.MIN_VALUE Double.MAX_VALUE Double.MIN_VALUE Short.MAX_VALUE Short.MIN_VALUE Byte.MAX_VALUE Byte.MIN_VALUE,Learn To Program J
11、ava,13,public class JavaDemo01 public static void main(String args) boolean flag = true ; flag = false ; / in language C, 0 - false others true flag = 1 ; System.out.println(flag) ; ;,Learn To Program Java,14,public class JavaDemo02 public static void main(String args) char c = A ; int i = c ; Syste
12、m.out.println(i) ; ;,Learn To Program Java,15,Identifiers,Declared entities (such as variables, method and class) and labels are named by identifiers. Identifier in Java must begin with a letter, underscore (_) or dollar sign ($), followed by any number of letters, digits, underscore (_), and dollar
13、 sign ($). Java is case-sensitive. tcs is not Tcs and is not TCS Naming Convention: Class name - first letter is capitalize. Variable name - if identifier is made-up of several words, first word is in lowercase and the rests are initial capitalized. E.g. thisIsALongVariableName,Learn To Program Java
14、,16,Identifiers,Which of the following are valid identifiers? HelloWorld $temp _$Str 1Dollar abc123 a&b,Valid,Valid,Valid,Invalid: cannot start with a digit,Valid,Invalid: illegal character &,Learn To Program Java,17,Reserved Word,Reserved words cannot be used as identifiers (refer to table 2.5 page
15、 22). Since Java is case-sensitive, Abstract can be considered as identifiers since abstract is not equivalent to Abstract. Such use of identifier names can be confusing and may obscure the intention of the code, hence not advisable. All the reserved word in Java is in lower case. Although goto / va
16、lid but not advisable,Learn To Program Java,18,Learn To Program Java,19,Comments,Good practice to write comments to describe the purpose and algorithm of a piece of code. Comments are ignored by the JVM at run-time Single line comment / Multi-line comment /* */ JavaDoc comment /* */ Multi-line comment cannot be nested.,class nestedComments /* public static void main (String args) /* some comment */ System.out.println(“hello”); */ ,