?? long.java
字號:
/* * @(#)Long.java 1.59 06/10/10 * * Copyright 1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package java.lang;/** * The <code>Long</code> class wraps a value of the primitive type * <code>long</code> in an object. An object of type <code>Long</code> * contains a single field whose type is <code>long</code>. * * <p> * * In addition, this class provides several methods for converting a * <code>long</code> to a <code>String</code> and a * <code>String</code> to a <code>long</code>, as well as other * constants and methods useful when dealing with a <code>long</code>. * * @author Lee Boynton * @author Arthur van Hoff * @version 1.52, 02/02/00 * @since JDK1.0 */public final class Long extends Number implements Comparable { /** * A constant holding the minimum value a <code>long</code> can * have, -2<sup>63</sup>. */ public static final long MIN_VALUE = 0x8000000000000000L; /** * A constant holding the maximum value a <code>long</code> can * have, 2<sup>63</sup>-1. */ public static final long MAX_VALUE = 0x7fffffffffffffffL; /** * The <code>Class</code> instance representing the primitive type * <code>long</code>. * * @since JDK1.1 */ public static final Class TYPE = Class.getPrimitiveClass("long"); /** * Returns a string representation of the first argument in the * radix specified by the second argument. * <p> * If the radix is smaller than <code>Character.MIN_RADIX</code> * or larger than <code>Character.MAX_RADIX</code>, then the radix * <code>10</code> is used instead. * <p> * If the first argument is negative, the first element of the * result is the ASCII minus sign <code>'-'</code> * (<code>'\u002d'</code>). If the first argument is not * negative, no sign character appears in the result. * <p> * The remaining characters of the result represent the magnitude * of the first argument. If the magnitude is zero, it is * represented by a single zero character <code>'0'</code> * (<code>'\u0030'</code>); otherwise, the first character of * the representation of the magnitude will not be the zero * character. The following ASCII characters are used as digits: * <blockquote><pre> * 0123456789abcdefghijklmnopqrstuvwxyz * </pre></blockquote> * These are <code>'\u0030'</code> through * <code>'\u0039'</code> and <code>'\u0061'</code> through * <code>'\u007a'</code>. If <code>radix</code> is * <var>N</var>, then the first <var>N</var> of these characters * are used as radix-<var>N</var> digits in the order shown. Thus, * the digits for hexadecimal (radix 16) are * <code>0123456789abcdef</code>. If uppercase letters are * desired, the {@link java.lang.String#toUpperCase()} method may * be called on the result: * <blockquote><pre> * Long.toString(n, 16).toUpperCase() * </pre></blockquote> * * @param i a <code>long</code>to be converted to a string. * @param radix the radix to use in the string representation. * @return a string representation of the argument in the specified radix. * @see java.lang.Character#MAX_RADIX * @see java.lang.Character#MIN_RADIX */ public static String toString(long i, int radix) { if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) radix = 10; if (radix == 10) return toString(i); char[] buf = new char[65]; int charPos = 64; boolean negative = (i < 0); if (!negative) { i = -i; } while (i <= -radix) { buf[charPos--] = Integer.digits[(int)(-(i % radix))]; i = i / radix; } buf[charPos] = Integer.digits[(int)(-i)]; if (negative) { buf[--charPos] = '-'; } return new String(buf, charPos, (65 - charPos)); } /** * Returns a string representation of the <code>long</code> * argument as an unsigned integer in base 16. * <p> * The unsigned <code>long</code> value is the argument plus * 2<sup>64</sup> if the argument is negative; otherwise, it is * equal to the argument. This value is converted to a string of * ASCII digits in hexadecimal (base 16) with no extra * leading <code>0</code>s. If the unsigned magnitude is zero, it * is represented by a single zero character <code>'0'</code> * (<code>'\u0030'</code>); otherwise, the first character of * the representation of the unsigned magnitude will not be the * zero character. The following characters are used as * hexadecimal digits: * <blockquote><pre> * 0123456789abcdef * </pre></blockquote> * These are the characters <code>'\u0030'</code> through * <code>'\u0039'</code> and <code>'\u0061'</code> through * <code>'\u0066'</code>. If uppercase letters are desired, * the {@link java.lang.String#toUpperCase()} method may be called * on the result: * <blockquote><pre> * Long.toHexString(n).toUpperCase() * </pre></blockquote> * * @param i a <code>long</code> to be converted to a string. * @return the string representation of the unsigned <code>long</code> * value represented by the argument in hexadecimal * (base 16). * @since JDK 1.0.2 */ public static String toHexString(long i) { return toUnsignedString(i, 4); } /** * Returns a string representation of the <code>long</code> * argument as an unsigned integer in base 8. * <p> * The unsigned <code>long</code> value is the argument plus * 2<sup>64</sup> if the argument is negative; otherwise, it is * equal to the argument. This value is converted to a string of * ASCII digits in octal (base 8) with no extra leading * <code>0</code>s. * <p> * If the unsigned magnitude is zero, it is represented by a * single zero character <code>'0'</code> * (<code>'\u0030'</code>); otherwise, the first character of * the representation of the unsigned magnitude will not be the * zero character. The following characters are used as octal * digits: * <blockquote><pre> * 01234567 * </pre></blockquote> * These are the characters <code>'\u0030'</code> through * <code>'\u0037'</code>. * * @param i a <code>long</code> to be converted to a string. * @return the string representation of the unsigned <code>long</code> * value represented by the argument in octal (base 8). * @since JDK 1.0.2 */ public static String toOctalString(long i) { return toUnsignedString(i, 3); } /** * Returns a string representation of the <code>long</code> * argument as an unsigned integer in base 2. * <p> * The unsigned <code>long</code> value is the argument plus * 2<sup>64</sup> if the argument is negative; otherwise, it is * equal to the argument. This value is converted to a string of * ASCII digits in binary (base 2) with no extra leading * <code>0</code>s. If the unsigned magnitude is zero, it is * represented by a single zero character <code>'0'</code> * (<code>'\u0030'</code>); otherwise, the first character of * the representation of the unsigned magnitude will not be the * zero character. The characters <code>'0'</code> * (<code>'\u0030'</code>) and <code>'1'</code> * (<code>'\u0031'</code>) are used as binary digits. * * @param i a <code>long</code> to be converted to a string. * @return the string representation of the unsigned <code>long</code> * value represented by the argument in binary (base 2). * @since JDK 1.0.2 */ public static String toBinaryString(long i) { return toUnsignedString(i, 1); } /** * Convert the integer to an unsigned number. */ private static String toUnsignedString(long i, int shift) { char[] buf = new char[64]; int charPos = 64; int radix = 1 << shift; long mask = radix - 1; do { buf[--charPos] = Integer.digits[(int)(i & mask)]; i >>>= shift; } while (i != 0); return new String(buf, charPos, (64 - charPos)); } /** * Returns a <code>String</code> object representing the specified * <code>long</code>. The argument is converted to signed decimal * representation and returned as a string, exactly as if the * argument and the radix 10 were given as arguments to the {@link * #toString(long, int)} method. * * @param i a <code>long</code> to be converted. * @return a string representation of the argument in base 10. */ public static String toString(long i) { if (i == Long.MIN_VALUE) return "-9223372036854775808"; char[] buf = (char[])(perThreadBuffer.get()); int charPos = getChars(i, buf); return new String(buf, charPos, (20 - charPos)); } // Per-thread buffer for string/stringbuffer conversion private static ThreadLocal perThreadBuffer = new ThreadLocal() { protected synchronized Object initialValue() { return new char[20]; } }; private static int getChars(long i, char[] buf) { long q; int r; int charPos = 20; char sign = 0; if (i < 0) { sign = '-'; i = -i; } // Get 2 digits/iteration using longs until quotient fits into an int while (i > Integer.MAX_VALUE) { q = i / 100; // really: r = i - (q * 100); r = (int)(i - ((q << 6) + (q << 5) + (q << 2))); i = q; buf[--charPos] = Integer.DigitOnes[r]; buf[--charPos] = Integer.DigitTens[r]; } // Get 2 digits/iteration using ints int q2; int i2 = (int)i; while (i2 >= 65536) { q2 = i2 / 100; // really: r = i2 - (q * 100); r = i2 - ((q2 << 6) + (q2 << 5) + (q2 << 2)); i2 = q2; buf[--charPos] = Integer.DigitOnes[r]; buf[--charPos] = Integer.DigitTens[r]; } // Fall thru to fast mode for smaller numbers if (sun.misc.BuildFlags.qAssertsEnabled) assert i2 <= 65536: i2; for (;;) { q2 = (i2 * 52429) >>> (16+3); r = i2 - ((q2 << 3) + (q2 << 1)); // r = i2-(q2*10) ... buf[--charPos] = Integer.digits[r]; i2 = q2; if (i2 == 0) break; } if (sign != 0) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -