Chương 1 Java Basic

Identifiers A name in a program is called an identifier An identifier in java is composed of a sequence of characters (include: letter, digits or connecting symbols ($, _). The first character in an identifier can’t be a digit. Identifiers in java are case sensitive. Keywords Key words are reserved identifiers. In java, all keywords are in lower case.

pptx22 trang | Chia sẻ: lylyngoc | Lượt xem: 1735 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Chương 1 Java Basic, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level 20/09/2013 ‹#› /XX MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH JAVA BASIC Nguyễn Xuân Vinh nguyenxuanvinh@hcmuaf.edu.vn CẤU TRÚC DỮ LIỆU DATA STRUCTURES [214441] 1. Basic Language Elements Identifiers A name in a program is called an identifier An identifier in java is composed of a sequence of characters (include: letter, digits or connecting symbols ($, _). The first character in an identifier can’t be a digit. Identifiers in java are case sensitive. Keywords Key words are reserved identifiers. In java, all keywords are in lower case. Literals A literal denotes a constant value of a particular data type The value a literal represents remains unchanged in the program. 2. Data Types 2.1 Primitive Data Types Types Length Values Default value byte 8-bit -28  28 – 1 0 short 16-bit -216  216 – 1 0 int 32-bit -231  231 – 1 0 long 64-bit -263  263 – 1 0 float 32-bit IEEE 754 floating point +/-3.40282347E+38  +/- 1.40239846E-45 0.0f double 64-bit IEEE 754 floating point +/-1.79769313486231570E  +/-4.9065645841246544E-324 0.0d boolean 1-bit true, false false char 16-bit Unicode \u0000 (0)  \uffff (65.535) 2.1 Primitive Data Types 2.2 Reference Data Types Reference to Tham chiếu tới một giá trị hay là tập hợp các giá trị mà biến khai báo. Các kiểu dữ liệu dẫn xuất: 3. Literal The new keyword isn't used when initializing a variable of a primitive type. A literal is the source code representation of a fixed value. Literals are represented directly in your code without requiring computation boolean result = true; char capitalC = 'C'; byte b = 100; short s = 10000; int i = 100000; 4. Variables Parameters The variables that are listed as part of a method declaration. Each parameter must have a unique name and a defined data type. public void method(int a, double b, boolean c) { ... } Instance variables Every object of the class will have its own copies of these variables, which are local to object The values of these variables at any given time constitute the state of the object Instance variables exist as long as object they belong to exist Static variables Belong only to the class, but not created for only object of the class All objects of the class share the same copy of this variable Class variables exist as long as class exist Local Variables Variables declared in methods including parameters or blocks are called Local variables After execution of the method or block completes local variables are no longer accessible 4. Variables Instance variables and local variables Instance variables are declared inside a class but not inside a method public class Student{ int num; // num is instance variable public void method(){} } Local variables are declared inside a method including method arguments. public class Student { public void sum(int a) { int x = a + 3; // a , x are local variables } } 5. Constant Từ khóa final chỉ dẫn đến 1 biến không thể thay đổi giá trị. Các hàm và lớp cũng có thể được khai báo final Hàm final không thể viết chồng Lớp final không thể là lớp con static final int VAR = 1; 6. JVM Memory The JVM divided the memory into following sections. Heap: contains Objects (may also contain reference variables). Stack: contains methods, local variables and reference variables. Code: contains your bytecode Static: contains Static data/methods. This division of memory is required for its effective management. 6. JVM Memory 6. JVM Memory Example 1 class A { B child = new B(); int e; // more code } class B { int c; int d; // more code } public static void main(String args[]) { A parent = new A(); // more code } 6. JVM Memory: Example 2 public void m1() { int x = 20; m2(10); } public void m2(int b) { boolean c; //more code m3(); } public void m3() { Account ref = new Account(); //more code } public class Account { int p; int q; } Heap m3 ref m2 b c m1 x Stack Account p=0 q=0 [int] 20 6. JVM Memory: Example 3 (Array) 6. JVM Memory: (Exp 4) Primitive vs Reference Primitive Data Types (byte,short,int,long,float,double,Boolean,chat) int a = 1; int b = 1; int[] arrayInt = new int[2]; arrayInt[0] = a; arrayInt[1] = b; Reference Data Type: Student (int sid, String name) Student s1 = new Student(1, “A”); Student s2 = new Student(2, “B”); Student s3 = new Student(1, “A”); Student[] arrayStudent = new Student[3]; arrayStudent[0] = s1; arrayStudent[1] = s2; arrayStudent[2] = s3; 6. JVM Memory (Exp 5) - String Pool String string1 = "abcd"; String string2 = "abcd"; String pool (String intern pool) is a special storage area in Java heap. When a string is created and if the string already exists in the pool, the reference of the existing string will be returned, instead of creating a new object and returning its reference. 6. JVM Memory: (Exp 7) String Literal vs. String Object String s1 = "Hello"; // String literal String s2 = "Hello"; // String literal String s3 = s1; // same reference String s4 = new String("Hello"); //String Object String s5 = new String("Hello"); // String object 7. Mutable vs Immutable 7. Why String is immutable Allow String to Cache its Hashcode The hashcode of string is frequently used in Java. For example, in a HashMap. Being immutable guarantees that hashcode will always the same, so that it can be cashed without worrying the changes.That means, there is no need to calculate hashcode every time it is used. This is more efficient. Security String is widely used as parameter for many java classes, e.g. network connection, opening files, etc. Were String not immutable, a connection or file would be changed and lead to serious security threat. 7. Object Immutable & Mutable class Mutable{ private int value; public Mutable(int value) { this.value = value; } }   class Immutable { private final int value; public Immutable(int value) { this.value = value; } } HỎI ĐÁP