java学习笔记4--对象的初始化与回收
本文地址:http://www.cnblogs.com/archimedes/p/java-study-note4.html,轉載請注明源地址。
1、對象初始化和回收
對象初始化
系統(tǒng)在生成對象時,會為對象分配內存空間,并自動調用構造方法對實例變量進行初始化
對象回收
對象不再使用時,系統(tǒng)會調用垃圾回收程序將其占用的內存回收
構造方法
-  一種和類同名的特殊方法 
-  用來初始化對象 
-  Java中的每個類都有構造方法,用來初始化該類的一個新的對象 
-  沒有定義構造方法的類,系統(tǒng)自動提供默認的構造方法 
-  方法名與類名相同 
-  沒有返回類型,修飾符void也不能有 
-  通常被聲明為公有的(public) 
-  可以有任意多個參數(shù) 
-  主要作用是完成對象的初始化工作 
-  不能在程序中顯式的調用 
-  在生成一個對象時,系統(tǒng)會自動調用該類的構造方法為新生成的對象初始化 
系統(tǒng)提供的默認構造方法
如果在類的聲明中沒有聲明構造方法,則Java編譯器會提供一個默認的構造方法;默認的構造方法沒有參數(shù),其方法體為空;使用默認的構造方法初始化對象時,如果在類聲明中沒有給實例變量賦初值,則對象的屬性值為零或空
例:聲明一個銀行帳號類及測試代碼
public class BankAccount{String ownerName;int accountNumber;float balance; } public class BankTester{public static void main(String args[]){ BankAccount myAccount = new BankAccount(); System.out.println("ownerName=" + myAccount.ownerName);System.out.println("accountNumber=" + myAccount.accountNumber);System.out.println("balance=" + myAccount.balance);} } 運行結果:ownerName=null
accountNumber=0
balance=0.0
自定義構造方法與方法重載可在生成對象時給構造方法傳送初始值,使用希望的值給對象初始化
構造方法可以被重載,構造方法的重載和方法的重載一致
一個類中有兩個及以上同名的方法,但參數(shù)表不同,這種情況就被稱為方法重載。在方法調用時,Java可以通過參數(shù)列表的不同來辨別應調用哪一個方法為BankAccount聲明一個有三個參數(shù)的構造方法
public BankAccount(String initName, int initAccountNumber, float initBalance) {ownerName = initName;accountNumber = initAccountNumber;balance = initBalance;}假設一個新帳號的初始余額可以為0,則可增加一個帶有兩個參數(shù)的構造方法
public BankAccount(String initName, int initAccountNumber) {ownerName = initName;accountNumber = initAccountNumber;balance = 0.0f; }自定義無參的構造方法
無參的構造方法對其子類的聲明很重要。如果在一個類中不存在無參的構造方法,則要求其子類聲明時必須聲明構造方法,否則在子類對象的初始化時會出錯
在聲明構造方法時,好的聲明習慣是不聲明構造方法;如果聲明,至少聲明一個無參構造方法
構建一個Bush類,有兩個有參數(shù)的構造方法: class Bush {Bush(int i) {}Bush(double d) {}}如果寫:new Bush();?編譯器將要告訴你找不到對應的構造方法
說明:
用戶在進行類聲明時,如果沒有聲明任何構造方法,系統(tǒng)會賦給此類一個默認(無參)的構造方法。但是,只要用戶聲明了構造方法,即使沒有聲明無參的構造方法,系統(tǒng)也不再賦默認的構造方法
例:創(chuàng)建一個擁有兩個構造方法的Tree類,一個有參,一個無參。
import java.util.*; class Tree { int height;Tree() { prt("Planting a seedling"); height = 0; }Tree(int i) { prt("Creating new Tree that is "+ i + " feet tall");height = i;}void info() {prt("Tree is " + height + " feet tall");}void info(String s) {prt(s + ": Tree is " + height + " feet tall"); }static void prt(String s) {System.out.println(s); } }測試Tree類:
public class javatest {public static void main(String[] args) {for(int i = 0; i < 5; i++) {Tree t = new Tree(i);t.info();t.info("overloaded method");}new Tree();} }測試結果:
 Creating new Tree that is 0 feet tall
 Tree is 0 feet tall
 overloaded method: Tree is 0 feet tall
 Creating new Tree that is 1 feet tall
 Tree is 1 feet tall
 overloaded method: Tree is 1 feet tall
 Creating new Tree that is 2 feet tall
 Tree is 2 feet tall
 overloaded method: Tree is 2 feet tall
 Creating new Tree that is 3 feet tall
 Tree is 3 feet tall
 overloaded method: Tree is 3 feet tall
 Creating new Tree that is 4 feet tall
 Tree is 4 feet tall
 overloaded method: Tree is 4 feet tall
 Planting a seedling
