MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
CMSC 202 Exceptions 2 nd Lecture. Aug 7, Methods may fail for multiple reasons public class BankAccount { private int balance = 0, minDeposit =
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
Road Map Introduction to object oriented programming. Classes
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
ECE122 Feb. 22, Any question on Vehicle sample code?
10-Nov-15 Java Object Oriented Programming What is it?
Programming in Java CSCI-2220 Object Oriented Programming.
Li Tak Sing COMPS311F. Threads A thread is a single sequential flow of control within a program. Many programming languages only allow you to write programs.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Classes, Interfaces and Packages
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
OOP Basics Classes & Methods (c) IDMS/SQL News
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Topic: Classes and Objects
JAVA MULTIPLE CHOICE QUESTION.
I/O Basics.
Constructor Overloading
An Introduction to Java – Part I, language basics
Packages and Interfaces
Object Oriented Programming in java
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions 2nd Lecture.
CS139 October 11, 2004.
Object Oriented Programming in java
Java Programming Language
Classes, Objects and Methods
CMSC 202 Exceptions 2nd Lecture.
Classes and Objects Object Creation
Chapter 7 Objects and Classes
CMSC 202 Constructors Version 9/10.
Presentation transcript:

MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )

Constructor A constructor is like a method except that – it should have the same name as the class; – it should have no return type. A constructor will be called when you use the 'new' keyword to create an instance of a class.

Default Constructors If you do not provide any constructor, the Java will create a default constructor for it.

Default Constructors When the default constructor is executed, all its attributes will be initialized according to the following rules: – if the attribute is numerical, like int, byte, float etc., the attribute is assigned the value of 0. – if the attribute is a class variable, then it is initialized to "null" which represent that the variable is not referring to any object yet. – if the attribute is boolean, then it will be assigned the value of false.

