JAVA-常用類

1、包裝類

在JAVA中,將基本類型數(shù)據(jù)封裝在包裝類中,這樣便可以把基本數(shù)據(jù)類型轉(zhuǎn)換為對象類型處理。
包裝類詳單于基本數(shù)據(jù)類型與對象類型之間的一個橋梁,這里有裝箱和拆箱的概念,裝箱就是將基本類型轉(zhuǎn)換為包裝類,而拆箱就是將包裝類轉(zhuǎn)換為基本數(shù)據(jù)類型。
JAVA中的包裝類及其對應(yīng)的基本數(shù)據(jù)類型如下表所示:

包裝類 基本數(shù)據(jù)類型
Byte byte
Short short
Integer int
Long long
Float float
Double double
Character char
Boolean boolean

Integer類
我們可以使用valueOf方法創(chuàng)建一個Integer對象,也可以用new的方法來創(chuàng)建。

package CommonClass;

public class IntegerDemo {
    public static void main(String[] args){
        int num = Integer.parseInt("456");
        Integer iNum = Integer.valueOf("456");
        Integer iNum2 = new Integer("456");
        System.out.println("int數(shù)據(jù)與Integer數(shù)據(jù)的比較:" + iNum.equals(num));
    }
}

輸出為:

int數(shù)據(jù)與Integer數(shù)據(jù)的比較:true

Double類
Double類的操作與Integer類基本相同。

package CommonClass;

public class DoubleDemo {
    public static void main(String[] args){
        Double dNum = new Double("3.14");
        double num = Double.parseDouble("3.14");
        Double dNum2 = Double.valueOf("3.14");
        System.out.println("double數(shù)據(jù)和Double數(shù)據(jù)比較:"+dNum.equals(num));
        System.out.println("整數(shù)部分為:"+dNum.intValue());
    }
}

輸出為:

double數(shù)據(jù)和Double數(shù)據(jù)比較:true
整數(shù)部分為:3

Boolean類
Boolean類也提供了兩種構(gòu)造方法,當(dāng)使用字符串時,只有當(dāng)字符串不為null且忽略大小寫時為true的情況下才為true,其他情況都是false。

package CommonClass;

public class BooleanDemo {
    public static void main(String[] args){
        Boolean b1 = new Boolean(true);
        Boolean b2 = new Boolean("ok");
        Boolean b3 = new Boolean("True");
        System.out.println("b1:" + b1.booleanValue());
        System.out.println("b2:" + b2.booleanValue());
        System.out.println("b3:" + b3.booleanValue());
    }
}

輸出為

b1:true
b2:false
b3:true

Character類
Character類提供了很多方法,可以確定字符的類型,以及很方便的將字符轉(zhuǎn)換大小寫等方法

package CommonClass;

public class CharacterDemo {
    public static void main(String[] args){
        Character mychar1 = new Character('A');
        Character mychar2 = new Character('a');
        Character mychar3 = new Character('1');
        if(Character.isUpperCase(mychar1)){
            System.out.println("mychar1轉(zhuǎn)換為小寫字母為:" + Character.toLowerCase(mychar1));
        }
        if(Character.isLowerCase(mychar2)){
            System.out.println("mychar2轉(zhuǎn)換為大寫字母為:" + Character.toUpperCase(mychar2));
        }
        if(Character.isDigit(mychar3)){
            System.out.println("mychar3是為數(shù)字字符" );
        }
    }
}

輸出為:

mychar1轉(zhuǎn)換為小寫字母為:a
mychar2轉(zhuǎn)換為大寫字母為:A
mychar3是為數(shù)字字符

2、Math類

Math 類包含常用的數(shù)學(xué)方法以及常用的數(shù)學(xué)常量。數(shù)學(xué)方法包括三角函數(shù)方法sin、cos、tan等,指數(shù)函數(shù)方法exp,log、log10、sqrt等、取整函數(shù)方法ceil、floor、round等遺跡最大值,最小值和絕對值函數(shù)方法。

package CommonClass;

