Javaの研究ノート12:パッケージの基本データ型、Mathクラスとクラスランダム

まず、基本的なデータ型のパッケージ

基本データ型 パッケージに対応
バイト バイト
ショート ショート
int型 整数
長いです 長いです
浮く 浮く
ダブル ダブル
CHAR シャア
ブーリアン ブーリアン

1、Integerクラスは、基本的なオブジェクトのint型の値をラップします。Integer型のオブジェクトは、int型のフィールドが含まれています。

1)、获取int 类型所表示的最大值和最小值
int minValue = Integer.MIN_VALUE;
int maxValue = Integer.MAX_VALUE;
2)、创建Integer对象
//Integer( int value)
//构造一个新分配的 Integer 对象,它表示指定的 int 值。
//Integer(String s)
//构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值。
int num=100;
Integer integer = new Integer(num);
System.out.println(integer);
String str="1000";
Integer integer1 = new Integer(str); //要一个字面上全是数字的字符串
System.out.println(integer1);

図2は、NumberFormatExceptionがデジタルフォーマット異常
A。文字列型はString型を変換する場合のタイプは、スローされた例外を満たしていない、指定されたデータ型に変換されます。
他のタイプに文字列を変換するとき、B。、スペースを最初に除去することができます。
クラスの範囲外C.コンバート値
D.ヌル値は、エラーを引き起こす可能性があります。
E.異なる進。
図3において、列変換

public class MyDemo2 {
    public static void main(String[] args) {
        //(1)、数字-----字符串  100-----"100"
        int num=100;
        //方式1
        String str=num+"";
        //方式2
        String s = String.valueOf(num); //把数字转成字符串的静态方法
        //方式3
        String s1 = new Integer(num).toString();
        //(2)、字符串---数字   "100"--------100
        String str4="100";
        //方式1
        Integer integer = new Integer(str4);
        int i = integer.intValue();
       //方式2
        int parseInt = Integer.parseInt(str4);//静态方法,把字符串转成数字 记住这个方法
        System.out.println(parseInt);
        //(3)、转不同的进制
        String s2 = Integer.toBinaryString(100);//以二进制无符号整数形式返回一个整数参数的字符串表示形式。
        String s3 = Integer.toHexString(100);//以十六进制无符号整数形式返回一个整数参数的字符串表示形式。
        String s4 = Integer.toOctalString(100);//以八进制无符号整数形式返回一个整数参数的字符串表示形式。    
    }
}

4文字の一般的な方法

public class CharacterDemo {
  public static void main(String[] args) {
     Character c = Character.valueOf('c');
     String str = "abfdfAA102032";  
     //static boolean isUpperCase ( char ch)
     //确定指定字符是否为大写字母。
     //static boolean isSpaceChar ( char ch)
     //确定指定字符是否为 Unicode 空白字符。
     //static boolean isLowerCase ( char ch)
     //确定指定字符是否为小写字母。
     //static boolean isDigit ( char ch)
     //确定指定字符是否为数字。
     boolean a = Character.isUpperCase('a');
     System.out.println(a);
  }
}

5、開梱や梱包
(1)、手動入力ボックス

public class IntegerDemo2 {
    public static void main(String[] args) {
        //手动装箱
        int num=100;
        //方式1
        //Integer integer = new Integer(num);
        //方式2
        Integer integer = Integer.valueOf(num);
        Integer inter2 = Integer.valueOf("1000");
        //手动拆箱
        Integer integer4 = new Integer(100);
        int i = integer4.intValue();
        System.out.println(i+10);
    }
 }

(2)自動入力ボックス

public class IntegerDemo {
    public static void main(String[] args) {
        //JDK1.5 之后 引入了自动拆装箱的机制
        //自动装箱:将基本类型自动包装成对应的引用类型
        //自动拆箱:将包装类型自动转成对应的基本类型
        //int num=100;
        Integer a=100; //自动装箱
        Integer b=200;// 自动装箱
        int sum=a+b;//自动拆箱
        System.out.println("---------------------");
        Integer c=10; //自动装箱
        c+=20;//自动拆箱 自动装箱
    }
}

二、数学クラス
A、概要:Mathクラスは、これまで指数関数、対数関数、三角関数や平方根のように、基本的な数学演算を実行するためのメソッドが含まれています。
B、メンバ変数

  • public static finalダブルE:天然塩基数
  • public static finalダブルPI:PI

C、メンバー・メソッド

  • パブリックstatic int型ABS(INT A)の絶対値
  • パブリック静的ダブルはceil(ダブルA)向上取整
  • パブリック静的ダブルフロア(ダブルA)は切り捨て
  • パブリックstatic int型の最大値(int型B、INT)の最大値を求めます
  • パブリックstatic int型の分(int型B、INT)の最小値を求めます
  • パブリック静的ダブルPOW(ダブルA、ダブルB)Bのパワーを求めます
  • )(ランダム二重公共静的以上0.0と1.0未満に等しい正の符号を用いて乱数を取得double値を返します。
  • 公共の静的なint型のラウンド(フロートa)の丸め
  • 正の平方根を取得するのpublic staticダブルのsqrt(ダブルA)

Mathクラスを使用して、実施例D、

public class MathDemo {
    public static void main(String[] args) {
        System.out.println(Math.PI);
        System.out.println(Math.E);
        //常用的静态方法
        double num = Math.random();    
        System.out.println(Math.abs(-1));
        System.out.println(Math.ceil(3.14));
        System.out.println(Math.floor(3.14));
        System.out.println(Math.max(19, Math.max(1, 10)));
        System.out.println(Math.min(1,90));
        System.out.println(Math.pow(2,3));
        System.out.println(Math.round(6.59));//拿小数点后面第一个数来舍入
        System.out.println(Math.sqrt(4));
        System.out.println(Math.pow(8.0, 1.0/3.0)); //求一个数的立方根
        double sin = Math.sin(Math.PI / 2.0);
        System.out.println(sin);
    }
}

三、ランダムクラス
Aの概要:クラスは数字の同じシーケンスを生成し、返されランダムシードの例と同じ2つのメソッド呼び出しの同じシーケンスの各インスタンスを作成するために使用された場合に乱数を発生させます。
B、コンストラクタ

  • 公共ランダム()は(ミリ秒現在のシステム)のデフォルトを使用して、シードを与えられていません
  • 公共ランダム(長いシード)毎に生成所与乱数後のシードの長い所与のタイプは、同じです。

C:メンバメソッド

  • 公共のint nextInt()//ない乱数範囲パラメータは、int型の範囲を表していません
  • 公共のint nextInt(int型n)は//乱数の範囲を指定することができます

D:ランダムクラスを使用した例

public class RandomDemo {
    public static void main(String[] args) {
        //创建随机类
        Random random = new Random();
        for (int i = 0; i < 1000; i++) {
           // int num= random.nextInt(); 不指定随机整数的范围,生成的数在int类型的范围内
            int num = random.nextInt(10);  //随机数的范围 大于等于 0 小于 10
            boolean b = random.nextBoolean();
            double v = random.nextDouble();
            System.out.println(num);
            System.out.println(b);
            System.out.println(v);
        }
    }
}
公開された24元の記事 ウォン称賛11 ビュー2055

おすすめ

転載: blog.csdn.net/weixin_43791069/article/details/86849696