java byte to byte_Java Byte compareTo()方法

Java Byte compareTo()方法

java.lang.Byte.compareTo(Byte anotherByte) 比较两个字节的对象上的数字。

1 语法

public int compareTo(Byte anotherByte)

2 参数

anotherByte:该字节进行比较

3 返回值

此方法返回值0,如果这个字节等于参数字节;值小于0,如果这个字节数值大于参数字节以内;和一个大于0的值,如果这个字节数值大于参数字节(符号比较)。

4 示例

package com.yiidian;

/**

* 一点教程网: http://www.yiidian.com

*/

/**

* Java Byte compareTo()方法

*/

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 < 0 ){

System.out.println( str3 );

}

}

}

输出结果为:

Second value is greater

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值