CS3101-3 Programming Language-Java Fall 2004 Sept.29
CS3101-3 Programming Language – Java Fall 2004 Sept. 29
Road Map today ●Java review ●Homework review ●Exception revisited ●Containers ●l/O
Road Map today lJava review lHomework review lException revisited lContainers lI/O
What is Java OA programming language OA virtual machine JVM OA runtime environment -JRE OPredefined libraries ●Portable,but slow OInterpreter OJIT helps
What is Java lA programming language lA virtual machine – JVM lA runtime environment – JRE ¡Predefined libraries lPortable, but slow ¡Interpreter ¡JIT helps
Object and class ●A class is a blueprint OAn object is an instance created from that blueprint DAll objects of the same class have the same set of attributes OEvery Person object have name,weight,height o But different value for those attributes Oke.name=Ke Wang,sal.name=Sal Stolfo
Object and class lA class is a blueprint lAn object is an instance created from that blueprint lAll objects of the same class have the same set of attributes ¡Every Person object have name, weight, height lBut different value for those attributes ¡ke.name=Ke Wang, sal.name=Sal Stolfo
Class Person:illustration Name:Ke Wang ke height:0 weight:0 Name:Salvatore J.Stolfo sal height:0 weight:0
Class Person: illustration Name: Ke Wang height: 0 weight: 0 Name: Salvatore J. Stolfo height: 0 weight: 0 ke sal
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