Java Long类相关方法调用示例

x33g5p2x  于2021-08-19 转载在 Java  
字(11.9k)|赞(0)|评价(0)|浏览(369)

Longclass在一个对象中包装了一个原始类型为long的值。一个Long类型的对象包含一个类型为long的字段。

此外,这个类还提供了一些方法,用于将long转换为String,将String转换为long,以及其他处理long时有用的常量和方法。

Long类的构造函数

  • Long(long value) - 构建一个新分配的Long对象,代表指定的long参数。
  • Long(String s) - 构建一个新分配的Long对象,代表String参数所表示的长值。
    例子。
long l = 55;
String str = "45";

// Construct two Long objects
Long x = new Long(l);
Long y = new Long(str);

Long类方法

图中显示了Long类所提供的API/方法的列表。

在这篇文章中,我们将学习Long封装类的一些重要方法。

toString()方法

  • String toString() - 返回一个代表此Long值的String对象。
  • static String toString(long i) - 返回一个代表指定long的{{$4$$}}对象。
  • static String toString(long i, int radix) - 返回第一个参数在第二个参数指定的radix中的字符串表示。
    例子:
private static void toStringMethods() {
 // toString()
 System.out.println("toString(b) = " + Long.toString(l));

 // toHexString(),toOctalString(),toBinaryString()
 // converts into hexadecimal, octal and binary forms.
 System.out.println("toHexString(b) =" + Long.toHexString(l));
 System.out.println("toOctalString(b) =" + Long.toOctalString(l));
 System.out.println("toBinaryString(b) =" + Long.toBinaryString(l));

}

valueOf()方法

  • static Long valueOf(long l) - 返回一个代表指定长值的Long实例。
  • static Long valueOf(String s) - 返回一个持有指定字符串值的Long对象。
  • static Long valueOf(String s, in - radix) - 返回一个Long对象,该对象持有从指定的String中提取的值,当用第二个参数给出的radix进行解析时。
    例子:
private static void valueOfMethods() {
 // valueOf(): return Long object
 // an overloaded method takes radix as well.
 Long z = Long.valueOf(l);
 System.out.println("valueOf(b) = " + z);
 z = Long.valueOf(str);
 System.out.println("ValueOf(bb) = " + z);
 z = Long.valueOf(str, 6);
 System.out.println("ValueOf(bb,6) = " + z);
}

parseLong()方法

  • static long parseLong(String s) - 将字符串参数解析为有符号的十进制长数。
  • static long parseLong(String s, int radix) - 将字符串参数解析为第二个参数指定的弧度的有符号长字符串。
    例子:
private static void parseLongMethods() {
 // parseLong(): return primitive long value
 // an overloaded method takes radix as well
 long zz = Long.parseLong(str);
 System.out.println("parseLong(bb) = " + zz);
 zz = Long.parseLong(str, 6);
 System.out.println("parseLong(bb,6) = " + zz);

}

rotateLeft()和rotateRight()方法

  • static long rotateLeft(long i, int distance) - 返回将指定的长数值的二进制补码向左旋转指定位数后得到的数值。
  • static long rotateRight(long i, int distance) - 返回将指定的长数值的二进制补码向右旋转指定位数得到的数值。
    例子:
private static void rotateMethods() {
 // rotateLeft and rotateRight can be used
 // to rotate bits by specified distance
 long valrot = 2;
 System.out.println("rotateLeft(0000 0000 0000 0010 , 2) =" + Long.rotateLeft(valrot, 2));
 System.out.println("rotateRight(0000 0000 0000 0010,3) =" + Long.rotateRight(valrot, 3));

}

decode()方法

  • static Long decode(String nm) - 将一个字符串解码成一个长字符串。
private static void decodeMethods() {

 // decode() : decodes the hex,octal and decimal
 // string to corresponding long values.
 String decimal = "45";
 String octal = "005";
 String hex = "0x0f";

 Long dec = Long.decode(decimal);
 System.out.println("decode(45) = " + dec);
 dec = Long.decode(octal);
 System.out.println("decode(005) = " + dec);
 dec = Long.decode(hex);
 System.out.println("decode(0x0f) = " + dec);

}

getLong()方法

  • static Long getLong(String nm) - 确定指定名称的系统属性的长值。
  • static Long getLong(String nm, long val) - 确定指定名称的系统属性的长值。
  • static Long getLong(String nm, Long val) - 返回指定名称的系统属性的长值。
    例如。
private static void getLongMethods() {
 // getLong(): can be used to retrieve
 // long value of system property
 long prop = Long.getLong("sun.arch.data.model");
 System.out.println("getLong(sun.arch.data.model) = " + prop);
 System.out.println("getLong(abcd) =" + Long.getLong("abcd"));

 // an overloaded getLong() method
 // which return default value if property not found.
 System.out.println("getLong(abcd,10) =" + Long.getLong("abcd", 10));
}

intValue、byteValue、shortValue、longValue、doubleValue、floatValue()方法

