博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA类型转换大全
阅读量:7072 次
发布时间:2019-06-28

本文共 1582 字,大约阅读时间需要 5 分钟。

Java自带的库很多用起来都不是那么方便,类型转换很多都要显示去做真的有点难记住。找了篇资料说的很详细,记录下来吧。

integer   to   String   :         int   i   =   42;   

        String   str   =   Integer.toString(i);   
        or   
        String   str   =   ""   +   i   
      
  double   to   String   :       String   str   =   Double.toString(i);   
      
  long   to   String   :       String   str   =   Long.toString(l);   
      
  float   to   String   :       String   str   =   Float.toString(f);     
    
  String   to   integer   :       str   =   "25";   
        int   i   =   Integer.valueOf(str).intValue();   
        or   
        int   i   =   Integer.parseInt(str);   
         
  String   to   double   :       double   d   =   Double.valueOf(str).doubleValue();   
        
  String   to   long   :       long   l   =   Long.valueOf(str).longValue();   
        or   
        long   l   =   Long.parseLong(str);   
        
  String   to   float   :       float   f   =   Float.valueOf(str).floatValue();   
         
  decimal   to   binary   :       int   i   =   42;   
        String   binstr   =   Integer.toBinaryString(i);   
       
  decimal   to   hexadecimal   :       int   i   =   42;   
        String   hexstr   =   Integer.toString(i,   16);   
        or   
        String   hexstr   =   Integer.toHexString(i);   
       
  hexadecimal   (String)   to   integer   :       int   i   =   Integer.valueOf("B8DA3",   16).intValue();   
        or   
        int   i   =   Integer.parseInt("B8DA3",   16);             
      
  ASCII   code   to   String       int   i   =   64;   
        String   aChar   =   new   Character((char)i).toString();   
      
  integer   to   ASCII   code   (byte)       char   c   =   'A';   
        int   i   =   (int)   c;   //   i   will   have   the   value   65   decimal   
      
  To   extract   Ascii   codes   from   a   String           String   test   =   "ABCD";   
            for   (   int   i   =   0;   i   <   test.length();   ++i   )   {   
                char   c   =   test.charAt(   i   );   
                int   i   =   (int)   c;   
                System.out.println(i);   
                }   
      
  integer   to   boolean       b   =   (i   !=   0);   
      
  boolean   to   integer       i   =   (b)?1:0;   
      
  note   :To   catch   illegal   number   conversion,   try   using   the   try/catch   mechanism.  

  try{   
      i   =   Integer.parseInt(aString);   
  }catch(NumberFormatException   e){   
  }   

 本文转自passover 51CTO博客,原文链接:http://blog.51cto.com/passover/425915,如需转载请自行联系原作者

你可能感兴趣的文章
reactor-netty中HttpClient对TcpClient的封装
查看>>
数据库安全性操作——操作原则及SQL注入
查看>>
Java网络爬虫实操(9)
查看>>
前面有一个Redux,我们去撩(聊)一下它。
查看>>
iOS开发证书"此证书的签发者无效"解决方法
查看>>
Python实现的通用树结构,支持节点索引,常数时间查找
查看>>
网络传输协议
查看>>
iOS Principle:Category
查看>>
Java多线程之synchronized增强版——ReentrantLock
查看>>
MVP设计模式应该这样掌握
查看>>
Git标签的管理和配置命令别名
查看>>
对UIView,UIWindow和CALayer的理解
查看>>
使用javap分析Java的字符串操作
查看>>
Node.js-Koa2-MongoDB构建RESTful Api
查看>>
饿了么UETool原理初探
查看>>
Android基础 和服务器交互你必须知道的json处理
查看>>
2018阿里云云数据库RDS核心能力演进
查看>>
JAVA面试 基础加强与巩固:反射、注解、泛型等
查看>>
JavaScript 工作原理之六-WebAssembly 对比 JavaScript 及其使用场景
查看>>
Win7环境下VS2015+Python 2.7 编译人脸识别开源库 face_recognition
查看>>