this關鍵字的使用:
-  可以使用this關鍵字在一個構造方法中調用另外的構造方法 
-  代碼更簡潔,維護起來也更容易 
-  通常用參數(shù)個數(shù)比較少的構造方法調用參數(shù)個數(shù)最多的構造方法 
使用this關鍵字,修改BankAccout類中無參數(shù)和兩參數(shù)的構造方法:
public BankAccount() { this("", 999999, 0.0f); } public BankAccount(String initName, int initAccountNumber) { this(initName, initAccountNumber, 0.0f); } public BankAccount(String initName, int initAccountNumber, float initBalance) { ownerName = initName; accountNumber = initAccountNumber; balance = initBalance; }2、內存回收技術
當一個對象在程序中不再被使用時,就成為一個無用對象,當前的代碼段不屬于對象的作用域,把對象的引用賦值為空
Java運行時系統(tǒng)通過垃圾收集器周期性地釋放無用對象所使用的內存
Java運行時系統(tǒng)會在對對象進行自動垃圾回收前,自動調用對象的finalize()方法
垃圾收集器
自動掃描對象的動態(tài)內存區(qū),對不再使用的對象做上標記以進行垃圾回收
作為一個線程運行,通常在系統(tǒng)空閑時異步地執(zhí)行
當系統(tǒng)的內存用盡或程序中調用System.gc()要求進行垃圾收集時,與系統(tǒng)同步運行
finalize()方法
-  在類java.lang.Object中聲明,因此 Java中的每一個類都有該方法 
-  用于釋放系統(tǒng)資源,如關閉打開的文件或socket等 
-  聲明格式 
如果一個類需要釋放除內存以外的資源,則需在類中重寫finalize()方法
應用舉例:
對銀行帳戶類BankAccount進行一系列修改和測試:
-  聲明BankAccount類 
-  聲明toString()方法 
-  聲明存取款方法 
-  使用DecimalFormat類 
-  聲明類方法生成特殊的實例 
-  聲明類變量 
- 包括狀態(tài)、構造方法、get方法及set方法
聲明測試類AccountTester
public class AccountTester { public static void main(String args[]) { BankAccount anAccount; anAccount = new BankAccount("ZhangLi", 100023,0); anAccount.setBalance(anAccount.getBalance() + 100); System.out.println("Here is the account: " + anAccount); System.out.println("Account name: " + anAccount.getOwnerName()); System.out.println("Account number: " + anAccount.getAccountNumber()); System.out.println("Balance: $" + anAccount.getBalance());} } 測試結果:Here is the account: BankAccount@372a1a
Account name: ZhangLi
Account number: 100023
Balance: $100.0
聲明toString()方法
將對象的內容轉換為字符串
Java的所有類都有一個默認的toString()方法,其方法體如下:
getClass().getName() + '@' + Integer.toHexString(hashCode())下面的兩行代碼等價:
System.out.println(anAccount); System.out.println(anAccount.toString());如果需要特殊的轉換功能,則需要自己重寫toString()方法
toString()方法的幾點說明
-  必須被聲明為public 
-  返回類型為String 
-  方法的名稱必須為toString,且沒有參數(shù) 
-  在方法體中不要使用輸出方法System.out.println() 
參考資料:
《java程序設計》--清華大學
總結
以上是生活随笔為你收集整理的java学习笔记4--对象的初始化与回收的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: java学习笔记3--类与对象的基础
- 下一篇: java学习笔记5--类的方法
