java學習(一)——資料型別、String類、char型別
阿新 • • 發佈:2018-12-17
資料型別
Java是一種強型別語言:共有8種基本型別
1.整型:用於表示沒有小數點的部分
1.1
int 4位元組
long 8位元組
short 2位元組
byte 1位元組
1.2
長整型數值:字尾L/l
二進位制:字首0b/0B
八進位制: 字首0
十六進位制:字首Ox/0X,可以為數字字面量加下劃線,如1_000_000(或0b1111_0100_0010_0000)表示一百萬,(這些下劃線只是為了讓人更易讀,編譯器會忽略這些下劃線)
1.3
注:java沒有無符號(unsigned)整數型別
class FirstDemo
{
public static void main(String[] args)
{
byte a=23;
int c=345;
short b=78;
long d=1000_2300_45L;
short e=0b1111;
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
}
}
2.浮點型:用於表示有小數部分的數值
2.1
double 8位元組 有效位數6~7位
float 4位元組 有效位數15位
2.2
float 型別的數值必須有一個字尾f/F double 型別的數值可以新增字尾D/d 注:預設浮點型別是 double 型別,在將一個浮點型字面常量賦給float賦給 double 型別時,必須使用字尾f/F
2.3 特殊的浮點數值
正無窮大 Double.POSITIVE_INFTNITY
負無窮大 Double.NEGATIVE_INFINITY
NaN(不是一個數字) Double.NaN 如:0/0、負數的平方根
class FirstDemo
{
public static void main(String \u005B\u005D args)
{
float a1=23.4f;
double b2=456.6;
System.out.println(Double.NEGATIVE_INFINITY);
System.out.println(Double. POSITIVE_INFINITY);
System.out.println(Double.NaN);
}
}
執行結果
-Infinity
Infinity
NaN
3.char 型別:用於表示單個字元(採用Unicode字元)
表示方式
單引號括起來:'A'
十六進位制值(範圍 \u0000到\Uffff):\u03C0
表示特殊字元的轉義序列
4.boolean 型別:用於判定邏輯條件
false 假
true 真
class FirstDemo
{
public static void main(String \u005B\u005D args)
{
System.out.println("退格:\b");
System.out.println("換行:\n");
System.out.println("製表:\t");
System.out.println("回車:\r");
boolean t1=true;
System.out.println(t1);
}
}
變數常量和字串String
class FirstDemo
{
public static void main(String[] args)
{
//1.變數
int a1; //變數的宣告
double d1=45.3; //變數宣告並初始化
//注:java不區分變數的宣告和定義
//2.常量:利用關鍵字 final(final表示這個變數只能被賦值一次,一旦賦值,就不能夠再更改了,常用大寫)指示常量
final int MAX=100;
//注:在java中,如果希望一個常量在該類中被多個方法使用,通常將這些常量稱為類常量,使用 static final s設定一個類常量
//3.數學常量
System.out.println(Math.PI);
System.out.println(Math.E);
//4.強制型別轉換(通過截斷小數部分將浮點值轉換為整型)
double x=9.993468;
int sx=(int)x;
System.out.println(sx);
//對浮點數進行舍入運算
int sx2=(int)Math.round(x); //返回最接近引數的long
System.out.println(sx2);
//5.移位運算子
System.out.println(0b111>>2);
System.out.println(0b0011<<2);
System.out.println(0b1101>>>2);
//6.列舉型別:列舉型別的變數只儲存這個型別宣告中給定的某個列舉值或者 null
enum Size{SMALL,MEDIUM,LARCE,EXTRA};
Size s=Size.MEDIUM;
System.out.println(s);
/*7.字串:java字串就是Unicode字元序列。
各種字串存放在公共的字串池中,字串變數指向字串池中相應的位置
String類(不可變字串):不能修改字串,但可以使字串變數引用另一個字串
*/
//7.1 提取一個子串:第一個引數是起始位置(含該位置字元),第二個引數是結束位置(不含該位置字元)
String str1="Hello Java!";
String s1=str1.substring(4,7);
System.out.println(s1+",長度為"+(7-4));
//7.2 拼接字串(+):當一個字串與一個非字串的值進行拼接時,後者轉換為字串
String str2="This is my first java program.";
String str3=str1+str2;
System.out.println(str3);
//把多個字串放在一起,用一個定界符分隔
String all=String.join("/","S","M","L","XL");
System.out.println(all);
//7.3 判斷字串是否相等:相等返回true,否則返回false
System.out.println(str1.equals("Hello Java!"));
//忽略大小寫
System.out.println(str1.equalsIgnoreCase("hello java!"));
//注:String型別的字串常量是共享的,而+和substring等操作產生的結果(建立了一個新字串)並不是共享的。
/*7.4 空串:空串""是長度為0的字串
null串:表示目前沒有任何物件與該變數關聯
*/
String str=null;
if(str==null) //判斷一個字串是否為null
System.out.println("str是null");
str="";
if(str.length()==0) //判斷一個串是否為空
System.out.println("str是空串");
}
}
char型別和Unicode
java的char型別是UTF-16編碼的一個程式碼單元,即2個位元組。大多數常用字元一個程式碼單元就可以表示,而一些輔助字元需要一對程式碼單元表示 java使用的字符集是Unicode字符集。在Unicode字符集中,與某個字元對應的程式碼值被稱作碼點, UTF-16編碼採用不同長度的編碼表示所有的Unicode碼點,在基本的多語言級別中,每個字元用16位表示,被稱為程式碼單元,而一些輔助字符采用一對相鄰的程式碼單元(即4個位元組)進行編碼
class FirstDemo
{
public static void main(String[] args)
{
String str1="Hello java!";
//length()方法:該方法返回的是給定字串所需要的程式碼單元數量,但大多數常用的Unicode字元一個程式碼單元就可以表示。所以在這種情況下可以看成是字串的長度
System.out.println(str1.length());
//codePointCount()方法:該方法返回給定字串的碼點數量,即字元個數
System.out.println(str1.codePointCount(0,str1.length()));
//charAt(n)方法:返回位置n的程式碼單元
System.out.println(str1.charAt(4));
//得到第i個碼點
int p=str1.offsetByCodePoints(0,4); //返回此String中從給定的index處偏移codePointOffset個程式碼點的索引。
int cp=str1.codePointAt(p); //返回指定索引處的字元(Unicode程式碼點)。
//遍歷一個字串,並且依次檢視每一個碼點
/*int i=0;
int cp=str1.codePointAp(i);
if(str1.isSupplementaryCodePoint(cp)) //確定指定字元(Unicode 程式碼點)是否在增補字元範圍內。
i+=2;
else
i++;
*/
int[] cods=str1.codePoints().toArray(); //將字串生成一個int型的流,,每個int值對應一個碼點
String str=new String(cods,0,cods.length); //將碼點陣列轉化成一個字串
}
}