我的java學習日記(4)

Java學習第四節之java認識

一、           數據類型

1、整數: byte (一個字節,八位,範圍是-128127

Short (2個字節,16位範圍是 -65535/265535/2)

Int 4個字節,32位)

Long (8個字節,64)

2、小數: float 4個字節,32位,保留六位小數。也叫單精度)

          double (8個字節,64位,保留13位小數。也叫雙精度)

3、boolean  1/8個字節,佔一位,只有兩個值,即truefalse

4、char  C語言中佔一個字節,八位

         java中佔兩個字節,16

(用J2ME做開發要儘量節約內存的使用)

 

注意問題:1、在java中小數都看成double類型

         2int型小於等於127,都爲byte類型,不同類型的數據有自己不同的範圍

         3、在java中數據類型相互給值時,小到大是自動轉換,大到小是手動轉換,即需要強制轉換

         4、強制轉換的方法:以18.8爲例:第一種,18.8float);第二種,18.8f)。

 

 數據類型轉換中,intfloat轉換,longfloatdouble轉換,均可能有精度損失的轉換。                                                                     

二、運算符

1、算術運算符:+-*/%

2、自增自減運算符:++--

3、關係運算符:> , < , <= , >= (選擇運算符:x<y?x:y)(從右往左運算)

4boolean運算符:truefalse

5、位運算符:& 與,| 或,^異或,~

  另外,>><<運算符是將二進制位右移或左移。當需要建立某些位模式屏蔽某些位時,使用這兩個運算符十分方便。

舉例:int fourBitFromRight = (n&(1<<3))>>3;

最後,>>>運算符將用0填充高位;>>運算符用符號填充高位。沒有<<<運算符。

三、           字符串

1、String char[ ]

2、字符串賦值方式:

String  s1=”ABCD”; (引用,沒有真正的存儲空間)

String  s2=new String (“ABCD”); (創建新的內存空間)

//Java引用理論用的是C語言的指針理論

==用以比較同一內存地址

equals 可比較s1s2

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};

四、           程序練習

  1public class test{

public static void main(String[] args){

float f=11.33f;

System.out.print("f="+f);

 

  }

}

2public 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); 

  }

}

3public 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);

    }

}

4public 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);

    }

}

5public 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));

}

 

}

6public class test6{

 

 public static void main(String[] s){

 

  int i,j;

  for(i=0;i<7;i++){

    for(j=0;j<i;j++)

     

    }

   }  

 }

7public 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);

}

 

}

8public 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);}

  

    }

 }

9public 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("-----------------");

     }

   }

 }

     

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章