從零開始學習java(一)java基礎文法

來源:互聯網
上載者:User

標籤:alt   switch   運行   代碼   byte   int   span   學習java   type   

  從公司裸辭一個月,原本工作是做VB的,現在想從事java;在找工作的時候總是要什麼項目經驗,多少有些不爽,所有語言都有共

通性,我就不信java有這麼難?給自己點時間來學習。堅持一個月自學,看看自己的努力究竟有多少用。本次學習從《java核心技術》

第9版開始,每讀一章寫一篇文章,如果寫的不對的話,請指教。

  “像Java這種功能強大的語言大都不太容易學習”,第一章開頭就給我一個下馬威。。。你既然這樣說,那我就學給你看!第一二章是

講java的概念,發展的,就此跳過!

     1.helloworld 

  按照慣例,學習一門語言的開始,簡單的程式:hellowrold;java 類名以大寫開頭,多個字母首字母大寫。class 代表一個類,java的

基礎就是由類組成,類裡麵包含屬性和方法。main方法和C語言的main方法一樣,是每個程式啟動並執行入口;每個類只能有一個class是

public的。另外java代碼每句話結束時以分號結束;

1 public class HelloWorld {2     3     public static void main(String[] args) {4          5         System.out.println("HelloWorld");6     }
View Code

  2.資料類型

  整形:byte:1位元組;(1位元組8個二進位 00000000 ,範圍:-2^8 to 2^8-1 )short:2位元組 int :4位元組; long  8位元組;

     浮點類型:用於表示帶有小數點的類型; float:4位元組 ;double :8 位元組; 

     char類型:char :單字元類型;“A”,"a”等,還有逸出字元“/n”換行等;

  boolean類型: FALSE  true ;真和假!

  3.變數

   在java中每個變數屬於一個類型(type),類型在變數之前。例如 int i;同時每個變數必須賦初始值,不然在用到變數的時候

會報錯;

  4.運算子

  + - * / %:加減乘除取餘數; ++,-- 自增自減運算子;& 與 ,| 或,~非,^異或 ; &&與,||或;

  5.強制類型轉換

  例如double轉int型,後面的小數位可能就沒有了,編譯器就認為有錯;這時候就需要強制轉換了,例如: int i=(int) 4.3;

  6.字串String類

  

  @Test    /**     * 測試String常用方法;String是由數組進行實現的,因此修改一個String相當於建立一個String     */    public void testString(){                String s="hello ";        //判斷是否為空白        System.out.println(s.isEmpty());        //判斷字串是否相等        System.out.println(s.equals("Hello"));        //判斷字串是否相等,不區分大小寫        System.out.println(s.equalsIgnoreCase("Hello"));        //返回下標為1的字串        System.out.println(s.charAt(1));        //返回字串的長度        System.out.println(s.length());        //字母全部大寫        System.out.println(s.toUpperCase());        //字母全部小寫        System.out.println(s.toLowerCase());        //去除開頭或者結尾的字串        System.out.println(s.trim());        //截取字串從下標開頭到結尾-1截取        System.out.println(s.substring(1,3));            }

    

  7.控制語句

  

@Test    public void testControl(){        int i = 0 ;                for(;i<10;i++){            System.out.println(i);        }        while(i>0){            System.out.println(i--);        }        if(i>0){            System.out.println("i>0");        }else{            System.out.println("i<=0");        }        switch(i) {            case 0:                System.out.println("switch:"+i);                break;//如果不加break將順序執行下去            case 1:                System.out.println("switch:"+i);                break;            default:                System.out.println(i);                break;        }           }
View Code

 

  8.數組

@Test    public void testArray(){         int a[]={6,4,7,6,3,8,9};                  //foreach迴圈         for(int j:a){             System.out.println(j);         }         Arrays.sort(a);//數組排序,利用的快速排序法         int b[]=Arrays.copyOf(a, a.length);         for(int j:b){              System.out.println("copy"+j);          }    } 
View Code

 

  

從零開始學習java(一)java基礎文法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.