• 学习笔记之基本数据类型-包装类-String之间的转换


    基本类型              包装类                String类

    int->Integer   :             Integer构造器

    Integer-> int  :   Integer.intValue()方法

    int -> String  :  Integer.toString方法,String.valueof()方法

    String -> int  :  Integer.parseInt()方法

    Integer -> String : Integer.toString()方法,String.valueof()方法

    String -> Integer : Integer的构造器,Integer.valueof()方法

    5.0以后  自动拆装箱

    例如:

    Integer iobj1 = 10; // 装

    int j = iobj1;  //拆

    public class TestDemo {
        public static void intDemo() {
            // int->String
            // String -> int
            int a = 3;
            System.out.println(a);
            String a1 = Integer.toString(a);
            System.out.println(a1);
            int a2 = Integer.parseInt(a1);
            System.out.println(a2 - 4);
    
            // int -> Integer
            // Integer -> int
            Integer a3 = a;
            int a4 = a3.intValue();
            System.out.println(a3);
            System.out.println(a4);
    
            // Integer -> String
            // String -> Integer
            Integer i = new Integer(10);
            String a5 = i.toString();
            System.out.println(a5);
            Integer i2 = Integer.valueOf(a5);
            Integer i3 = Integer.parseInt(a5);
            System.out.println(i2 + 5);
            System.out.println(i3 + 7);
    
        }
        
    
        public static void floatDemo(){
            //float->Float
            //Float->float
            float f1 = 1.20f;
            Float f2 = f1;
            float f3 = f2;
            System.out.println(f2 +"	" + f3);
            
            //float -> String
            //String -> float
            float f4 = 2.234f;
            String f5 = Float.toString(f4);
            System.out.println(f5 + 7);
            f4 = Float.parseFloat(f5);
            System.out.println(f4);
            
            //Float -> String
            //String -> Float
            Float f6 = new Float(12.4);
            String f7 = Float.toString(f6);
            System.out.println(f7 + 9);
            f6 = Float.parseFloat(f7);
            System.out.println(f6 + 1.1f);
        }
        
        public static void charDemo(){
            //char -> Character
            //Character -> char
            char ch = 'p';
            Character ch2 = new Character('q');
            ch2 = ch;
            System.out.println(ch2);
            ch = new Character('u');
            System.out.println(ch);
            
            //char -> String
            //String -> char
            char ch3 = 'q';
            String ch4 = Character.toString(ch3);
            System.out.println(ch4);
            char ch5 = ch4.charAt(0);
            System.out.println(ch5);
            
            //Character -> String
            //String -> Character
            Character ch6 = new Character('a');
            String ch7 = ch6 + "";
            System.out.println(ch7);
            Character ch8 = Character.valueOf(ch6);
            System.out.println(ch8);
        }
        
        public static void main(String[] args) {
            TestDemo.intDemo();
        }
    
    }
  • 相关阅读:
    01-面向对象
    12-期末作业
    11-Linux-vim /bash
    组播地址
    rip
    华为hcnp r&s考试一共有三门,R&S-IERS,R&S-IENP,R&S-IEEP

    spring注解开发
    yml和properties的加载顺序和区别
    @ImportResource
  • 原文地址:https://www.cnblogs.com/hnzyyTl/p/4915020.html
Copyright © 2020-2023  润新知