Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 56 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
56
Dung lượng
597,78 KB
Nội dung
ADVANCED PROGRAMMING JAVA BASICS A Simple Java Program public class FirstSample{ public static void main(String[] args) { System.out.println("Hello, World!"); } } Java is case sensitive The keyword public is called an access modifier The keyword class is there to remind you that everything in a Java program must be inside a class The main method in the source file is necessary in order to execute the program The System.out.println( ) is used to invoke method println of an object System.out Khoa CNTT – ĐH Nông Lâm TP HCM 01/2013 2/56 Comments System.out.println("We will not use 'Hello world!'"); // is this too cute? /* This is the first sample program Copyright (C) by Cay Horstmann and Gary Cornell */ public class FirstSample { public static void main(String[] args) { System.out.println("We will not use 'Hello, World!'"); } } Khoa CNTT – ĐH Nông Lâm TP HCM 01/2013 3/56 Compiling and executing the program The java compiler creates a file called 'First.class' that contains the byte codes To actually run the program, a java interpreter called java is required to execute the code Khoa CNTT – ĐH Nông Lâm TP HCM 01/2013 4/56 Passing Command Line Arguments class CommLineArg { public static void main (String [] pargs) { System.out.println("These are the arguments passed to the main method."); System.out.println(pargs [0]); System.out.println(pargs [1]); System.out.println(pargs [2]); } } Khoa CNTT – ĐH Nông Lâm TP HCM 01/2013 5/56 Passing Command Line Arguments Output Khoa CNTT – ĐH Nông Lâm TP HCM 01/2013 6/56 Identifiers Identifiers are: Text strings that represent variables, methods, classes or labels Case-sensitive Characters can be digit, letter, '$' or '_' Identifiers cannot: Begin with a digit Be the same as a reserved word An_Identifier a_2nd_Identifier Go2 $10 An-Identifier 2nd_Identifier goto 10$ Khoa CNTT – ĐH Nông Lâm TP HCM 01/2013 7/56 Legal Identifiers Identifiers must start with a letter, a currency character ($), or a connecting character such as the underscore ( _ ) Identifiers cannot start with a number! After the first character, identifiers can contain any combination of letters, currency characters, connecting characters, or numbers In practice, there is no limit to the number of characters an identifier can contain You can't use a Java keyword as an identifier Identifiers in Java are case-sensitive; foo and FOO are two different identifiers Khoa CNTT – ĐH Nông Lâm TP HCM 01/2013 8/56 Complete List of Java Keywords Khoa CNTT – ĐH Nông Lâm TP HCM 01/2013 9/56 Java Code Conventions Classes: the names should typically be nouns Dog Account PrintWriter Interfaces: the names should be adjectives Runnable Serializable Methods: the names should be verb-noun pairs getBalance doCalculation setCustomerName Khoa CNTT – ĐH Nông Lâm TP HCM 01/2013 10/56 Creating Arrays An example to create and initialize a primitive (char) array: public char[] createArray() { char[] s; s = new char[26]; for ( int i = 0; i < 26; i++ ) { s[i] = (char) ('A' + i); } return s; } Khoa CNTT – ĐH Nông Lâm TP HCM 01/2013 42/56 Creating object reference Arrays Creating an Array of Point Objects class Point { private int x, y; public Point(int x, int y) { this.x = x; this.y = y; } } public Point[] createArray() { Point[] p; p = new Point[10]; for ( int i=0; i