public class MathDemo {
    public static void main(String[] args){
        System.out.println("90度的正弦值:" + Math.sin(Math.PI / 2));
        System.out.println("2的平方根與2商的反弦值:" + Math.asin(Math.sqrt(2)/2));
        System.out.println("e的平方值:"+ Math.exp(2));
        System.out.println("以e為底2的對數(shù)值:" + Math.log(2));
        System.out.println("以10為底2的對數(shù)值:" + Math.log10(2));
        System.out.println("2的平方根:" + Math.sqrt(2));
        System.out.println("2的平方:" + Math.pow(2,2));
        System.out.println("不小于3.5的最小整數(shù)" + Math.ceil(3.5));
        System.out.println("不大于3.5的最大整數(shù):" + Math.floor(3.5));
        System.out.println("3.5最接近的整數(shù)為,同樣接近取偶數(shù):" + Math.rint(3.5));

    }
}

輸出為:

90度的正弦值:1.0
2的平方根與2商的反弦值:0.7853981633974484
e的平方值:7.38905609893065
以e為底2的對數(shù)值:0.6931471805599453
以10為底2的對數(shù)值:0.3010299956639812
2的平方根:1.4142135623730951
2的平方:4.0
不小于3.5的最小整數(shù)4.0
不大于3.5的最大整數(shù):3.0
3.5最接近的整數(shù)為,同樣接近取偶數(shù):4.0

3、隨機數(shù)

Math.Random()方法
Math.Random()方法返回一個大于等于0,小于1的隨機數(shù)。該方法返回的隨機數(shù)實際上是偽隨機數(shù),它通過復(fù)雜的運算得到一系列的數(shù),該方法是通過當(dāng)前時間作為隨機數(shù)生成器的參數(shù),所以每次執(zhí)行程序都會產(chǎn)生不同的隨機數(shù)。

Random類
我們通過一個例子來了解Random類的使用:

package CommonClass;

import java.util.Random;
import java.util.Scanner;

public class RandomDemo {
    public static void main(String[] args){
        System.out.println("------模擬微信搶紅包--------");
        Random r = new Random();
        Scanner sc = new Scanner(System.in);
        System.out.println("請輸入紅包金額:");
        double total = sc.nextDouble();
        System.out.println("請輸入紅包總數(shù):");
        int bagsnum = sc.nextInt();
        double min = 0.01;

        for (int i = 1;i<bagsnum;i++){
            //后面每個至少有1分
            double max = total - (bagsnum-i) * min;
            double bound = max - min;
            double safe = (double)r.nextInt((int)(bound * 100))/100;
            double money = safe + min;
            total = total - money;
            System.out.println("第" + i + "個紅包的金額是:" + String.format("%.2f",money));
        }
        System.out.println("第" + bagsnum + "個紅包的金額是:" + String.format("%.2f",total));
    }
}

輸出為:

------模擬微信搶紅包--------
請輸入紅包金額:
10
請輸入紅包總數(shù):
10
第1個紅包的金額是:0.46
第2個紅包的金額是:1.42
第3個紅包的金額是:5.41
第4個紅包的金額是:1.64
第5個紅包的金額是:0.22
第6個紅包的金額是:0.07
第7個紅包的金額是:0.50
第8個紅包的金額是:0.17
第9個紅包的金額是:0.03
第10個紅包的金額是:0.08

4、日期時間類

Date類
目前Date類只有兩種構(gòu)造方法,Date()和Date(long date),后一種方法需要傳入一個毫秒數(shù):

package CommonClass;

import java.util.Date;
public class DateDemo {
    public static void main(String[] args){
        Date d1 = new Date();
        long timeMillis = System.currentTimeMillis();
        Date d2 = new Date(timeMillis);
        long value = d2.getTime();
        System.out.println("日期" + d1);
        System.out.println("毫秒數(shù)" + value);
    }    
}

輸出為:

日期Sat Jan 27 13:57:19 CST 2018
毫秒數(shù)1517032639754

DataFormat類
DateFormat 類是日期/時間格式化子類的抽象類,在java.text包中,一般使用的是SimpleDateFormat類:

package CommonClass;

import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Date;

public class DateFormatDemo {
    public static void main(String[] args) {
        DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss EE");
        DateFormat df2 = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = new Date();
        System.out.println("格式化輸出日期1:" + df1.format(date));
        System.out.println("格式化輸出日期2:" + df2.format(date));
    }
}

輸出為:

