zoukankan      html  css  js  c++  java
  • 类型转换和常量

    package com.lei.duixiang;
    
    
    
    public class Summation {
        /**
         * 类型转换
         * 常量    Integer  Boolean Byte  Character Double Number
         * 只是以Integer作为实例,基本一样
         * MAX_VALUE(表示 int类型可取的最大值) 
         * MIN_VALUE(表示 int类型可取的最小值)    
         * SIZE(用来以二进制补码形式表示int值的位数)     
         * TYPE(基本类型int的Class实例)
         * @param args
         */
        public static void main(String[] args) {
    
            String str[] = {"10","20","30","40","50"};    //定义 String数组
            int sum = 0;
            for(int i=0;i < str.length;i++){
                int myint = Integer.parseInt(str[i]);    //将每个数组都转换为 int
                sum = sum + myint;
            }
            System.out.println("数组中的各元素之和是:"+sum);
    
            String s1 = Integer.toString(456);    //十进制表示
            String s2 = Integer.toBinaryString(456); //二进制表示
            String s3 = Integer.toHexString(456);    //十六进制表示
            String s4 = Integer.toOctalString(456);    //八进制表示
            System.out.println("456的十进制表示为:"+s1);
            System.out.println("456的二进制表示为:"+s2);
            System.out.println("456的十六进制表示为:"+s3);
            System.out.println("456的八进制表示为:"+s4);
    
            //Integer 常量
            Summation s = new Summation();
            s.GetCon();
    
            //Boolean 常量
            s.GetBoolean();
    
            // Byte 常量方法
            s.GetByte();
    
            // Character 常量方法
            s.GetCharacter();
        }
        // Integer 常量方法
        public void GetCon(){
            System.out.println("-----------------");
            /**
             * Integer 类 提供 4个常量
             * MAX_VALUE(表示 int类型可取的最大值) 
             * MIN_VALUE(表示 int类型可取的最小值)    
             * SIZE(用来以二进制补码形式表示int值的位数)     
             * TYPE(基本类型int的Class实例)
             */
            int maxint = Integer.MAX_VALUE;    //获取 Integer的常量值
            int minint = Integer.MIN_VALUE; 
            int intsize = Integer.SIZE;
            System.out.println("int 类型可取的最大值是:"+maxint);
            System.out.println("int 类型可取的最小值是:"+minint);
            System.out.println("int 类型可取的二进制位数是:"+intsize);
        }
    
        // Boolean 常量方法
        public void GetBoolean(){
            System.out.println("-----------------");
            /**
             * Boolean 类 提供 3个常量
             * 1、TYPE(基本类型boolean的Class实例)     
             * 2、FALSE(对应基值false的Boolean对象)     
             * 3、TRUE(对应基值true的Boolean对象)
             */
            Boolean b1 = new Boolean(true);    //创建 Boolean对象
            Boolean b2 = new Boolean("ok");
            System.out.println("b1 :"+b1.booleanValue());
            System.out.println("b2 :"+b2.booleanValue());
        }
    
        // Byte 常量方法
        public void GetByte(){
            System.out.println("-----------------");
    
            byte mybyte = 45;
            Byte b = new Byte(mybyte);
            Byte a = new Byte("12");
            Byte maxByte = Byte.MAX_VALUE;    //获取 Integer的常量值
            Byte minByte = Byte.MIN_VALUE; 
            Byte Bytesize = Byte.SIZE;
            System.out.println("Byte 类型可取的最大值是:"+maxByte);
            System.out.println("Byte 类型可取的最小值是:"+minByte);
            System.out.println("Byte 类型可取的二进制位数是:"+Bytesize);
            System.out.println(b.byteValue());
            System.out.println(a.byteValue());
        }
    
        // Character 常量方法
        public void GetCharacter(){
            System.out.println("-----------------");
    
            Character mychar1 = new Character('A');
            Character mychar2 = new Character('a');
    
            System.out.println(mychar1+"是大写字母吗?"+Character.isUpperCase(mychar1));
            System.out.println(mychar1+"是小写字母吗?"+Character.isLowerCase(mychar2));
        }
    }
  • 相关阅读:
    第九周作业
    第八周作业
    第七周作业
    作业2
    作业1
    2019春总结作业
    第十四周总结
    十二周编程总结
    十一周编程总结
    第十周作业
  • 原文地址:https://www.cnblogs.com/spadd/p/4165966.html
Copyright © 2011-2022 走看看