Bài giảng cấu trúc dữ liệu chương 1 nguyễn xuân vinh

23 274 0
Bài giảng cấu trúc dữ liệu  chương 1   nguyễn xuân vinh

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

GV: NGUYỄN XUÂN VINH CẤU TRÚC DỮ LIỆU DATA STRUCTURES MÔN: CẤU TRÚC DỮ LIỆU [214441] JAVA BASIC /XX 12/3/15 Nguyễn Xuân Vinh nguyenxuanvinh@hcmuaf.edu.vn GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 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 3 /XX 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH Data Types GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 2.1 Primitive Data Types Types Length Values Default value byte 8-bit 8 -2  – short 16-bit -2 int 32-bit -2 long 64-bit -2 float 32-bit IEEE 754 floating point +/-3.40282347E+38  +/- 1.40239846E-45 0.0f double 64-bit IEEE 754 floating point +/-1.79769313486231570E  +/- 0.0d 16 31 63 2 2 2 16 31 63 –1 –1 –1 4.9065645841246544E-324 boolean 1-bit true, false char 16-bit Unicode \u0000 (0)  \uffff (65.535) false /XX 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH 2.1 Primitive Data Types GV: NGUYỄN XUÂN VINH 2.2 Reference Data Types  Reference to Tham chiếu tới giá trị tập hợp giá trị mà biến khai báo /XX 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU  Các kiểu liệu dẫn xuất: GV: NGUYỄN XUÂN VINH 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; /XX 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU char capitalC = 'C'; byte b = 100; short s = 10000; int i = 100000; GV: NGUYỄN XUÂN VINH 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 /XX 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU public void method(int a, double b, boolean c) { } GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU Variables  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 /XX 12/3/15  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 GV: NGUYỄN XUÂN VINH 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 { 10 /XX 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU public void sum(int a) { int x = a + 3; // a , x are local variables } } GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 11 Constant  Từ khóa final dẫn đến biến thay đổi giá trị  Các hàm lớp khai báo final  Hàm final viết chồng  Lớp final lớp static final int VAR = 1; GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 12 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 13 /XX 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH JVM Memory GV: NGUYỄN XUÂN VINH JVM Memory Example class A { B child = new B(); int e; // more code } class B { MÔN: CẤU TRÚC DỮ LIỆU int c; int d; // more code } public static void main(String args[]) { A parent = new A(); // more code 14 /XX 12/3/15 } GV: NGUYỄN XUÂN VINH JVM Memory: Example Heap public void m1() { int x = 20; m2(10); } public void m2(int b) { Account [int] p=0 20 q=0 MÔN: CẤU TRÚC DỮ LIỆU boolean c; //more code m3(); } Stack public void m3() { Account ref = new Account(); //more code } 15 m3 ref int p; m2 b c int q; m1 x public class Account { /XX 12/3/15 } 16 /XX 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH JVM Memory: Example (Array) GV: NGUYỄN XUÂN VINH 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; MÔN: CẤU TRÚC DỮ LIỆU 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; 17 /XX 12/3/15 arrayStudent[2] = s3; String string1 = "abcd"; String string2 = "abcd"; MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH JVM Memory (Exp 5) - String Pool 12/3/15 returning its reference 18 already exists in the pool, the reference of the existing string will be returned, instead of creating a new object and /XX String pool (String intern pool) is a special storage area in Java heap When a string is created and if the string GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 19 JVM Memory: (Exp 7) String Literal vs String Object String String String String String s1 s2 s3 s4 s5 = = = = = "Hello"; // String literal "Hello"; // String literal s1; // same reference new String("Hello"); //String Object new String("Hello"); // String object 20 /XX 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH Mutable vs Immutable GV: NGUYỄN XUÂN VINH 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 21 /XX 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU  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 GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12/3/15 /XX 22 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; } } 23 /XX 12/3/15 MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH HỎI ĐÁP [...]... management 13 /XX 12 /3 /15 MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH 6 JVM Memory GV: NGUYỄN XUÂN VINH 6 JVM Memory Example 1 class A { B child = new B(); int e; // more code } class B { MÔN: CẤU TRÚC DỮ LIỆU int c; int d; // more code } public static void main(String args[]) { A parent = new A(); // more code 14 /XX 12 /3 /15 } GV: NGUYỄN XUÂN VINH 6 JVM Memory: Example 2 Heap public void m1() { int...GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12 /3 /15 /XX 11 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; GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12 /3 /15 /XX 12 6 JVM Memory The JVM divided the memory into following sections 1 2 3... m1() { int x = 20; m2 (10 ); } public void m2(int b) { Account [int] p=0 20 q=0 MÔN: CẤU TRÚC DỮ LIỆU boolean c; //more code m3(); } Stack public void m3() { Account ref = new Account(); //more code } 15 m3 ref int p; m2 b c int q; m1 x public class Account { /XX 12 /3 /15 } 16 /XX 12 /3 /15 MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH 6 JVM Memory: Example 3 (Array) GV: NGUYỄN XUÂN VINH 6 JVM Memory: (Exp... MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH 6 JVM Memory (Exp 5) - String Pool 12 /3 /15 returning its reference 18 already exists in the pool, the reference of the existing string will be returned, instead of creating a new object and /XX String pool (String intern pool) is a special storage area in Java heap When a string is created and if the string GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12 /3 /15 ... int a = 1; int b = 1; int[] arrayInt = new int[2]; arrayInt[0] = a; MÔN: CẤU TRÚC DỮ LIỆU 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; 17 /XX 12 /3 /15 arrayStudent[2] = s3; String string1 = "abcd";... hashcode every time it is used This is more efficient 21 /XX 12 /3 /15 MÔN: CẤU TRÚC DỮ LIỆU  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 GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12 /3 /15 /XX 22 7 Object Immutable & Mutable class Mutable{... DỮ LIỆU 12 /3 /15 /XX 19 6 JVM Memory: (Exp 7) String Literal vs String Object String String String String String s1 s2 s3 s4 s5 = = = = = "Hello"; // String literal "Hello"; // String literal s1; // same reference new String("Hello"); //String Object new String("Hello"); // String object 20 /XX 12 /3 /15 MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH 7 Mutable vs Immutable GV: NGUYỄN XUÂN VINH 7 Why String... Mutable{ private int value; public Mutable(int value) { this.value = value; } } class Immutable { private final int value; public Immutable(int value) { this.value = value; } } 23 /XX 12 /3 /15 MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH HỎI ĐÁP ... /XX 12 /3 /15 MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH Data Types GV: NGUYỄN XUÂN VINH MÔN: CẤU TRÚC DỮ LIỆU 12 /3 /15 /XX 2 .1 Primitive Data Types Types Length Values Default value byte 8-bit 8 -2 ... 0.0d 16 31 63 2 2 2 16 31 63 1 1 1 4.90656458 412 46544E-324 boolean 1- bit true, false char 16 -bit Unicode u0000 (0)  uffff (65.535) false /XX 12 /3 /15 MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN... short 16 -bit -2 int 32-bit -2 long 64-bit -2 float 32-bit IEEE 754 floating point + /-3 .40282347E+38  + /- 1. 40239846E-45 0.0f double 64-bit IEEE 754 floating point +/ -1 . 79769 313 4862 315 70E  + /- 0.0d

Ngày đăng: 03/12/2015, 02:46

Mục lục

  • Slide 1

  • 1. Basic Language Elements

  • 2. Data Types

  • 2.1 Primitive Data Types

  • 2.1 Primitive Data Types

  • 2.2 Reference Data Types

  • 3. Literal

  • 4. Variables

  • Slide 9

  • Instance variables and local variables

  • 5. Constant

  • 6. JVM Memory

  • 6. JVM Memory

  • 6. JVM Memory Example 1

  • 6. JVM Memory: Example 2

  • 6. JVM Memory: Example 3 (Array)

  • 6. JVM Memory: (Exp 4) Primitive vs Reference

  • 6. JVM Memory (Exp 5) - String Pool

  • 6. JVM Memory: (Exp 7) String Literal vs. String Object

  • 7. Mutable vs Immutable

Tài liệu cùng người dùng

Tài liệu liên quan