天天看點

Java基礎知識

<b>一、           </b><b>java</b><b>的資料類型</b><b></b>

java的資料類型分為基本資料類型和引用資料類型.

1.      基本資料類型:

(1)、數值型:包括整數類型和浮點類型。其中整數類型有byte,short,int,long。浮點資料類型有float和double。

(2)、字元型:char

(3)、布爾型:boolean

2.引用資料類型:

(1)、類:class

(2)、接口:interface

(3)、數組

<b>二、基本資料類型的預設值</b><b></b>

1.byte:(byte)0

2.shor:(short)0

3.int:0

4.long:0

5.floa:0.0f

6.double:0.0d

7.char:\n0000(空,’’)

8.boolean:false

以上資料類型預設值比較好了解的,如果是字元,預設是’’,如果是數字預設是0,如果是boolean,預設是false。

在主方法中,變量的預設值是無效的。

<b>三、           </b><b>java</b><b>的正規表達式</b><b></b>

1.      在java中字元串如果要判斷是否存在某個待驗證的字元,可以使用string.matches(“\\d+”)來确認是否比對正規表達式。string的split方法,replace方法都支援正規表達式,可以直接使用。比較友善。也比較常用。

2.      使用pattern類:

該類位于<b>java.util.regex.pattern,</b>該類沒有構造方法。使用靜态方法compile(string regex)來建立對象。這一點非常類似于python中的正則。代碼示例:

<b>import</b> java.util.regex.pattern;

<b>public</b> <b>class</b> patterndemotest {

   <b>public</b> <b>static</b> <b>void</b> main(string[] args) {

     // <b>todo</b> auto-generated method stub

     string str="guoxu|hi|world";

     string regex="\\|";

     pattern pat=pattern.compile(regex);

     string s[]=pat.split(str);

     <b>for</b>(<b>int</b> i=0;i&lt;s.length;i++){

        system.out.println(s[i]);

     }

   }

}3.    matcher類主要用于驗證及替換操作:此類的執行個體化需要依靠pattern類的matchers方法

public matcher matcher(charsequence input)

在matcher類中,以下兩個方法最為常用:

驗證:public boolean <b>matches</b>()

替換:public string replaceall(string replacement)

matches方法示例代碼如下:

<b>import</b> java.util.regex.matcher;

<b>public</b> <b>class</b> matcherdemo {

   <b>public</b> <b>static</b> <b>void</b> main(string args[]){

     pattern pat=pattern.compile("\\d+");

     string str="12345f600";

     matcher match=pat.matcher(str);

     <b>if</b>(match.matches()){

        system.out.println("ok");

     }<b>else</b>{

        system.out.println("no");

}

replaceall(str)方法示例:

pattern pat=pattern.compile("\\d+");

     str=match.replaceall("hi");

     system.out.println(str);

<b>四、             </b><b>java</b><b>的數組</b>

數組的申明可以有以下兩種寫法

資料類型[]名稱=null;

資料類型名稱[]=null;

可以有兩種申明方式:

string[] str=new string[size];

string[] str={“hi”,”home”};