Reference Person ke; //only created the reference,not an object. It points to nothing now(null). ke new Person(); //create the object (allocate storage in memory),and ke is initialized. ke.name=“Ke Wang”; //access the object through the reference Can have multiple reference to one object No reference means the object is inaccessible forever goes to garbage collector
Reference Person ke; //only created the reference, not an object. It points to nothing now (null). ke = new Person(); //create the object (allocate storage in memory), and ke is initialized. ke.name=“Ke Wang”; //access the object through the reference Can have multiple reference to one object No reference means the object is inaccessible forever – goes to garbage collector
Class Person:variables Name:Ke Wang ke height:0 weight:0 Name:Salvatore J.Stolfo sal height:0 weight:0 references objects
Class Person: variables Name: Ke Wang height: 0 weight: 0 Name: Salvatore J. Stolfo height: 0 weight: 0 ke sal x references objects
Arrays in Java:declaration ●Declaration Oint]arr, OPerson[]persons; OAlso support:int arr[];Person persons[] (confusing,should be avoided) ●Creation Oint[]arr new int[1024]; ○int[00arr={{1,2,3},{4,5,6}}; OPerson[]persons new Person[50];
Arrays in Java: declaration lDeclaration ¡int[] arr; ¡Person[] persons; ¡Also support: int arr[]; Person persons[]; (confusing, should be avoided) lCreation ¡int[] arr = new int[1024]; ¡int [][] arr = { {1,2,3}, {4,5,6} }; ¡Person[] persons = new Person[50];
Arrays in Java:safety o Cannot be accessed outside of its range OArrayIndexOutOfBoundsException o Guaranteed to be initialized OArray of primitive type will be initialized to their default value oZeroes the memory for the array OArray of objects:actually it's creating an array of references,and each of them is initialized to null
Arrays in Java: safety lCannot be accessed outside of its range ¡ArrayIndexOutOfBoundsException lGuaranteed to be initialized ¡Array of primitive type will be initialized to their default value lZeroes the memory for the array ¡Array of objects: actually it’s creating an array of references, and each of them is initialized to null
Arrays in Java: second kind of reference types in Java int[]arr new int [5]; arr int arr new int [2][5]; arr arr[0] arr[1]
Arrays in Java: lsecond kind of reference types in Java int[] arr = new int [5]; arr int[][] arr = new int [2][5]; arr[0] arr[1] arr