格式化輸出日期1:2018-01-27 02:03:16 星期六
格式化輸出日期2:2018年01月27日

Calendar類
為了更加便捷的對日期進(jìn)行操作,Calendar類對YEAR、MONTH、DAY_OF_MONTH、HOUR等日歷字段之間的轉(zhuǎn)換提供了一些方法,并為操作日歷字段(例如獲得下星期的日期)提供了一些方法。

package com.test.calendar;

import java.util.Calendar;

import org.junit.Before;
import org.junit.Test;

public class CalendarDemo {
    Calendar calendar = null;

    @Before
    public void test() {
        calendar = Calendar.getInstance();
    }

    // 基本用法,獲取年月日時分秒星期
    @Test
    public void test1() {
        // 獲取年
        int year = calendar.get(Calendar.YEAR);

        // 獲取月,這里需要需要月份的范圍為0~11,因此獲取月份的時候需要+1才是當(dāng)前月份值
        int month = calendar.get(Calendar.MONTH) + 1;

        // 獲取日
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        // 獲取時
        int hour = calendar.get(Calendar.HOUR);
        // int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24小時表示

        // 獲取分
        int minute = calendar.get(Calendar.MINUTE);

        // 獲取秒
        int second = calendar.get(Calendar.SECOND);

        // 星期,英語國家星期從星期日開始計算
        int weekday = calendar.get(Calendar.DAY_OF_WEEK);

        System.out.println("現(xiàn)在是" + year + "年" + month + "月" + day + "日" + hour
                + "時" + minute + "分" + second + "秒" + "星期" + weekday);
    }

    // 一年后的今天
    @Test
    public void test2() {
        // 同理換成下個月的今天calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.YEAR, 1);

        // 獲取年
        int year = calendar.get(Calendar.YEAR);

        // 獲取月
        int month = calendar.get(Calendar.MONTH) + 1;

        // 獲取日
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        System.out.println("一年后的今天:" + year + "年" + month + "月" + day + "日");
    }

    // 獲取任意一個月的最后一天
    @Test
    public void test3() {
        // 假設(shè)求6月的最后一天
        int currentMonth = 6;
        // 先求出7月份的第一天,實際中這里6為外部傳遞進(jìn)來的currentMonth變量
        // 1
        calendar.set(calendar.get(Calendar.YEAR), currentMonth, 1);

        calendar.add(Calendar.DATE, -1);

        // 獲取日
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        System.out.println("6月份的最后一天為" + day + "號");
    }

    // 設(shè)置日期
    @Test
    public void test4() {
        calendar.set(Calendar.YEAR, 2000);
        System.out.println("現(xiàn)在是" + calendar.get(Calendar.YEAR) + "年");

        calendar.set(2008, 8, 8);
        // 獲取年
        int year = calendar.get(Calendar.YEAR);

        // 獲取月
        int month = calendar.get(Calendar.MONTH);

        // 獲取日
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        System.out.println("現(xiàn)在是" + year + "年" + month + "月" + day + "日");
    }
}

輸出為:

現(xiàn)在是2016年11月7日11時42分18秒星期2
一年后的今天:2017年11月7日
6月份的最后一天為30號
現(xiàn)在是2000年
現(xiàn)在是2008年8月8日
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 參考文獻(xiàn):《Java瘋狂講義》(第三版) Objects工具類 知識點: 它提供了一些工具方法來操作對象,這些工具...
    houc閱讀 746評論 0 0
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,696評論 18 399
  • 曾經(jīng)看到一篇文章說,因為一個人愛上一座城市。雖然之前和那座城市是那么的不相干,卻在猛然中,因為某個人因為某段情因為...
    知易行難789閱讀 472評論 0 0
  • 晴,宜思考。 突然開始反思自己,我終于發(fā)現(xiàn)自己的學(xué)習(xí)習(xí)慣有多不好,容易在學(xué)習(xí)的時候拿起手機查閱資料的時候,然后點開...
    傻孩子_shz閱讀 278評論 0 0
  • 今天,2017年9月18日。你們聽起來是不是覺得這個日子很普通?。侩m然對你們很普通,但是對我們來說就很重要。因為今...
    懶貓鳶閱讀 514評論 3 4

友情鏈接更多精彩內(nèi)容