private static void valueMethods() {
 Long x = new Long(l);
 // xxxValue can be used to retrieve
 // xxx type value from long value.
 // xxx can be int,byte,short,long,double,float
 System.out.println("bytevalue(x) = " + x.byteValue());
 System.out.println("shortvalue(x) = " + x.shortValue());
 System.out.println("intvalue(x) = " + x.intValue());
 System.out.println("longvalue(x) = " + x.longValue());
 System.out.println("doublevalue(x) = " + x.doubleValue());
 System.out.println("floatvalue(x) = " + x.floatValue());
}

还有一些方法,请参考下面程序中的注释,这些都是自我描述。

public static void main(String[] args) {
 long l = 55;
 String str = "45";

 // Construct two Long objects
 Long x = new Long(l);
 Long y = new Long(str);

 long value = 45;

 // bitcount() : can be used to count set bits
 // in twos complement form of the number
 System.out.println("Long.bitcount(value)=" + Long.bitCount(value));

 // numberOfTrailingZeroes and numberOfLeaadingZeroes
 // can be used to count prefix and postfix sequence of 0
 System.out.println("Long.numberOfTrailingZeros(value)=" + Long.numberOfTrailingZeros(value));
 System.out.println("Long.numberOfLeadingZeros(value)=" + Long.numberOfLeadingZeros(value));

 // highestOneBit returns a value with one on highest
 // set bit position
 System.out.println("Long.highestOneBit(value)=" + Long.highestOneBit(value));

 // highestOneBit returns a value with one on lowest
 // set bit position
 System.out.println("Long.lowestOneBit(value)=" + Long.lowestOneBit(value));

 // reverse() can be used to reverse order of bits
 // reverseytes() can be used to reverse order of bytes
 System.out.println("Long.reverse(value)=" + Long.reverse(value));
 System.out.println("Long.reverseBytes(value)=" + Long.reverseBytes(value));

 // signum() returns -1,0,1 for negative,0 and positive
 // values
 System.out.println("Long.signum(value)=" + Long.signum(value));

 // hashcode() returns hashcode of the object
 int hash = x.hashCode();
 System.out.println("hashcode(x) = " + hash);

 // equals returns boolean value representing equality
 boolean eq = x.equals(y);
 System.out.println("x.equals(y) = " + eq);

 // compare() used for comparing two int values
 int e = Long.compare(x, y);
 System.out.println("compare(x,y) = " + e);

 // compareTo() used for comparing this value with some
 // other value
 int f = x.compareTo(y);
 System.out.println("x.compareTo(y) = " + f);
}

输出。

toString(b) = 55
toHexString(b) =37
toOctalString(b) =67
toBinaryString(b) =110111
decode(45) = 45
decode(005) = 5
decode(0x0f) = 15
getLong(sun.arch.data.model) = 64
getLong(abcd) =null
getLong(abcd,10) =10
parseLong(bb) = 45
parseLong(bb,6) = 29
rotateLeft(0000 0000 0000 0010 , 2) =8
rotateRight(0000 0000 0000 0010,3) =4611686018427387904
bytevalue(x) = 55
shortvalue(x) = 55
intvalue(x) = 55
longvalue(x) = 55
doublevalue(x) = 55.0
floatvalue(x) = 55.0
valueOf(b) = 55
ValueOf(bb) = 45
ValueOf(bb,6) = 29
Long.bitcount(value)=4
Long.numberOfTrailingZeros(value)=0
Long.numberOfLeadingZeros(value)=58
Long.highestOneBit(value)=32
Long.lowestOneBit(value)=1
Long.reverse(value)=-5476377146882523136
Long.reverseBytes(value)=3242591731706757120
Long.signum(value)=1
hashcode(x) = 55
x.equals(y) = false
compare(x,y) = 1
x.compareTo(y) = 1

###完整的程序供参考

package com.javaguides.corejava.wrapperclasses;

public class LongClassExample {

 static long l = 55;
 static String str = "45";

 private static void toStringMethods() {
  // toString()
  System.out.println("toString(b) = " + Long.toString(l));

  // toHexString(),toOctalString(),toBinaryString()
  // converts into hexadecimal, octal and binary forms.
  System.out.println("toHexString(b) =" + Long.toHexString(l));
  System.out.println("toOctalString(b) =" + Long.toOctalString(l));
  System.out.println("toBinaryString(b) =" + Long.toBinaryString(l));

 }

 private static void valueOfMethods() {
  // valueOf(): return Long object
  // an overloaded method takes radix as well.
  Long z = Long.valueOf(l);
  System.out.println("valueOf(b) = " + z);
  z = Long.valueOf(str);
  System.out.println("ValueOf(bb) = " + z);
  z = Long.valueOf(str, 6);
  System.out.println("ValueOf(bb,6) = " + z);
 }

 private static void parseLongMethods() {
  // parseLong(): return primitive long value
  // an overloaded method takes radix as well
  long zz = Long.parseLong(str);
  System.out.println("parseLong(bb) = " + zz);
  zz = Long.parseLong(str, 6);
  System.out.println("parseLong(bb,6) = " + zz);

 }

