码迷,mamicode.com
首页 > 其他好文 > 详细

12、类型转换和常量

时间:2014-12-15 23:25:23      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   for   on   

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));
    }
}

 

12、类型转换和常量

标签:style   blog   io   ar   color   os   sp   for   on   

原文地址:http://www.cnblogs.com/spadd/p/4165966.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!