【JAVASE_学習ノート】データ型

11510 ワード

【JAVASE_学習ノート】データ型
定数
定数:プログラム実行中にその値が変わらない量定数タイプ:1.字面値定数:a.整数定数例えば12 b.小数定数例えば3.14 c.ブール定数ブール定数は2つの値しかなく、true,false d.文字定数単一文字は、「a」e.文字列定数データのような単一引用符によるコンテンツは、「helloworld」f.空定数null 2のような二重引用符によるコンテンツである.カスタム定数(キーワード:final関連:オブジェクト向けセクションの説明)
public class ConstantDemo {
    public static void main(String[] args) {
        //     
        System.out.println("helloworld");
        //    +(    )      =    
        System.out.println("helloworld"+'a'+1);//helloworlda1
        System.out.println('a'+1+"helloworld");//98helloworld
        System.out.println("5+5="+5+5);//5+5=55
        System.out.println(5+5+"=5+5");//10=5+5

        System.out.println("------------------------");

        //    
        //     
        System.out.println('A');
        System.out.println('a');
        System.out.println('0');
//      System.out.println('ab');//  
        System.out.println("------------------------");

        //          ,    ASCII          
        //'A'---->65
        //'a'---->97
        //'0'---->48
        System.out.println('A'+1);//66
        System.out.println('a'+1);//98
        System.out.println('0'+1);//49
        System.out.println("----------------------");

        //    
        System.out.println(100);
        //    
        System.out.println(3.1415926);
        System.out.println("-------------------");
        //    
        System.out.println(true);
        System.out.println(false);
        //   
        String s = null ;//      
        System.out.println(s);
    }
}

変数#ヘンスウ#
変数:プログラム実行中にその値が変化可能な量A.8種類の基本データ型1.整数タイプ:byte(バイト)1バイト(8 bit)範囲:-128~127 short(短整数)2バイト(16 bit)int(整数)4バイト(32 bit)long(長整数)8バイト(64 bit)2.小数タイプ:float(単精度浮動小数点型)4バイト(32 bit)double(二重精度浮動小数点型)8バイト(64 bit)3.ブールタイプ:boolean(ブールタイプ)1バイトまたは4バイト基本タイプ変数を宣言するときに4バイトを占める配列タイプを宣言するときに、配列内の要素ごとに4バイトを占める4.文字タイプ:char(文字タイプ)2バイトB.引用タイプString注意:整数タイプ:デフォルトタイプ:intタイプlongタイプを使用して変数を定義し、現在の変数値の後ろにLまたはl(Lを推奨)を付ける.システムに私が定義したのはlongタイプの浮動小数点タイプであることを教えます:デフォルトの二重精度:doubleはfloatタイプを定義して、この変数値の末尾でFあるいはfに追いつきます
public class DataTypeDemo {
    public static void main(String[] args) {
        //byte  
        byte b = 100 ;
        System.out.println(b);
        System.out.println("--------------");

        //   
        short s = 20 ;
        s = 50 ;
        System.out.println("s:"+s);
        System.out.println("--------------");
//      int i = 1000000000000;
//      System.out.println("i:"+i);
        //   
        long l = 1000000000000L;
        System.out.println("l:"+l);

        //    :  double  
        double d = 12.56 ;
        System.out.println("d:"+d);

        //   
        float f = 12.34F ;
        System.out.println("f:"+f);

        //                   .  :        :BigDecimal
//      System.out.println(1.0-0.32);

        //char  
        char ch = 'A' ;
        System.out.println(ch);

        //boolean  
        boolean flag = true;
        System.out.println(flag);



    }
}

変数の定義(宣言)
フォーマット:方法1:データ型変数名;方式2:データ型変数1、変数2...;注意:1.Javaでは、変数を定義しても繰り返し定義できません.同じ行に複数の変数を定義しないことを推奨します.変数の定義は数値で始まることはできません
public class DataTypeDemo2 {
    public static void main(String[] args) {
        //    
        int a = 10 ;
//      int a = 20 ;
        a = 20 ;


        //        ,       
//      int m = 10 ,n = 20 ,x = 30 ;
        int m = 10 ;
        int n = 20 ;
        int x = 30 ;

        //       
//      int 2z = 40 ;
    }
}

データ型変換
注意:1.byte,short,char型データは演算時に自動的にint型に変換する再演算2.2つの異なるタイプのデータ演算、結果は大きなデータ型データに依存します.
  int    +long          long  

小データ型——>大データ型自動型変換大データ型——>小データ型強制型変換(精度を損なう)強制型変換フォーマット:ターゲットデータ型変数名=(ターゲットデータ型)(大データ型データ)練習:
/*
 *    :
            byte b1=3,b2=4,b;
                b=b1+b2;
                b=3+4;
                     ?    

           :        ,          ,    ,        
        :b = 3 + 4 ;      ,   ---->                ,      !
 * */
public class DataTypeDemo3 {
    public static void main(String[] args) {

        byte b1 = 3,b2 = 4 ,b ;

//      b =  b1 + b2 ;//   :byte         int    
        b = (byte)(b1 + b2) ;

        b = 3 + 4 ;
        System.out.println("b:"+b);
    }
}

負数の記憶
負数の最高位は1で、正数最高位0負数計算機に記憶されているのは、符号補完演算ステップ(負数):1.負数を先に得るバイナリ符号(原符号);2.最高位は変わらず、原符号は逆(逆符号);3.逆符号プラス1(符号補完)正数の原符号、逆符号、符号補完は同じ練習:
/*
 *    
        byte b = 130;     ?         ,     ?      ?


               :    :byte     :-128~127
                   ,         ,  -126

 * */
public class DataTypeDemo4 {
    public static void main(String[] args) {
        //    
        byte b =(byte)130 ;

        System.out.println("b:"+b);
    }
}
/*
 * 130      :  int  
 *             
 *          00000000 00000000 0000000   10000010
 * 
 *                0,    ,        ,  ,     
 *      
 *                        :byte b = (byte)130;
 *          10000010(  )----->  
 * 
 *                             
 *      1                   0000010           
 *                               -1
 *      1                   0000001             --->       ,       
 *      1                   1111110
 * 
 *   :      
 *      -                   64+32+16+8+4+2
 *      -                   126
 * 
 * */