Java8 基础数据类型包装类-Long

博客围绕Java展开,介绍基础部分,包括常量、继承、抽象类Number,以及包装类与基本类型转换值获取等。方法部分涉及转特定进制字符串、字符串与数值转换、获取系统属性值、比较求和、无符号数及位运算等内容。

 

基础

//final修饰不可更改,每次赋值都是新建类(其中-128~127是通过LongCache数组获取的不是新建的,所以可以使用==比较,但其他数据是通过new创建的,不能使用==直接比较大小,因为是不同的类,地址不同,需用equals)
public final class Long extends Number implements Comparable<Long> {}
  • 1
  • 2

常量

//-2^63
@Native public static final long MIN_VALUE = 0x8000000000000000L; //2^63-1 @Native public static final long MAX_VALUE = 0x7fffffffffffffffL; //位数 @Native public static final int SIZE = 64; //字节数 public static final int BYTES = SIZE / Byte.SIZE;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

继承 
抽象类Number 
获取包装类与基本类型之间的转换值,short、int、long、byte、float、double。 
实现 
Comparable 接口 
实现compareTo(T o)方法 
私有静态内部类

//初始化是-128~127的Long对象
private static class LongCache {
    private LongCache(){} static final Long cache[] = new Long[-(-128) + 127 + 1]; static { for(int i = 0; i < cache.length; i++) cache[i] = new Long(i - 128); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

方法

转成特定进制的字符串

//i:数值
//radix:需要转换的进制(2~36 不在此范围的按10进制处理)
public static String toString(long i, int radix) {} //默认转成10进制,当i==MIN_VALUE 直接返回数值"-9223372036854775808" public static String toString(long i) {} //调用toString(long i) public String toString() {}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

按无符号数值转特定进制的字符串,BigInteger

//无符号数转字符串
//i:数值
//radix:需要转换的进制(2~36 不在此范围的按10进制处理)
public static String toUnsignedString(long i, int radix) {} private static BigInteger toUnsignedBigInteger(long i) {} //转化成16进制无符号字符串 public static String toHexString(long i) {} //转化成10进制无符号字符串 public static String toUnsignedString(long i) {} //转化成8进制无符号字符串 public static String toOctalString(long i) {} //转化成2进制无符号字符串 public static String toBinaryString(long i) {}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

字符串转long

//s:数值字符串
//radix:s字符串是几进制(radix不在2~36则抛异常)
public static long parseLong(String s, int radix) throws NumberFormatException{} //字符串为10进制 public static long parseLong(String s) throws NumberFormatException {} //无符号字符串转long(字符串第一位为“-”抛异常) //radix:表示s字符串是几进制(radix不在2~36则抛异常) public static long parseUnsignedLong(String s, int radix) throws NumberFormatException {} //字符串为10进制 public static long parseUnsignedLong(String s) throws NumberFormatException {}

字符串,long转Long

//s:数字型字符串
//radix:表示s字符串是几进制(radix不在2~36则抛异常)
public static Long valueOf(String s, int radix) throws NumberFormatException {} //默认为10进制 public static Long valueOf(String s) throws NumberFormatException{} //其中-128~127的Long是程序启动时就已经保存到LongCache中,所以在这个范围的数据是直接取的LongCache中的值 public static Long valueOf(long l) {} //nm:可以是010(8进制)、0x10(16进制)(#开头的在这个方法中也表示16进制) public static Long decode(String nm) throws NumberFormatException {}

Long转基本数据类型

public byte byteValue() {}
public short shortValue() {} public int intValue() {} public long longValue() {} public float floatValue() {} public double doubleValue() {}

获取系统属性的值

//nm:系统属性的名称
//val:如果为空时的默认值
public static Long getLong(String nm, long val) {} public static Long getLong(String nm, Long val) {} //返回默认值为null public static Long getLong(String nm) {}

比较及求和

//返回-1,1,0(-1表示y大,1表示x大)
public int compareTo(Long anotherLong) {}
//返回-1,1,0(-1表示y大,1表示x大) public static int compare(long x, long y) {} //比较xy无符号数的大小,返回-1,1,0(-1表示y大,1表示x大) public static int compareUnsigned(long x, long y) {} public static long sum(long a, long b) {} public static long max(long a, long b) {} public static long min(long a, long b) {}

无符号数运算

//将dividend和divisor转成无符号整数相除取整
public static long divideUnsigned(long dividend, long divisor) {} //将dividend和divisor转成无符号整数相除取余 public static long remainderUnsigned(long dividend, long divisor) {}

位运算 
同Integer

转载于:https://www.cnblogs.com/kelelipeng/p/10895622.html

### Java 基本数据类型对应的包装 Java 中有八种基本数据类型,每一种都有相应的包装。这些包装允许开发者将基本数据类型作为对象处理,并提供了许多有用的方法来操作这些值。 #### 包装对应表 | 基本数据类型 | 对应的包装 | |--------------|---------------| | `boolean` | `Boolean` | | `byte` | `Byte` | | `short` | `Short` | | `int` | `Integer` | | `long` | `Long` | | `float` | `Float` | | `double` | `Double` | | `char` | `Character` | #### 练习题及答案 1. **题目**: 将整数 `100` 转换成字符串并打印出来。 ```java public class Main { public static void main(String[] args) { int num = 100; String strNum = Integer.toString(num); System.out.println(strNum); // 输出 "100" } } ``` 2. **题目**: 使用两种不同的方法将浮点数 `3.14` 转换为字符串。 ```java public class Main { public static void main(String[] args) { double pi = 3.14; // 方法一:使用静态方法 String strPi1 = Double.toString(pi); System.out.println(strPi1); // 输出 "3.14" // 方法二:使用拼接运算符 String strPi2 = "" + pi; System.out.println(strPi2); // 输出 "3.14" } } ``` 3. **题目**: 解析字符串 `"98"` 并将其转换成整数值再加 `1` 后输出。 ```java public class Main { public static void main(String[] args) { String strInt = "98"; int intValue = Integer.parseInt(strInt) + 1; System.out.println(intValue); // 输出 99 } } ``` 4. **题目**: 判断字符 `'A'` 是否可以被解析为有效的整数。 ```java public class Main { public static void main(String[] args) { char ch = 'A'; try { int result = Integer.parseInt(Character.toString(ch)); System.out.println(result); } catch (NumberFormatException e) { System.out.println("无法解析"); } } } ``` 5. **题目**: 创建一个布尔型变量并将它转换成字符串形式输出。 ```java public class Main { public static void main(String[] args) { boolean flag = true; String boolStr = Boolean.toString(flag); System.out.println(boolStr); // 输出 "true" } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值