Java學習第四節之java認識
一、 數據類型
1、整數: byte (一個字節,八位,範圍是-128到127)
Short (2個字節,16位範圍是 -65535/2到65535/2)
Int (4個字節,32位)
Long (8個字節,64位)
2、小數: float (4個字節,32位,保留六位小數。也叫單精度)
double (8個字節,64位,保留13位小數。也叫雙精度)
3、boolean 1/8個字節,佔一位,只有兩個值,即true和false
4、char 在C語言中佔一個字節,八位
在java中佔兩個字節,16位
(用J2ME做開發要儘量節約內存的使用)
注意問題:1、在java中小數都看成double類型
2、int型小於等於127,都爲byte類型,不同類型的數據有自己不同的範圍
3、在java中數據類型相互給值時,小到大是自動轉換,大到小是手動轉換,即需要強制轉換
4、強制轉換的方法:以18.8爲例:第一種,18.8(float);第二種,18.8(f)。
數據類型轉換中,int向float轉換,long向float和double轉換,均可能有精度損失的轉換。
二、運算符
1、算術運算符:+,-,*,/,%
2、自增自減運算符:++,--
3、關係運算符:> , < , <= , >= (選擇運算符:x<y?x:y)(從右往左運算)
4、boolean運算符:true,false
5、位運算符:& 與,| 或,^異或,~非
另外,>>和<<運算符是將二進制位右移或左移。當需要建立某些位模式屏蔽某些位時,使用這兩個運算符十分方便。
舉例:int fourBitFromRight = (n&(1<<3))>>3;
最後,>>>運算符將用0填充高位;>>運算符用符號填充高位。沒有<<<運算符。
三、 字符串
1、String 即char[ ]
2、字符串賦值方式:
String s1=”ABCD”; (引用,沒有真正的存儲空間)
String s2=new String (“ABCD”); (創建新的內存空間)
//Java引用理論用的是C語言的指針理論
==用以比較同一內存地址
equals 可比較s1和s2
3、隨機數
charc=(char)(Math.random()*26+’a’);
4、申請數組長度的方法
int[ ] i;
int[ ]i={123,12,124};
int[ ] i=new int[50];
int[ ] i=newint[ ]{123,14,13};
四、 程序練習
1、public class test{
public static void main(String[] args){
float f=11.33f;
System.out.print("f="+f);
}
}
2、public class test1{
public static void main(String arg[]) {
inti1=10, i2=20,i;
i= i2++;
System.out.print("i=" + i);
System.out.println("i2=" +i2);
i= ++i2;
System.out.print("i=" + i);
System.out.println(" i2=" + i2);
i= --i1;
System.out.print("i=" + i);
System.out.println(" i1=" + i1);
i = i1--;
System.out.print("i="+ i);
System.out.println(" i1=" + i1);
}
}
3、public class test2{
public static void main(String args[]){
boolean a,b,c;
a = true; b = false;
c = a & b;
System.out.println(c);
c = a | b; System.out.println(c);
c = a ^ b; System.out.println(c);
c = !a; System.out.println(c);
c = a && b;System.out.println(c);
c = a || b; System.out.println(c);
}
}
4、public class test3{
public static void main(String args[]) {
int i=1,j=2;
boolean flag1 = (i>3)&&((i+j)>5); //第二個操作數將不再計算
boolean flag2 = (i<2)||((i+j)<6); //第二個操作數將不再計算
System.out.println(flag1);
System.out.println(flag2);
}
}
5、public class test5{
public static void main(String[] s){
String s1="ABCD";
String s2="ABCD";
String s3=new String("ABCD");
System.out.println(s1==s2);
System.out.println(s1==s3);
System.out.println(s1.equals(s3));
}
}
6、public class test6{
public static void main(String[] s){
int i,j;
for(i=0;i<7;i++){
for(j=0;j<i;j++)
}
}
}
7、public class test7{
public static void main(String[] s){
char c=(char)(Math.random()*26+'A');
System.out.println(c);
System.out.println((int)Math.random()*10);
System.out.println((int)(Math.random()*10));
System.out.println((int)(Math.random()*10));
float f=(float)(Math.random()*10);
System.out.println(f);
}
}
8、public class test8{
public static void main(String[] s){
int[] i={12,211,121,152,245,54,25,45,5,24,5,1,214};
//for(int a=0;a<i.length;a++){
//int y=i[a];
//System.out.println(y);}
for(int y:i) {System.out.println(y);}
}
}
9、public class test9{
public static void main(String[] s){
int[][][] a={
{
{15,13,12},
{15,13,21},
{15,13,12}
},
{
{155,13,32},
{155,13,23},
{155,13,32},
},
{
{1555,13,43},
{1555,13,34},
{1555,13,34}
}
} ;
for(int[][] i:a){
for(int[] j:i){
for(int k:j){
System.out.print(k+",");
}
System.out.println();
}
System.out.println("-----------------");
}
}
}