Longclass在一个对象中包装了一个原始类型为long的值。一个Long类型的对象包含一个类型为long的字段。
此外,这个类还提供了一些方法,用于将long转换为String,将String转换为long,以及其他处理long时有用的常量和方法。
long l = 55;
String str = "45";
// Construct two Long objects
Long x = new Long(l);
Long y = new Long(str);
图中显示了Long类所提供的API/方法的列表。
在这篇文章中,我们将学习Long封装类的一些重要方法。
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 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 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 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 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
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.javaguides.net/2018/08/long-wrapper-class-in-java.html
内容来源于网络,如有侵权,请联系作者删除!