摘要:基本數(shù)據(jù)類(lèi)型對(duì)象包裝類(lèi)概述基本數(shù)據(jù)類(lèi)型對(duì)象包裝類(lèi)概述基本類(lèi)型包裝類(lèi)的產(chǎn)生在實(shí)際程序使用中,程序界面上用戶(hù)輸入的數(shù)據(jù)都是以字符串類(lèi)型進(jìn)行存儲(chǔ)的。
01基本數(shù)據(jù)類(lèi)型對(duì)象包裝類(lèi)概述
*A:基本數(shù)據(jù)類(lèi)型對(duì)象包裝類(lèi)概述 *a.基本類(lèi)型包裝類(lèi)的產(chǎn)生 在實(shí)際程序使用中,程序界面上用戶(hù)輸入的數(shù)據(jù)都是以字符串類(lèi)型進(jìn)行存儲(chǔ)的。而程序開(kāi)發(fā)中,我們需要把字符串?dāng)?shù)據(jù),根據(jù)需求轉(zhuǎn)換成指定的基本數(shù)據(jù)類(lèi)型,如年齡需要轉(zhuǎn)換成int類(lèi)型,考試成績(jī)需要轉(zhuǎn)換成double類(lèi)型等 *b.八種基本類(lèi)型對(duì)應(yīng)的包裝類(lèi) char Character int Integer byte Byte short Short long Long float Float double Double boolean Boolean02Integer類(lèi)parseInt方法
*A:Integer類(lèi)parseInt方法: *a:parseInt() int i = Integer.parseInt("12"); System.out.println(i/2);//6 *b:parseInt(String s, int radix) /* * Integer類(lèi)靜態(tài)方法parseInt(String s, int radix) * radix基數(shù),進(jìn)制 * "110",2 含義 前面的數(shù)字是二進(jìn)制的,但是方法parseInt運(yùn)行結(jié)果都是十進(jìn)制 * 指定進(jìn)制的字符串轉(zhuǎn)換為十進(jìn)制的整數(shù) */ public static void function_1(){ int i = Integer.parseInt("110", 2); System.out.println(i); }03Integer類(lèi)int轉(zhuǎn)成字符串
*A:Integer類(lèi)int轉(zhuǎn)成字符串: *a:使用+與字符串拼接 int i = 3; String s = i+""; System.out.println(s+1);//"31" *b:toString(int ,int 進(jìn)制),任意進(jìn)制整數(shù)轉(zhuǎn)成任意進(jìn)制的字符串 (了解) String s1 = Integer.toString(5,2); System.out.println(s1);04Integer類(lèi)構(gòu)造方法
*A:Integer類(lèi)構(gòu)造方法 /* * Integer類(lèi)構(gòu)造方法 * Integer (String s) * 將數(shù)字格式的字符串,傳遞到Integer類(lèi)的構(gòu)造方法中 * 創(chuàng)建Integer對(duì)象,包裝的是一個(gè)字符串 * 將構(gòu)造方法中的字符串,轉(zhuǎn)成基本數(shù)據(jù)類(lèi)型,調(diào)用方法,非靜態(tài)的, intValue() */ public static void function_3(){ Integer in = new Integer("100"); int i = in.intValue(); System.out.println(--i);//99 }05Integer類(lèi)其他方法
*A:Integer類(lèi)其他方法
/* * Integer類(lèi)的3個(gè)靜態(tài)方法 * 做進(jìn)制的轉(zhuǎn)換 * 十進(jìn)制轉(zhuǎn)成二進(jìn)制 toBinarString(int) * 十進(jìn)制轉(zhuǎn)成八進(jìn)制 toOctalString(int) * 十進(jìn)制轉(zhuǎn)成十六進(jìn)制 toHexString(int) * 三個(gè)方法,返回值都是以String形式出現(xiàn) */ a:十進(jìn)制轉(zhuǎn)二,八,十六進(jìn)制 public static void function_1(){ System.out.println(Integer.toBinaryString(99)); System.out.println(Integer.toOctalString(99)); System.out.println(Integer.toHexString(999)); } b:獲取int的最大值和最小值 /* * Integer類(lèi)的靜態(tài)成員變量 * MAX_VALUE * MIN_VALUE */ public static void function(){ System.out.println(Integer.MAX_VALUE); System.out.println(Integer.MIN_VALUE); }06自動(dòng)裝箱和自動(dòng)拆箱
*A:自動(dòng)裝箱與自動(dòng)拆箱:
//JDK1.5新特性 //自動(dòng)裝箱,拆箱的 好處: 基本類(lèi)型和引用類(lèi)直接運(yùn)算 //自動(dòng)裝箱:使用Integer.valueOf(整數(shù)值)返回一個(gè)封裝了該整數(shù)值的Integer對(duì)象 //自動(dòng)拆箱:使用Integer對(duì)象.intValue()返回Integer對(duì)象中封裝的整數(shù)值 public static void function(){ //引用類(lèi)型 , 引用變量一定指向?qū)ο? //自動(dòng)裝箱, 基本數(shù)據(jù)類(lèi)型1, 直接變成了對(duì)象 Integer in = 1; // Integer in = new Integer(1) //in 是引用類(lèi)型,不能和基本類(lèi)型運(yùn)算, 自動(dòng)拆箱,引用類(lèi)型in,轉(zhuǎn)換基本類(lèi)型 //in+1 ==> in.inValue()+1 = 2 //in = 2 自動(dòng)裝箱 in = in + 1; System.out.println(in); }07自動(dòng)裝箱和自動(dòng)拆箱練習(xí)題
*A:自動(dòng)裝箱與自動(dòng)拆箱:
Integer i = new Integer(1); Integer j = new Integer(1); System.out.println(i==j);// false 對(duì)象地址 System.out.println(i.equals(j));// true 繼承Object重寫(xiě)equals,比較的對(duì)象數(shù)據(jù) System.out.println("==================="); Integer a = 500;//Integer integer=Integer.valueOf(500) //integer=new Integer(500); Integer b = 500; System.out.println(a==b);//false System.out.println(a.equals(b));//true System.out.println("==================="); //數(shù)據(jù)在byte(-128~127)范圍內(nèi),JVM不會(huì)從新new對(duì)象 Integer aa = 127; // Integer aa = new Integer(127) Integer bb = 127; // Integer bb = aa; System.out.println(aa==bb); //true System.out.println(aa.equals(bb));//true
=========================第二節(jié)課開(kāi)始====================================
08System類(lèi)方法currentTimeMillis*A:System類(lèi)方法currentTimeMillis():用于計(jì)算程序的執(zhí)行時(shí)間
/* * 獲取系統(tǒng)當(dāng)前毫秒值 * static long currentTimeMillis() * 對(duì)程序執(zhí)行時(shí)間測(cè)試 */ public static void function(){ long start = System.currentTimeMillis();//當(dāng)前時(shí)間x-1970年1月1日零時(shí)零分零秒 for(int i = 0 ; i < 10000; i++){ System.out.println(i); } long end = System.currentTimeMillis();//當(dāng)前時(shí)間y-1970年1月1日零時(shí)零分零秒 System.out.println(end - start);//當(dāng)前時(shí)間y-當(dāng)前時(shí)間x }09System類(lèi)方法exit
*A:System類(lèi)方法exit()方法 /* * 退出虛擬機(jī),所有程序全停止 * static void exit(0) */ public static void function_1(){ while(true){ System.out.println("hello"); System.exit(0);//該方法會(huì)在以后的finally代碼塊中使用(講到再說(shuō)) } }10System類(lèi)方法gc
A:System類(lèi)方法gc
public class Person { public void finalize(){ System.out.println("垃圾收取了"); } } /* * JVM在內(nèi)存中,收取對(duì)象的垃圾 * 當(dāng)沒(méi)有更多引用指向該對(duì)象時(shí),會(huì)自動(dòng)調(diào)用垃圾回收機(jī)制回收堆中的對(duì)象 * 同時(shí)調(diào)用回收對(duì)象所屬類(lèi)的finalize方法() * static void gc() */ public static void function_2(){ new Person(); new Person(); new Person(); new Person(); new Person(); new Person(); new Person(); new Person(); System.gc(); }11System類(lèi)方法getProperties
A:System類(lèi)方法getProperties(了解)
/* * 獲取當(dāng)前操作系統(tǒng)的屬性:例如操作系統(tǒng)名稱(chēng), * static Properties getProperties() */ public static void function_3(){ System.out.println( System.getProperties() ); }12System類(lèi)方法arraycopy
A:System類(lèi)方法arraycopy: /* * System類(lèi)方法,復(fù)制數(shù)組 * arraycopy(Object src, int srcPos, Object dest, int destPos, int length) * Object src, 要復(fù)制的源數(shù)組 * int srcPos, 數(shù)組源的起始索引 * Object dest,復(fù)制后的目標(biāo)數(shù)組 * int destPos,目標(biāo)數(shù)組起始索引 * int length, 復(fù)制幾個(gè) */ public static void function_4(){ int[] src = {11,22,33,44,55,66}; int[] desc = {77,88,99,0}; System.arraycopy(src, 1, desc, 1, 2);//將src數(shù)組的1位置開(kāi)始(包含1位置)的兩個(gè)元素,拷貝到desc的1,2位置上 for(int i = 0 ; i < desc.length ; i++){ System.out.println(desc[i]); } }
================================第三節(jié)課開(kāi)始======================================================
13Math類(lèi)的方法_1A:Math類(lèi)中的方法
/*
* static double sqrt(double d) * 返回參數(shù)的平方根 */ public static void function_4(){ double d = Math.sqrt(-2); System.out.println(d); } /*0 * static double pow(double a, double b) * a的b次方 */ public static void function_3(){ double d = Math.pow(2, 3); System.out.println(d); } /* * static double floor(double d) * 返回小于或者等于參數(shù)d的最大整數(shù) */ public static void function_2(){ double d = Math.floor(1.5); System.out.println(d); } /* * static double ceil(double d) * 返回大于或者等于參數(shù)d的最小整數(shù) */ public static void function_1(){ double d = Math.ceil(5.1); System.out.println(d); } /* * static int abs(int i) * 獲取參數(shù)的絕對(duì)值 */ public static void function(){ int i = Math.abs(0); System.out.println(i); }14Math類(lèi)的方法_2
A:Math類(lèi)的方法_2
/*
static double round(doubl d)
獲取參數(shù)的四舍五入,取整數(shù)
*/
public static void function_6(){
double d = Math.round(5.4195); System.out.println(d);
}
/*
static double random() 返回隨機(jī)數(shù) 0.0-1.0之間
來(lái)源,也是Random類(lèi)
*/
public static void function_5(){
for(int i = 0 ; i < 10 ;i++){ double d = Math.random(); System.out.println(d); }
}
15Arrays工具類(lèi)A:Arrays工具類(lèi):
public class ArraysDemo { public static void main(String[] args) { function_2(); int[] arr = {56,65,11,98,57,43,16,18,100,200}; int[] newArray = test(arr); System.out.println(Arrays.toString(newArray)); } /* * 定義方法,接收輸入,存儲(chǔ)的是10個(gè)人考試成績(jī) * 將最后三個(gè)人的成績(jī),存儲(chǔ)到新的數(shù)組中,返回新的數(shù)組 */ public static int[] test(int[] arr){ //對(duì)數(shù)組排序 Arrays.sort(arr); //將最后三個(gè)成績(jī)存儲(chǔ)到新的數(shù)組中 int[] result = new int[3]; //成績(jī)數(shù)組的最后三個(gè)元素,復(fù)制到新數(shù)組中 // System.arraycopy(arr, 0, result, 0, 3); for(int i = 0 ; i < 3 ;i++){ result[i] = arr[i]; } return result; } /* * static String toString(數(shù)組) * 將數(shù)組變成字符串 */ public static void function_2(){ int[] arr = {5,1,4,6,8,9,0}; String s = Arrays.toString(arr); System.out.println(s); } /* * static int binarySearch(數(shù)組, 被查找的元素) * 數(shù)組的二分搜索法 * 返回元素在數(shù)組中出現(xiàn)的索引 * 元素不存在, 返回的是 (-插入點(diǎn)-1) */ public static void function_1(){ int[] arr = {1,4,7,9,11,15,18}; int index = Arrays.binarySearch(arr, 10); System.out.println(index); } /* * static void sort(數(shù)組) * 對(duì)數(shù)組升序排列 */ public static void function(){ int[] arr = {5,1,4,6,8,9,0}; Arrays.sort(arr); for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } }16數(shù)組復(fù)制練習(xí)
*A:數(shù)組復(fù)制練習(xí):
public static void main(String[] args) { int[] arr = {56,65,11,98,57,43,16,18,100,200}; int[] newArray = test(arr); System.out.println(Arrays.toString(newArray)); } /* * 定義方法,接收輸入,存儲(chǔ)的是10個(gè)人考試成績(jī) * 將最后三個(gè)人的成績(jī),存儲(chǔ)到新的數(shù)組中,返回新的數(shù)組 */ public static int[] test(int[] arr){ //對(duì)數(shù)組排序 Arrays.sort(arr); //將最后三個(gè)成績(jī)存儲(chǔ)到新的數(shù)組中 int[] result = new int[3]; //成績(jī)數(shù)組的最后三個(gè)元素,復(fù)制到新數(shù)組中 //System.arraycopy(arr, 0, result, 0, 3); for(int i = 0 ; i < 3 ;i++){ result[i] = arr[i]; } return result; }
====================第四節(jié)課開(kāi)始============================
17BigInteger類(lèi)概述和構(gòu)造方法A:BigInteger類(lèi)概述和構(gòu)造方法
public static void main(String[] args) {
function(); } /* * BigInteger類(lèi)的構(gòu)造方法 * 傳遞字符串,要求數(shù)字格式,沒(méi)有長(zhǎng)度限制 */ public static void function(){ BigInteger b = new BigInteger("8465846668464684562385634168451684568645684564564"); System.out.println(b); BigInteger b1 = new BigInteger("5861694569514568465846668464684562385634168451684568645684564564"); System.out.println(b1); }18BigInteger類(lèi)四則運(yùn)算
A:BigInteger類(lèi)四則運(yùn)算
public static void main(String[] args) { function_1(); } /* * BigInteger對(duì)象的四則運(yùn)算 * 調(diào)用方法計(jì)算,計(jì)算結(jié)果也只能是BigInteger對(duì)象 */ public static void function_1(){ BigInteger b1 = new BigInteger("5665464516451051581613661405146"); BigInteger b2 = new BigInteger("965855861461465516451051581613661405146"); //計(jì)算 b1+b2對(duì)象的和,調(diào)用方法 add BigInteger bigAdd = b1.add(b2);//965855867126930032902103163227322810292 System.out.println(bigAdd); //計(jì)算b1-b2對(duì)象的差,調(diào)用方法subtract BigInteger bigSub = b1.subtract(b2); System.out.println(bigSub); //計(jì)算b1*b2對(duì)象的乘積,調(diào)用方法multiply BigInteger bigMul = b1.multiply(b2); System.out.println(bigMul); //計(jì)算b2/b1對(duì)象商,調(diào)用方法divied BigInteger bigDiv = b2.divide(b1); System.out.println(bigDiv); }19員工案例的子類(lèi)的編寫(xiě)
A:BigDecimal類(lèi)概述
/* * 計(jì)算結(jié)果,未知 * 原因: 計(jì)算機(jī)二進(jìn)制中,表示浮點(diǎn)數(shù)不精確造成 * 超級(jí)大型的浮點(diǎn)數(shù)據(jù),提供高精度的浮點(diǎn)運(yùn)算, BigDecimal System.out.println(0.09 + 0.01);//0.09999999999999999 System.out.println(1.0 - 0.32);//0.6799999999999999 System.out.println(1.015 * 100);//101.49999999999999 System.out.println(1.301 / 100);//0.013009999999999999 */20BigDecimal類(lèi)實(shí)現(xiàn)加法減法乘法
A:BigDecimal類(lèi)實(shí)現(xiàn)加法減法乘法
/*
* BigDecimal實(shí)現(xiàn)三則運(yùn)算 * + - * */ public static void function(){ BigDecimal b1 = new BigDecimal("0.09"); BigDecimal b2 = new BigDecimal("0.01"); //計(jì)算b1+b2的和,調(diào)用方法add BigDecimal bigAdd = b1.add(b2); System.out.println(bigAdd); BigDecimal b3 = new BigDecimal("1"); BigDecimal b4 = new BigDecimal("0.32"); //計(jì)算b3-b2的差,調(diào)用方法subtract BigDecimal bigSub = b3.subtract(b4); System.out.println(bigSub); BigDecimal b5 = new BigDecimal("1.015"); BigDecimal b6 = new BigDecimal("100"); //計(jì)算b5*b6的成績(jī),調(diào)用方法 multiply BigDecimal bigMul = b5.multiply(b6); System.out.println(bigMul); }21BigDecimal類(lèi)實(shí)現(xiàn)除法
A:BigDecimal類(lèi)實(shí)現(xiàn)除法 /* * BigDecimal實(shí)現(xiàn)除法運(yùn)算 * divide(BigDecimal divisor, int scale, int roundingMode) * int scale : 保留幾位小數(shù) * int roundingMode : 保留模式 * 保留模式 閱讀API文檔 * static int ROUND_UP 向上+1 * static int ROUND_DOWN 直接舍去 * static int ROUND_HALF_UP >= 0.5 向上+1 * static int ROUND_HALF_DOWN > 0.5 向上+1 ,否則直接舍去 */ public static void function_1(){ BigDecimal b1 = new BigDecimal("1.0301"); BigDecimal b2 = new BigDecimal("100"); //計(jì)算b1/b2的商,調(diào)用方法divied BigDecimal bigDiv = b1.divide(b2,2,BigDecimal.ROUND_HALF_UP);//0.01301 System.out.println(bigDiv); }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/67167.html
摘要:常用類(lèi)概述包含執(zhí)行基本數(shù)字運(yùn)算的方法沒(méi)有構(gòu)造方法,如何使用類(lèi)中的成員呢看類(lèi)的成員是否都是靜態(tài)的,如果是,通過(guò)類(lèi)名就可以直接調(diào)用。所有類(lèi)都直接或間接的繼承該類(lèi)。 1 常用API1.1 Math1.1.1 Math類(lèi)概述Math包含執(zhí)行基本數(shù)字運(yùn)算的方法沒(méi)有構(gòu)造方法,如何使用類(lèi)中的成員呢?看類(lèi)的成員是否都是靜態(tài)的,...
摘要:成員方法類(lèi)概述用于產(chǎn)生隨機(jī)數(shù)成員方法正則表達(dá)式相關(guān)方法判斷功能分割功能替換功能獲取功能和類(lèi)的使用類(lèi)概述包含一些有用的類(lèi)字段和方法。注意它不能被實(shí)例化,因?yàn)榇祟?lèi)構(gòu)造器是私有的成員方法類(lèi)類(lèi)概述表示特定的瞬間,精確到毫秒。 前言 我們都知道,JDK包含了JRE,而JRE中也提供了各種功能的java類(lèi),現(xiàn)在我們就對(duì)這些類(lèi)有個(gè)簡(jiǎn)單了解,然后靈活運(yùn)用。 常用類(lèi):Object類(lèi)/Scanner類(lèi),...
摘要:構(gòu)造器沒(méi)有返回值一個(gè)對(duì)象變量并沒(méi)有實(shí)際包含一個(gè)對(duì)象,而僅僅引用一個(gè)對(duì)象,如有兩個(gè)部分。子類(lèi)重寫(xiě)方法的返回值范圍必須小于等于父類(lèi)方法的返回值。枚舉類(lèi)型中可以添加一些構(gòu)造器方法和域。 第三章 Java是一種強(qiáng)類(lèi)型語(yǔ)言。 https://blog.csdn.net/qq_3619... 在Java中,整型的范圍與機(jī)器無(wú)關(guān)。 int 4字節(jié) short 2字節(jié) long ...
摘要:在那里,可以理解為指針。局部變量不能夠被訪問(wèn)控制符及修飾都可以被修飾變量的傳遞與語(yǔ)言相似調(diào)用對(duì)象方法時(shí)要傳遞參數(shù)。內(nèi)部類(lèi)內(nèi)部類(lèi)是所在類(lèi)的成員。大體上相當(dāng)于其他語(yǔ)言的匿名函數(shù)或函數(shù)指針。 1. 變量及其傳遞 基本類(lèi)型變量(primitive type)和引用類(lèi)型變量(reference type) 基本類(lèi)型(primitive type):其值直接存于變量中。在這里 引用型(refer...
摘要:創(chuàng)建字符串教程字符串長(zhǎng)度用于獲取有關(guān)對(duì)象的信息的方法稱(chēng)為訪問(wèn)器方法。類(lèi)在中被提出,它和之間的最大不同在于的方法不是線程安全的不能同步訪問(wèn)。然而在應(yīng)用程序要求線程安全的情況下,則必須使用類(lèi)。 一般地,當(dāng)需要使用數(shù)字的時(shí)候,我們通常使用內(nèi)置數(shù)據(jù)類(lèi)型,如:byte、int、long、double 等。 在實(shí)際開(kāi)發(fā)過(guò)程中,我們經(jīng)常會(huì)遇到需要使用對(duì)象,而不是內(nèi)置數(shù)據(jù)類(lèi)型的情形。為了解決這個(gè)問(wèn)題,...
閱讀 1846·2021-09-14 18:03
閱讀 2278·2019-08-30 15:48
閱讀 1135·2019-08-30 14:09
閱讀 518·2019-08-30 12:55
閱讀 2740·2019-08-29 11:29
閱讀 1501·2019-08-26 13:43
閱讀 2325·2019-08-26 13:30
閱讀 2383·2019-08-26 12:17