天天看點

java lang compare,java.lang.Byte.compareTo()方法執行個體

全屏

java.lang.Byte.compareTo(Byte anotherByte)比較兩個位元組的對象上的數字。

聲明

以下是java.lang.Byte.compareTo()方法的聲明public int compareTo(Byte anotherByte)

Specified by

compareTo 在接口 Comparable

參數anotherByte - 該位元組進行比較

傳回值

此方法傳回值0,如果這個位元組等于參數位元組;值小于0,如果這個位元組數值大于參數位元組以内;和一個大于0的值,如果這個位元組數值大于參數位元組(符号比較)。

異常NA

例子

下面的例子顯示lang.Byte.compareTo()方法的使用。package cn.sxt;

import java.lang.*;

public class ByteDemo {

public static void main(String[] args) {

// create 2 Byte objects b1, b2

Byte b1, b2;

// assign values to b1, b2

b1 = new Byte("-100");

b2 = new Byte("10");

// create an int res

int res;

// compare b1 with b2 and assign result to res

res = b1.compareTo(b2);

String str1 = "Both values are equal ";

String str2 = "First value is greater";

String str3 = "Second value is greater";

if( res == 0 ){

System.out.println( str1 );

}

else if( res > 0 ){

System.out.println( str2 );

}

else if( res 

System.out.println( str3 );

}

}

}

讓我們來編譯和運作上面的程式,這将産生以下結果:Second value is greater

分享到:

0評論

java lang compare,java.lang.Byte.compareTo()方法執行個體