Java——對Integer類中的幾個常用方法做一簡單的介紹

適用於初學者,使其更能深層次的理解Integer類中的方法!!!!!!!

package com.day.test01;

public class IntegerTest {
	public static void main(String[] args) {
		/*
		 * private final int value;
		 * 
		 * 初始化成員變量
		 * public Integer(int value) {
		 *		this.value = value;
    	 *	}
		 */
		Integer i = new Integer('d');
		Integer i0=new Integer('d');
		/*
		 * 
		 * toString()返回的只是一個字符串,並且切忌的是該toString()方法並
不是重寫得來的,是該類中自己的方法
		 */
		System.out.println(i.toString()+"abc");
		/*
		 * byteValue(),這個方法的意思是強制類型的轉換,將大於Byte類型數值強制轉換成Byte
		 * public byte byteValue() {
         *		return (byte)value;
    	 *  }
		 *
		 */
		i.byteValue();
		System.out.println(i.byteValue());
		/*
		 * 該方法比較的是對象所指向堆中地址中所存儲的內容,而並不是地址
		 * public int compareTo(Integer anotherInteger) {
         *		return compare(this.value, anotherInteger.value);
    	 *	}
    	 *	源碼:
    	 *public static int compare(int x, int y) {
    	 *		return (x < y) ? -1 : ((x == y) ? 0 : 1);
    	 *	}
    	 *如果大於則返回1,等於放回0,小於返回-1
		 */
		i.compareTo(i0);
		System.out.println(i.compareTo(i0));
		/*
		 * 源碼:首先是傳入一個對象,先判斷該對象是否屬於該類,如果屬於將傳進來的對象強制類型轉換成該類的對象在將該對象所指向堆中所存儲
		 * 的地址所指向的內容,強制轉換成整型int
		 * 該方法比較的也是內容
		 * public boolean equals(Object obj) {
         *	if (obj instanceof Integer) {
         *   	return value == ((Integer)obj).intValue();
         *	}
         *	return false;
         *}
		 */
		i.equals(i0);
		System.out.println(i.equals(i0));
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章