Default Constructors public class ABC { int a; float b; boolean c; ABC d; public static void main(String st[]) { ABC abc=new ABC(); //default constructor System.out.println(abc.a+" "+abc.b+" "+abc.c+" "+abc.d); }

Default Constructors The output of the above program is: false null

Default Constructors If you want to initialize the attributes to other value, you can do it like this: public class ABC { int a=1; float b=2; boolean c=true; ABC d=null; public static void main(String st[]) { ABC abc=new ABC(); System.out.println(abc.a+" "+abc.b+" "+abc.c+" "+abc.d); }

Default Constructors The output of the above program is: true null

Constructors The above method has a problem in that all instance of ABC would be the same when they are just created, i.e., the attribute a will definitely be 1, the attribute b will be 2.0 etc. If we want to have the attributes initialized to different values depending on different situations, we need to write our own constructors.

Constructor A constructor is a piece of code that will be executed when a new instance of class is created. Just like methods, a constructor can have a number of parameters. Just like methods, you overload a constructor provided that these constructors have different signatures. Unlike methods, a constructor cannot have return value.

Constructors public class ABC { int a=1; float b=2; boolean c=true; public ABC(int aa) { a=aa; } public ABC(float bb) { b=bb; } public ABC(boolean cc) { c=cc; }

Constructors... ABC a1=new ABC(); //error, now ABC does not have a default // constructors ABC a2=new ABC(10); //a2.a=10, a2.b= 2.0, a2.c = true ABC a3=new ABC(10.0); //a3.a=1, a3.b=10.0, a3.c= true ABC a4=new ABC(false); //a3.a=1, a3.b=2.0, a3.c=false.

Constructors Consider a change in ABC: public class ABC { int a=1; float b=2; boolean c=true; public ABC(float bb) { b=bb; } public ABC(boolean cc) { c=cc; }

Constructor... ABC a1=new ABC(); //error, now ABC does not have a default // constructors ABC a2=new ABC(10); //a2.a=1, a2.b= 10.0, a2.c = true ABC a3=new ABC(10.0); //a3.a=1, a3.b=10.0, a3.c= true ABC a4=new ABC(false); //a3.a=1, a3.b=2.0, a3.c=false.

Garbage collection When you create a new object, it takes up the computer's resources. So as you create more and more objects, it is possible that all the computer's resources are used up. So it is necessary to release those resources that have been taken up by objects that will never be used again.

Garbage Collection Consider the following code: for (int i=0;i<10;i++) { ABC abc=new ABC();.... } In each loop, an ABC is created. Assume that once the loop is finished, these ABCs are no longer used and referenced, then the resources taken up by these ABCs can be released to other use.

Garbage collection In Java, this is done by a process called garbage collection. From time to time, the process will check whether there are resources that are taken up by objects that are no long used. It will then release the resources back to the system.

Finalize When the garbage collection process is releasing the resource taken up by an object, it is possible that this object is holding some important information. For example, this object may have an opened file with some contents that have not been written to the file properly. So by releasing the resources held by the object may result in loosing this contents.

Finalize In order to prevent this from happening, we need to have some code to close the file before the resources are released. We need to use the finalize method for this purpose.

Finalize The finalize method must have no parameters and no return type. So it is always declared as: public void finalize() When the garbage collector is going to release the resource of an object, it will invoke the finalize method of the object first.

Finalize So it is the programmer's responsibility to put the code in the finalize method to do whatever it need to do before the resources are released, for example, close a file or close a network connection. However, in real Java applications, you rarely use the finalize method because it is usually good habit to close a file when you are done with the file. Not to wait until the object is garbage collected.

Packages A package is a collection of classes. It is very similar to directories in a file system. In a file system, a directory can have a number of other directories and files. So a package can contain a number of other packages and classes.

Packages For example, you have been using the following command many times: System.out.println("...."); Actually, System is a class defined in Package java.lang. So here, java is a package, lang is also a package that is defined in java and then System is a class defined in lang. Then, out is a static object of the class System. println is a method of the object out.

Packages So actually, the above statement can be changed as: java.lang.System.out.println("...");

Packages The following statement in a java file define the package that the class is in: package myPackage; When a class is defined in myPackage, then the class file should be put under a directory called myPackage.

Packages Consider the following java file: package abc.def; public class MyClass { public void method() {.... } static int aStaticVariable=2; }

Packages After compilation of the file, the MyClass.class file should be put in the following directory: abc/def When other programs wants to use MyClass, it can be referred to as: abc.def.MyClass

Packages If you do not want to use that long name, you can have the following statement: import abc.def.MyClass; This statement will tell the compiler where to find MyClass and therefore we can simply refer to the class by using MyClass, not the full name.

Packages The way of using the import keyword is: import abc.def.*; This statement inform the compiler to look for all classes in the package abc.def, not just MyClass as that in the last statement. So all classes defined in the directory abc/def can be used without the need of the full name.

Packages So, when you want to use a class ABC in a package def.ghi, you can do either of the following ways: – use the full name: def.ghi.ABC – include the statement: import def.ghi.ABC; and then you can refer to the class as ABC in the program. – include the statement: import def.ghi.*; and then you can refer to the class as ABC.

Packages There are three situations where you do not need to specify the package at all: – The code that uses the class is in the same package of the class. – The class belongs to the package java.lang. This package hold the most fundamental classes of Java and therefore there is no need to use either the import statement or the full name for classes in that directory. – The class does not belong to any package.

Packages That is why when you use System.out.println(".."), you do not need to preceed it with java.lang.

Packages When you create a project in Netbeans, you will be asked about the package name. Then, usually, when you create a class, the class will be placed in that package.

Where to find a package We know that when a class ABC is defined in a package def.ghi, then ABC.class should be found in the directory def/ghi. However, how can the Java system knows where is def/ghi? The is done by either using the environment variable called CLASSPATH or you can set the classpath when you run the java command: java -classpath=... ABC

Where to find a package The other way is that Java will look for the current directory. ABC.class is actually at: /jkl/mno/def/ghi/ABC.class, Then, in order for the Java interpreter to find the class, we can set the classpath to /jkl/mno or we run the command at the directory /jkl/mno.