《Java基礎知識總結》系列是對本身學習Java歷程中知識的一個總結,把這些JAVA基礎知識分享給你們。,本文主講JAVA數據類型基礎,具體以下:java
簡單數據類型包括:
整型(Interger): byte, short, int, long
浮點類型(Floating): float, double
字符類型(Textual): char
布爾類型(Logical): boolean數組
複合數據類型包括:
class
interface
數組
Stringapp
常量:
用final. eg: final int NUM = 100;ide
變量:
局部變量、類變量、方法參數、例外處理參數學習
簡單數據間的優先數據關係:
低-->高: byte,short,char->int->long->float->doublespa
自動轉換:
運算中,不一樣類型數據運算將自動轉換,從低級到高級orm
強制轉換:
高級數據要轉換成低級數據,需強制轉換
eg: int i;
byte b = (byte)i;對象
運算符:
基本與c語言一致,實例運算符instanceof, 內存分配運算符newip
複合語句:
基本與c語言一致內存
例外處理語句:
包括try, catch, finally, throw
數組:(簡單數據類型與複合數據類型的差異)
一維數組:
1. 定義
type intArray[];
eg: int intArray[];
Date dateArray[];
2. 一位數組的初始化
靜態初始化:
int intArray[] = {1, 2, 3, 4};
String stringArray[] = {"abc", "How", "You"};
動態初始化:
簡單類型的數組:
int intArray[];
intArray = new int[];
複合類型的數組:
String stringArray[];
String stringArray = new String[3]; // 爲數組中每一個元素開闢引用
stringArray[0] = new String("How"); // 開闢空間
stringArray[1] = new String("are");
stringArray[2] = new String("you");
3. 一維數組的引用
arrayName[index]
arrayName.length屬性指明數組的長度
多維數組:
1. 二維數組的定義:
type arrayName[][];
type [][]arrayName;
2. 二維數組的初始化:
靜態初始化
int intArray[][] = {{1, 2}, {2, 3}, {3, 4, 5}};
java中, 把二維數組當作時數組的數組, 空間並非連續的,並不要求每一維的大小相同
動態初始化
直接爲每一維分配空間:
arrayName = new type[arrayLength1][arrayLength2];
int a[][] = new int[2][3];
從最高維開始,分別爲每一維分配空間:
arrayName = new type[arrayLength1][];
arrayName[0] = new type[arrayLength20];
arrayName[1] = new type[arrayLength21];
...
eg: int a[][] = new int[2][];
a[0] = new int[3];
a[1] = new int[5];
二維複合數據類型數組,只能以此種方式
eg: String s[][] = new String[2][];
s[0] = new String[2]; // 爲最高維分配引用空間
s[1] = new String[2];
s[0][0] = new String("Good"); //分配空間
s[0][1] = new String("Luck");
s[1][0] = new String("to");
s[1][1] = new String("You");
3. 二維數組的引用:
arrayName[index1][index2]; eg: num[1][2];
4. 二維數組舉例
public class MatrixMultiply
{
public static void main(String args[]) {
int i, j, k;
int a[][] = new int[2][3];
int b[][] = {{1, 5, 2, 8}, {9, 5, 10, -3}, {2, 7, -5, -18}};
int c[][] = new int[2][4];
for (i = 0; i < 2; i++)
for (j = 0; j < 3; j++)
a[i][j] = (i + 1) * (j + 2);
for (i = 0; i < 2; i++) {
for (j = 0; j < 4; j++) {
c[i][j] = 0;
for (k = 0; k < 3; k++)
c[i][j] += a[i][k] * b[k][j];
}
}
System.out.println("*********Matrix C***********");
for (i = 0; i < 2; i++) {
for (j = 0; j < 4; j++)
System.out.println(c[i][j] + " ");
System.out.println();
}
}
}
Java語言中,把字符串做爲對象來處理,類String和StringBuffer均可以用來表示一個字符串。
1. 字符串常量
"Hello World"
2. String表示字符串常量
String(char chars[]); // 用字符數組初始化String
String(char chars[], int startIndex, int numChars);
String(byte ascii[], int hiByte); // 用字節數組初始化String,hiByte表示高8位,通常爲0
String(byte ascii[], int hiByte, int startIndex, int numChars);
使用不一樣方法生成"abc"
char chars1[] = {'a', 'b', 'c'};
char chars2[] = {'a', 'b', 'c', 'd', 'e'};
String s1 = new String(chars1);
String s2 = new String(chars2, 0, 3); // 取chars2[]的前三個字符
byte ascii1[] = {97, 98, 99};
byte ascii2[] = {97, 98, 99, 100, 101};
String s3 = new String(ascii1, 0);
String s4 = new String(ascii2, 0, 0, 3); //取前三個
3. 用StringBuffer表示字符串
StringBuffer(); // 分配16個字符串的緩衝區
StringBuffer(int len); // 分配len個字符的緩衝區
StringBuffer(String s); // 除了按照s的大小分配空間外,再分配16個字符的緩衝區
4. 訪問字符串
類String提供了length() charAt() indexOf() lastIndexOf() getChars() getBytes() toCharArray()等方法
public int length() 返回字符串的字符個數
public char charAt(int index) 返回字符串中index位置上的字符,index範圍:0--length-1
public int indexOf(char ch) 返回字符ch在字符串中出現的第一個位置
public int lastIndexOf(char ch) 返回字符ch在字符串中出現的最後一個位置
public int indexOf(String str) 返回子串str在字符串中出現的第一個位置
public int lastIndexOf(String str) 返回子串str在字符串中出現的最後一個位置
public int indexOf(char ch, int fromIndex) 返回字符ch在字符串中fromIndex之後出現的第一個位置
public int lastIndexOf(char ch, int fromIndex) 返回字符ch在字符串中fromIndex之後出現的最後一個位置
public int indexOf(String str, int fromIndex) 返回子串str在字符串中fromIndex之後出現的第一個位置
public int lastIndexOf(String str, int fromIndex) 返回子串str在字符串中fromIndex之後出現的最後一個位置
public void getChars(int srcbegin, int end, char buf[], int dstbegin)
srcbegin 要提取的第一個字符在源串中的位置
end 要提取的最後一個字符在源串中的位置
buf[] 目的字符串
dstbigin 提取的字符串的在目的字符串中起始位置
public void getBytes(int srcbegin, int serEnd, byte[] dst, int dstBegin)
用法同上,只是爲8位
類StringBuffer提供了length() charAt() getChars() capacity()等方法
capacity()用來獲得資環穿緩衝區的容量,與length()方法所返回的值一般是不一樣的
5. 修改字符串
類String提供的方法 contat() replace() substring() toLowerCase() toUpperCase()
public String contat(string str) 將當前字符串與str鏈接起來
public String replace(char oldChar, char newChar); 將當前字符串中oldChar換成newChar
public String substring(int beginIndex); 返回子串
public String subString(int beginIndex, int endIndex);
public String toLowerCase(); 把串中全部字符變成小寫
public String toUpperCase(); 把傳中全部字符變成大寫
類StringBuffer提供的方法 append() insert() setCharAt()
若操做後的字符已超出分配的緩衝區,系統自動爲它分配額外的空間
public synchronized StringBuffer append(String str); 在當前字符串末尾添加一個字符串str
public synchronized StringBuffer insert(int offset, String str); 在當前字符串的offset處,插入str
public synchronized void setChatAt(int index, char ch); 設定指定index處的值
String和StringBuffer的區別在於:String是對拷貝進行操做,而StringBuffer是直接對字符串操做
6. 其餘操做
字符串比較
String中的方法:
equals()
equalsIgnoreCase()
這兩個方法與'=='的區別:'=='只關心是否引用同一對象,此兩種方法關心對應的字符是否相同
字符串的轉化
java.lang.Object中提供了方法
toString() 把對象轉化爲字符串
'+'操做
PS:如趕上什麼問題,請直接留言或者直接在羣457036818提出