/** Scanner:用于接收鍵盤錄入數(shù)據(jù)。** 前面的時候:* A:導(dǎo)包* B:創(chuàng)建對象* C:調(diào)用方法** System類下有一個靜態(tài)的字段:* public static final InputStream in; 標(biāo)準(zhǔn)的輸入流,對應(yīng)著鍵盤錄入。** InputStream is = System.in;** class Demo {* public static final int x = 10;* public static final Student s = new Student();* }* int y = Demo.x;* Student s = Demo.s;*** 構(gòu)造方法:* Scanner(InputStream source)*/public class ScannerDemo {public static void main(String[] args) {// 創(chuàng)建對象Scanner sc = new Scanner(System.in);int x = sc.nextInt();System.out.println("x:" + x);}}
/** 基本格式:* public boolean hasNextXxx():判斷是否是某種類型的元素* public Xxx nextXxx():獲取該元素** 舉例:用int類型的方法舉例* public boolean hasNextInt()* public int nextInt()** 注意:* InputMismatchException:輸入的和你想要的不匹配*/public class ScannerDemo {public static void main(String[] args) {// 創(chuàng)建對象Scanner sc = new Scanner(System.in);// 獲取數(shù)據(jù)if (sc.hasNextInt()) {int x = sc.nextInt();System.out.println("x:" + x);} else {System.out.println("你輸入的數(shù)據(jù)有誤");}}}
?
/** 常用的兩個方法:* public int nextInt():獲取一個int類型的值* public String nextLine():獲取一個String類型的值** 出現(xiàn)問題了:* 先獲取一個數(shù)值,在獲取一個字符串,會出現(xiàn)問題。* 主要原因:就是那個換行符號的問題。* 如何解決呢?* A:先獲取一個數(shù)值后,在創(chuàng)建一個新的鍵盤錄入對象獲取字符串。* B:把所有的數(shù)據(jù)都先按照字符串獲取,然后要什么,你就對應(yīng)的轉(zhuǎn)換為什么。*/public class ScannerDemo {public static void main(String[] args) {// 創(chuàng)建對象Scanner sc = new Scanner(System.in);// 獲取兩個int類型的值// int a = sc.nextInt();// int b = sc.nextInt();// System.out.println("a:" + a + ",b:" + b);// System.out.println("-------------------");// 獲取兩個String類型的值// String s1 = sc.nextLine();// String s2 = sc.nextLine();// System.out.println("s1:" + s1 + ",s2:" + s2);// System.out.println("-------------------");// 先獲取一個字符串,在獲取一個int值// String s1 = sc.nextLine();// int b = sc.nextInt();// System.out.println("s1:" + s1 + ",b:" + b);// System.out.println("-------------------");// 先獲取一個int值,在獲取一個字符串// int a = sc.nextInt();// String s2 = sc.nextLine();// System.out.println("a:" + a + ",s2:" + s2);// System.out.println("-------------------");int a = sc.nextInt();Scanner sc2 = new Scanner(System.in);String s = sc2.nextLine();System.out.println("a:" + a + ",s:" + s);}}
/** 需求1:我要求大家把100這個數(shù)據(jù)的二進(jìn)制,八進(jìn)制,十六進(jìn)制計算出來* 需求2:我要求大家判斷一個數(shù)據(jù)是否是int范圍內(nèi)的。* 首先你的知道int的范圍是多大?** 為了對基本數(shù)據(jù)類型進(jìn)行更多的操作,更方便的操作,Java就針對每一種基本數(shù)據(jù)類型提供了對應(yīng)的類類型。包裝類類型。* byte Byte* short Short* int Integer* long Long* float Float* double Double* char Character* boolean Boolean** 用于基本數(shù)據(jù)類型與字符串之間的轉(zhuǎn)換。*/public class IntegerDemo {public static void main(String[] args) {// 不麻煩的就來了// public static String toBinaryString(int i)System.out.println(Integer.toBinaryString(100));// public static String toOctalString(int i)System.out.println(Integer.toOctalString(100));// public static String toHexString(int i)System.out.println(Integer.toHexString(100));// public static final int MAX_VALUESystem.out.println(Integer.MAX_VALUE);// public static final int MIN_VALUESystem.out.println(Integer.MIN_VALUE);}}
/** Integer的構(gòu)造方法:* public Integer(int value)* public Integer(String s)* 注意:這個字符串必須是由數(shù)字字符組成*/public class IntegerDemo {public static void main(String[] args) {// 方式1int i = 100;Integer ii = new Integer(i);System.out.println("ii:" + ii);// 方式2String s = "100";// NumberFormatException// String s = "abc";Integer iii = new Integer(s);System.out.println("iii:" + iii);}}
?
/** int類型和String類型的相互轉(zhuǎn)換** int -- String* String.valueOf(number)** String -- int* Integer.parseInt(s)*/public class IntegerDemo {public static void main(String[] args) {// int -- Stringint number = 100;// 方式1String s1 = "" + number;System.out.println("s1:" + s1);// 方式2String s2 = String.valueOf(number);System.out.println("s2:" + s2);// 方式3// int -- Integer -- StringInteger i = new Integer(number);String s3 = i.toString();System.out.println("s3:" + s3);// 方式4// public static String toString(int i)String s4 = Integer.toString(number);System.out.println("s4:" + s4);System.out.println("-----------------");// String -- intString s = "100";// 方式1// String -- Integer -- intInteger ii = new Integer(s);// public int intValue()int x = ii.intValue();System.out.println("x:" + x);//方式2//public static int parseInt(String s)int y = Integer.parseInt(s);System.out.println("y:"+y);}}
?
?
/** 常用的基本進(jìn)制轉(zhuǎn)換* public static String toBinaryString(int i)* public static String toOctalString(int i)* public static String toHexString(int i)** 十進(jìn)制到其他進(jìn)制* public static String toString(int i,int radix)* 由這個我們也看到了進(jìn)制的范圍:2-36* 為什么呢?0,...9,a...z** 其他進(jìn)制到十進(jìn)制* public static int parseInt(String s,int radix)*/public class IntegerDemo {public static void main(String[] args) {// 十進(jìn)制到二進(jìn)制,八進(jìn)制,十六進(jìn)制System.out.println(Integer.toBinaryString(100));System.out.println(Integer.toOctalString(100));System.out.println(Integer.toHexString(100));System.out.println("-------------------------");// 十進(jìn)制到其他進(jìn)制System.out.println(Integer.toString(100, 10));System.out.println(Integer.toString(100, 2));System.out.println(Integer.toString(100, 8));System.out.println(Integer.toString(100, 16));System.out.println(Integer.toString(100, 5));System.out.println(Integer.toString(100, 7));System.out.println(Integer.toString(100, -7));System.out.println(Integer.toString(100, 70));System.out.println(Integer.toString(100, 1));System.out.println(Integer.toString(100, 17));System.out.println(Integer.toString(100, 32));System.out.println(Integer.toString(100, 37));System.out.println(Integer.toString(100, 36));System.out.println("-------------------------");//其他進(jìn)制到十進(jìn)制System.out.println(Integer.parseInt("100", 10));System.out.println(Integer.parseInt("100", 2));System.out.println(Integer.parseInt("100", 8));System.out.println(Integer.parseInt("100", 16));System.out.println(Integer.parseInt("100", 23));//NumberFormatException//System.out.println(Integer.parseInt("123", 2));}}
?
/** JDK5的新特性* 自動裝箱:把基本類型轉(zhuǎn)換為包裝類類型* 自動拆箱:把包裝類類型轉(zhuǎn)換為基本類型** 注意一個小問題:* 在使用時,Integer x = null;代碼就會出現(xiàn)NullPointerException。* 建議先判斷是否為null,然后再使用。*/public class IntegerDemo {public static void main(String[] args) {// 定義了一個int類型的包裝類類型變量i// Integer i = new Integer(100);Integer ii = 100;ii += 200;System.out.println("ii:" + ii);// 通過反編譯后的代碼// Integer ii = Integer.valueOf(100); //自動裝箱// ii = Integer.valueOf(ii.intValue() + 200); //自動拆箱,再自動裝箱// System.out.println((new StringBuilder("ii:")).append(ii).toString());Integer iii = null;// NullPointerExceptionif (iii != null) {iii += 1000;System.out.println(iii);}}}
?
/** 看程序?qū)懡Y(jié)果** 注意:Integer的數(shù)據(jù)直接賦值,如果在-128到127之間,會直接從緩沖池里獲取數(shù)據(jù)*/public class IntegerDemo {public static void main(String[] args) {Integer i1 = new Integer(127);Integer i2 = new Integer(127);System.out.println(i1 == i2);System.out.println(i1.equals(i2));System.out.println("-----------");Integer i3 = new Integer(128);Integer i4 = new Integer(128);System.out.println(i3 == i4);System.out.println(i3.equals(i4));System.out.println("-----------");Integer i5 = 128;Integer i6 = 128;System.out.println(i5 == i6);System.out.println(i5.equals(i6));System.out.println("-----------");Integer i7 = 127;Integer i8 = 127;System.out.println(i7 == i8);System.out.println(i7.equals(i8));// 通過查看源碼,我們就知道了,針對-128到127之間的數(shù)據(jù),做了一個數(shù)據(jù)緩沖池,如果數(shù)據(jù)是該范圍內(nèi)的,每次并不創(chuàng)建新的空間// Integer ii = Integer.valueOf(127);}}------------------------------------falsetrue-----------falsetrue-----------falsetrue-----------truetrue
/** 看程序?qū)懡Y(jié)果:結(jié)果和我們想的有一點點不一樣,這是因為float類型的數(shù)據(jù)存儲和整數(shù)不一樣導(dǎo)致的。它們大部分的時候,都是帶有有效數(shù)字位。** 由于在運算的時候,float類型和double很容易丟失精度,演示案例。所以,為了能精確的表示、計算浮點數(shù),Java提供了BigDecimal** BigDecimal類:不可變的、任意精度的有符號十進(jìn)制數(shù),可以解決數(shù)據(jù)丟失問題。*/public class BigDecimalDemo {public static void main(String[] args) {System.out.println(0.09 + 0.01);System.out.println(1.0 - 0.32);System.out.println(1.015 * 100);System.out.println(1.301 / 100);System.out.println(1.0 - 0.12);}}
import java.math.BigDecimal;/** 構(gòu)造方法:* public BigDecimal(String val)** public BigDecimal add(BigDecimal augend)* public BigDecimal subtract(BigDecimal subtrahend)* public BigDecimal multiply(BigDecimal multiplicand)* public BigDecimal divide(BigDecimal divisor)* public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode):商,幾位小數(shù),如何舍取*/public class BigDecimalDemo {public static void main(String[] args) {// System.out.println(0.09 + 0.01);// System.out.println(1.0 - 0.32);// System.out.println(1.015 * 100);// System.out.println(1.301 / 100);BigDecimal bd1 = new BigDecimal("0.09");BigDecimal bd2 = new BigDecimal("0.01");System.out.println("add:" + bd1.add(bd2));System.out.println("-------------------");BigDecimal bd3 = new BigDecimal("1.0");BigDecimal bd4 = new BigDecimal("0.32");System.out.println("subtract:" + bd3.subtract(bd4));System.out.println("-------------------");BigDecimal bd5 = new BigDecimal("1.015");BigDecimal bd6 = new BigDecimal("100");System.out.println("multiply:" + bd5.multiply(bd6));System.out.println("-------------------");BigDecimal bd7 = new BigDecimal("1.301");BigDecimal bd8 = new BigDecimal("100");System.out.println("divide:" + bd7.divide(bd8));System.out.println("divide:"+ bd7.divide(bd8, 3, BigDecimal.ROUND_HALF_UP));System.out.println("divide:"+ bd7.divide(bd8, 8, BigDecimal.ROUND_HALF_UP));}}
?
8. BigInteger
BigInteger(理解)
?? (1)針對大整數(shù)的運算
?? (2)構(gòu)造方法
????? A:BigInteger(String s)
?? (3)成員方法(自己補齊)
????? A:加
????? B:減
????? C:乘
????? D:除
????? E:商和余數(shù)
import java.math.BigInteger;/** BigInteger:可以讓超過Integer范圍內(nèi)的數(shù)據(jù)進(jìn)行運算** 構(gòu)造方法:* BigInteger(String val)*/public class BigIntegerDemo {public static void main(String[] args) {// 這幾個測試,是為了簡單超過int范圍內(nèi),Integer就不能再表示,所以就更談不上計算了。// Integer i = new Integer(100);// System.out.println(i);// // System.out.println(Integer.MAX_VALUE);// Integer ii = new Integer("2147483647");// System.out.println(ii);// // NumberFormatException// Integer iii = new Integer("2147483648");// System.out.println(iii);// 通過大整數(shù)來創(chuàng)建對象BigInteger bi = new BigInteger("2147483648");System.out.println("bi:" + bi);}}
?
import java.math.BigInteger;/** public BigInteger add(BigInteger val):加* public BigInteger subtract(BigInteger val):減* public BigInteger multiply(BigInteger val):乘* public BigInteger divide(BigInteger val):除* public BigInteger[] divideAndRemainder(BigInteger val):返回商和余數(shù)的數(shù)組*/public class BigIntegerDemo {public static void main(String[] args) {BigInteger bi1 = new BigInteger("100");BigInteger bi2 = new BigInteger("50");// public BigInteger add(BigInteger val):加System.out.println("add:" + bi1.add(bi2));// public BigInteger subtract(BigInteger val):加System.out.println("subtract:" + bi1.subtract(bi2));// public BigInteger multiply(BigInteger val):加System.out.println("multiply:" + bi1.multiply(bi2));// public BigInteger divide(BigInteger val):加System.out.println("divide:" + bi1.divide(bi2));// public BigInteger[] divideAndRemainder(BigInteger val):返回商和余數(shù)的數(shù)組BigInteger[] bis = bi1.divideAndRemainder(bi2);System.out.println("商:" + bis[0]);System.out.println("余數(shù):" + bis[1]);}}
?
9. Calendar
/** Calendar:它為特定瞬間與一組諸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日歷字段之間的轉(zhuǎn)換提供了一些方法,并為操作日歷字段(例如獲得下星期的日期)提供了一些方法。** public int get(int field):返回給定日歷字段的值。日歷類中的每個日歷字段都是靜態(tài)的成員變量,并且是int類型。*/public class CalendarDemo {public static void main(String[] args) {// 其日歷字段已由當(dāng)前日期和時間初始化:Calendar rightNow = Calendar.getInstance(); // 子類對象// 獲取年int year = rightNow.get(Calendar.YEAR);// 獲取月int month = rightNow.get(Calendar.MONTH);// 獲取日int date = rightNow.get(Calendar.DATE);System.out.println(year + "年" + (month + 1) + "月" + date + "日");}}
/** abstract class Person { public static Person getPerson() { return new* Student(); } }** class Student extends Person {** }*/
/** public void add(int field,int amount):根據(jù)給定的日歷字段和對應(yīng)的時間,來對當(dāng)前的日歷進(jìn)行操作。* public final void set(int year,int month,int date):設(shè)置當(dāng)前日歷的年月日*/public class CalendarDemo {public static void main(String[] args) {// 獲取當(dāng)前的日歷時間Calendar c = Calendar.getInstance();// 獲取年int year = c.get(Calendar.YEAR);// 獲取月int month = c.get(Calendar.MONTH);// 獲取日int date = c.get(Calendar.DATE);System.out.println(year + "年" + (month + 1) + "月" + date + "日");// // 三年前的今天// c.add(Calendar.YEAR, -3);// // 獲取年// year = c.get(Calendar.YEAR);// // 獲取月// month = c.get(Calendar.MONTH);// // 獲取日// date = c.get(Calendar.DATE);// System.out.println(year + "年" + (month + 1) + "月" + date + "日");// 5年后的10天前c.add(Calendar.YEAR, 5);c.add(Calendar.DATE, -10);// 獲取年year = c.get(Calendar.YEAR);// 獲取月month = c.get(Calendar.MONTH);// 獲取日date = c.get(Calendar.DATE);System.out.println(year + "年" + (month + 1) + "月" + date + "日");System.out.println("--------------");c.set(2011, 11, 11);// 獲取年year = c.get(Calendar.YEAR);// 獲取月month = c.get(Calendar.MONTH);// 獲取日date = c.get(Calendar.DATE);System.out.println(year + "年" + (month + 1) + "月" + date + "日");}}
/** 獲取任意一年的二月有多少天** 分析:* A:鍵盤錄入任意的年份* B:設(shè)置日歷對象的年月日* 年就是A輸入的數(shù)據(jù)* 月是2* 日是1* C:把時間往前推一天,就是2月的最后一天* D:獲取這一天輸出即可*/public class CalendarTest {public static void main(String[] args) {// 鍵盤錄入任意的年份Scanner sc = new Scanner(System.in);System.out.println("請輸入年份:");int year = sc.nextInt();// 設(shè)置日歷對象的年月日Calendar c = Calendar.getInstance();c.set(year, 2, 1); // 其實是這一年的3月1日// 把時間往前推一天,就是2月的最后一天c.add(Calendar.DATE, -1);// 獲取這一天輸出即可System.out.println(c.get(Calendar.DATE));}}
/** Random:產(chǎn)生隨機(jī)數(shù)的類** 構(gòu)造方法:* public Random():沒有給種子,用的是默認(rèn)種子,是當(dāng)前時間的毫秒值* public Random(long seed):給出指定的種子** 給定種子后,每次得到的隨機(jī)數(shù)是相同的。** 成員方法:* public int nextInt():返回的是int范圍內(nèi)的隨機(jī)數(shù)* public int nextInt(int n):返回的是[0,n)范圍的內(nèi)隨機(jī)數(shù)*/public class RandomDemo {public static void main(String[] args) {// 創(chuàng)建對象// Random r = new Random();Random r = new Random(1111);for (int x = 0; x < 10; x++) {// int num = r.nextInt();int num = r.nextInt(100) + 1;System.out.println(num);}}}
?
13. System
System(掌握)
?? (1)系統(tǒng)類,提供了一些有用的字段和方法
?? (2)成員方法(自己補齊)
????? A:運行垃圾回收器
????? B:退出jvm
????? C:獲取當(dāng)前時間的毫秒值
????? D:數(shù)組復(fù)制
/** System類包含一些有用的類字段和方法。它不能被實例化。** 方法:* public static void gc():運行垃圾回收器。* public static void exit(int status)* public static long currentTimeMillis()* public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)*/public class SystemDemo {public static void main(String[] args) {Person p = new Person("趙雅芝", 60);System.out.println(p);p = null; // 讓p不再指定堆內(nèi)存System.gc();}}
/** System類包含一些有用的類字段和方法。它不能被實例化。** 方法:* public static void gc():運行垃圾回收器。* public static void exit(int status):終止當(dāng)前正在運行的 Java 虛擬機(jī)。參數(shù)用作狀態(tài)碼;根據(jù)慣例,非 0 的狀態(tài)碼表示異常終止。* public static long currentTimeMillis():返回以毫秒為單位的當(dāng)前時間* public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)*/public class SystemDemo {public static void main(String[] args) {// System.out.println("我們喜歡林青霞(東方不敗)");// System.exit(0);// System.out.println("我們也喜歡趙雅芝(白娘子)");// System.out.println(System.currentTimeMillis());// 單獨得到這樣的實際目前對我們來說意義不大// 那么,它到底有什么作用呢?// 要求:請大家給我統(tǒng)計這段程序的運行時間long start = System.currentTimeMillis();for (int x = 0; x < 100000; x++) {System.out.println("hello" + x);}long end = System.currentTimeMillis();System.out.println("共耗時:" + (end - start) + "毫秒");}}
/** System類包含一些有用的類字段和方法。它不能被實例化。** 方法:* public static void gc():運行垃圾回收器。* public static void exit(int status):終止當(dāng)前正在運行的 Java 虛擬機(jī)。參數(shù)用作狀態(tài)碼;根據(jù)慣例,非 0 的狀態(tài)碼表示異常終止。* public static long currentTimeMillis():返回以毫秒為單位的當(dāng)前時間* public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)* 從指定源數(shù)組中復(fù)制一個數(shù)組,復(fù)制從指定的位置開始,到目標(biāo)數(shù)組的指定位置結(jié)束。*/public class SystemDemo {public static void main(String[] args) {// 定義數(shù)組int[] arr = { 11, 22, 33, 44, 55 };int[] arr2 = { 6, 7, 8, 9, 10 };// 請大家看這個代碼的意思System.arraycopy(arr, 1, arr2, 2, 2);System.out.println(Arrays.toString(arr));System.out.println(Arrays.toString(arr2));}}