 private static void getLongMethods() {
  // getLong(): can be used to retrieve
  // long value of system property
  long prop = Long.getLong("sun.arch.data.model");
  System.out.println("getLong(sun.arch.data.model) = " + prop);
  System.out.println("getLong(abcd) =" + Long.getLong("abcd"));

  // an overloaded getLong() method
  // which return default value if property not found.
  System.out.println("getLong(abcd,10) =" + Long.getLong("abcd", 10));
 }

 private static void decodeMethods() {

  // decode() : decodes the hex,octal and decimal
  // string to corresponding long values.
  String decimal = "45";
  String octal = "005";
  String hex = "0x0f";

  Long dec = Long.decode(decimal);
  System.out.println("decode(45) = " + dec);
  dec = Long.decode(octal);
  System.out.println("decode(005) = " + dec);
  dec = Long.decode(hex);
  System.out.println("decode(0x0f) = " + dec);

 }

 private static void rotateMethods() {
  // rotateLeft and rotateRight can be used
  // to rotate bits by specified distance
  long valrot = 2;
  System.out.println("rotateLeft(0000 0000 0000 0010 , 2) =" + Long.rotateLeft(valrot, 2));
  System.out.println("rotateRight(0000 0000 0000 0010,3) =" + Long.rotateRight(valrot, 3));

 }

 private static void valueMethods() {
  Long x = new Long(l);
  // xxxValue can be used to retrieve
  // xxx type value from long value.
  // xxx can be int,byte,short,long,double,float
  System.out.println("bytevalue(x) = " + x.byteValue());
  System.out.println("shortvalue(x) = " + x.shortValue());
  System.out.println("intvalue(x) = " + x.intValue());
  System.out.println("longvalue(x) = " + x.longValue());
  System.out.println("doublevalue(x) = " + x.doubleValue());
  System.out.println("floatvalue(x) = " + x.floatValue());
 }

 public static void main(String[] args) {
  long l = 55;
  String str = "45";

  // Construct two Long objects
  Long x = new Long(l);
  Long y = new Long(str);

  toStringMethods();
  decodeMethods();
  getLongMethods();
  parseLongMethods();
  rotateMethods();
  valueMethods();
  valueOfMethods();
  
  
  long value = 45;

  // bitcount() : can be used to count set bits
  // in twos complement form of the number
  System.out.println("Long.bitcount(value)=" + Long.bitCount(value));

  // numberOfTrailingZeroes and numberOfLeaadingZeroes
  // can be used to count prefix and postfix sequence of 0
  System.out.println("Long.numberOfTrailingZeros(value)=" + Long.numberOfTrailingZeros(value));
  System.out.println("Long.numberOfLeadingZeros(value)=" + Long.numberOfLeadingZeros(value));

  // highestOneBit returns a value with one on highest
  // set bit position
  System.out.println("Long.highestOneBit(value)=" + Long.highestOneBit(value));

  // highestOneBit returns a value with one on lowest
  // set bit position
  System.out.println("Long.lowestOneBit(value)=" + Long.lowestOneBit(value));

  // reverse() can be used to reverse order of bits
  // reverseytes() can be used to reverse order of bytes
  System.out.println("Long.reverse(value)=" + Long.reverse(value));
  System.out.println("Long.reverseBytes(value)=" + Long.reverseBytes(value));

  // signum() returns -1,0,1 for negative,0 and positive
  // values
  System.out.println("Long.signum(value)=" + Long.signum(value));

  // hashcode() returns hashcode of the object
  int hash = x.hashCode();
  System.out.println("hashcode(x) = " + hash);

  // equals returns boolean value representing equality
  boolean eq = x.equals(y);
  System.out.println("x.equals(y) = " + eq);

  // compare() used for comparing two int values
  int e = Long.compare(x, y);
  System.out.println("compare(x,y) = " + e);

  // compareTo() used for comparing this value with some
  // other value
  int f = x.compareTo(y);
  System.out.println("x.compareTo(y) = " + f);
 }

}

输出。

toString(b) = 55
toHexString(b) =37
toOctalString(b) =67
toBinaryString(b) =110111
decode(45) = 45
decode(005) = 5
decode(0x0f) = 15
getLong(sun.arch.data.model) = 64
getLong(abcd) =null
getLong(abcd,10) =10
parseLong(bb) = 45
parseLong(bb,6) = 29
rotateLeft(0000 0000 0000 0010 , 2) =8
rotateRight(0000 0000 0000 0010,3) =4611686018427387904
bytevalue(x) = 55
shortvalue(x) = 55
intvalue(x) = 55
longvalue(x) = 55
doublevalue(x) = 55.0
floatvalue(x) = 55.0
valueOf(b) = 55
ValueOf(bb) = 45
ValueOf(bb,6) = 29
Long.bitcount(value)=4
Long.numberOfTrailingZeros(value)=0
Long.numberOfLeadingZeros(value)=58
Long.highestOneBit(value)=32
Long.lowestOneBit(value)=1
Long.reverse(value)=-5476377146882523136
Long.reverseBytes(value)=3242591731706757120
Long.signum(value)=1
hashcode(x) = 55
x.equals(y) = false
compare(x,y) = 1
x.compareTo(y) = 1

###参考资料

https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html

相关文章