鍍金池/ 教程/ Java/ Java快速入門
Java for循環(huán)
Java接口
Java是什么?
Java命名約定
java中方法重載和方法重寫(xiě)的區(qū)別
Java運(yùn)算符
Java抽象類
Java快速入門
Java實(shí)例初始化程序塊
Java靜態(tài)綁定和動(dòng)態(tài)綁定
Java do-while循環(huán)
Java對(duì)象克隆
Java Object類
Java聚合
Java繼承
Java this關(guān)鍵字
Java虛擬機(jī)內(nèi)部體系結(jié)構(gòu)
Java構(gòu)造器(構(gòu)造方法)
Eclipse安裝和配置
Java多態(tài)
Java方法重載
Java while循環(huán)
Java教程
Java按值調(diào)用和引用調(diào)用
Java strictfp關(guān)鍵字
Java封裝
Java語(yǔ)言特點(diǎn)
Java數(shù)組
Java instanceof運(yùn)算符
Java包裝類
Java命令行參數(shù)
Java包
Java面向?qū)ο螅∣OP)概念
簡(jiǎn)單Java程序
Java基礎(chǔ)實(shí)例程序
Java對(duì)象和類
Java continue語(yǔ)句
Java抽象類和接口的區(qū)別
C++ VS Java比較
Java if/else語(yǔ)句
Java switch語(yǔ)句
Java歷史
Java變量和數(shù)據(jù)類型
安裝Java環(huán)境(Linux)
Java JDK安裝和配置
Hello Java程序的內(nèi)部細(xì)節(jié)
Java break語(yǔ)句
Java方法重寫(xiě)
Java Unicode編碼系統(tǒng)
怎么樣開(kāi)始學(xué)習(xí)Java編程?
對(duì)象和類之間的區(qū)別
Java訪問(wèn)修飾符
Java super關(guān)鍵字
Java注釋
JDK,JRE和JVM之間的區(qū)別
Java final關(guān)鍵字
Java static關(guān)鍵字

Java快速入門

初學(xué)者快速學(xué)習(xí)Java

1- 介紹

2- 創(chuàng)建Java工程

3- 原始數(shù)據(jù)類型

4- 變量

5- 控制流程

5.1- if-else語(yǔ)句

5.2- 一般操作符

5.3- 布爾值

5.4- switch- case -default語(yǔ)句

5.5- for循環(huán)

5.6- while循環(huán)

5.7- do-while循環(huán)

6- Java數(shù)組

6.1-什么是數(shù)組?

6.2- 使用數(shù)組

7- 類,承,構(gòu)造器

8- 字段

9- 方法

10- Java繼承

1- 介紹

首先,學(xué)習(xí)Java需要什么,先閱這里,這里列出一些開(kāi)發(fā)工具和環(huán)境配置:

2- 創(chuàng)建一個(gè)工程

首先,我們使用Eclipse(注意就是Eclise,如果沒(méi)有安裝好,選安裝好再接著下一個(gè)步驟)創(chuàng)建一個(gè)新的項(xiàng)目,這將在本教程中使用。

輸入項(xiàng)目名稱:
  • BasicJavaTutorial

項(xiàng)目已創(chuàng)建:

注:為了能夠在除英語(yǔ)工程其他語(yǔ)言可以使用,應(yīng)該切換到UTF-8編碼。
右鍵單擊該項(xiàng)目并選擇屬性:


3- 原始數(shù)據(jù)類型

JAVA中有八種基本類型
  • 對(duì)于整數(shù)有4種類型:byte, short, int, long
  • 實(shí)數(shù)類型: float, double
  • 字符類型: char
  • 布爾: 返回 truefalse 值 (true 或 false)
類型 描述 bit 最小值 最大值
byte
8位整數(shù)
8  -128 (-2^7) 127 (2^7-1)
short
16位整數(shù)
16 -32,768 (-2^15) 32,767 (2^15 -1)
int
32位整數(shù)
32 - 2,147,483,648
(-2^31)
2,147,483,647
(2^31 -1)
long
64位整數(shù)
64 -9,223,372,036,854,775,808
(-2^63)
9,223,372,036,854,775,807
(2^63 -1)
float
32位實(shí)數(shù)
32 -3.4028235 x 10^38 3.4028235 x 10^38
double
64位實(shí)數(shù)
64 -1.7976931348623157 x 10^308 1.7976931348623157 x 10^308
boolean
邏輯類型

false true
char
字符
16 '\u0000' (0) '\uffff' (65,535).

4- 變量

右鍵點(diǎn)擊 src 并選擇 "New/Package":

新建命名包是:
  • com.yiibai.tutorial.javabasic.variable

選擇上面創(chuàng)建的包名 com.yiibai.tutorial.javabasic.variable,在彈出的菜單中選擇 New 中選擇 Class。
輸入類的名稱:
  • VariableExample1

創(chuàng)建 VariableExample1 類如下:

  • VariableExample1.java
package com.yiibai.tutorial.javabasic.variable;

public class VariableExample1 {

    public static void main(String[] args) {

        // Declare a variable of type int (integer 32-bit)
        int firstNumber;

        // Assigning values to firstNumber
        firstNumber = 10;

        System.out.println("First Number =" + firstNumber);

        // Declare a 32-bit real number (float)
        // This number is assigned a value of 10.2
        float secondNumber = 10.2f;

        System.out.println("Second Number =" + secondNumber);

        // Declare a 64-bit real numbers
        // This number is assigned a value of 10.2
        // character d at the end to tell with Java this is the type double.
        // Distinguished from a float.
        double thirdNumber = 10.2d;

        System.out.println("Third Number =" + thirdNumber);

        // Declare a character
        char ch = 'a';

        System.out.println("Char ch= " + ch);

    }
}
運(yùn)行類 VariableExample1:
在 VariableExample1 類右鍵單擊選擇 "Run As/Java Application":

運(yùn)行類,在控制臺(tái)上看到的結(jié)果如下:

您也可以一次聲明多個(gè)變量,下例說(shuō)明了這一點(diǎn):
創(chuàng)建一個(gè)新的類 VariableExample2

  • VariableExample2.java
package com.yiibai.tutorial.javabasic.variable;

public class VariableExample2 {

    public static void main(String[] args) {

        // Declare three 64-bit integer (long)
        long firstNumber, secondNumber, thirdNumber;

        // Assign value to firstNumber
        // L at the end to tell java a long type, distinguished from type int.
        firstNumber = 100L;

        // Assign values to secondNumber
        secondNumber = 200L;

        // Assign values to thirdNumber
        thirdNumber = firstNumber + secondNumber;

        System.out.println("First Number = " + firstNumber);
        System.out.println("Second Number = " + secondNumber);
        System.out.println("Third Number = " + thirdNumber);
    }

}
運(yùn)行類 VariableExample2 的結(jié)果 :

5- 控制流

5.1- if-else語(yǔ)句

if-else 語(yǔ)句的結(jié)構(gòu)是:
if(condition1 true)  {
 // Do something here
}elseif(condition2 true) {
 // Do something here
}elseif(condition3 true) {
 // Do something here
}else  { // Other
 // Do something here
}
創(chuàng)建一個(gè)類 ElseIfExample1:

  • ElseIfExample1.java
package com.yiibai.tutorial.javabasic.controlflow;

public class ElseIfExample1 {

    public static void main(String[] args) {

        // Declaring a integer number (int)        
        int score = 20;

        System.out.println("Your score =" + score);

        // If the score is less than 50
        if (score < 50) {
            System.out.println("You are not pass");
        }

        // Else if the score more than or equal to 50 and less than 80.
        else if (score >= 50 && score < 80) {
            System.out.println("You are pass");
        }

        // Remaining cases (that is greater than or equal to 80)
        else {
            System.out.println("You are pass, good student!");
        }

    }
}
運(yùn)行 ElseIfExample1 類的結(jié)果:

改變?cè)谏厦娴睦又?,變量“score”的值,然后重新運(yùn)行ElseIfExample1類:
int score = 80;

5.2- 常規(guī)操作符

  • > 大于號(hào)
  • < 小于號(hào)
  • >= 大于或等于
  • <= 小于或等于
  • && 且
  • || 或
  • == 等一個(gè)值
  • != 不等于一個(gè)值
  • ! 非
創(chuàng)建一個(gè)類 ElseIfExample2
  • ElseIfExample2.java
package com.yiibai.tutorial.javabasic.controlflow;

public class ElseIfExample2 {
    public static void main(String[] args) {

        // Declare a variable int simulate your age.
        int age = 20;

        // Test age less than or equal 17
        if (age <= 17) {
            System.out.println("You are 17 or younger");
        }

        // Test age equals 18
        else if (age == 18) {
            System.out.println("You are 18 year old");
        }

        // Test age, greater than 18 and less than 40
        else if (age > 18 && age < 40) {
            System.out.println("You are between 19 and 39");
        }

        // Remaining cases (Greater than or equal to 40)
        else {
            // Nested if statements
            // Test age not equals 50.
            if (age != 50) {
                System.out.println("You are not 50 year old");
            }

            // Negative statements
            if (!(age == 50)) {
                System.out.println("You are not 50 year old");
            }

            // If age is 60 or 70
            if (age == 60 || age == 70) {
                System.out.println("You are 60 or 70 year old");
            }

        }

    }
}
您可以修改 “age” 的值,然后重新運(yùn)行 ElseIfExample2 類,并查看結(jié)果。

5.3- 布爾值

布爾是一種數(shù)據(jù)類型,它只有兩個(gè)值true或false。
創(chuàng)建一個(gè)類 BooleanExample
  • BooleanExample.java
package com.yiibai.tutorial.javabasic.controlflow;

public class BooleanExample {
    public static void main(String[] args) {

        // Declare a variable of type boolean
        boolean value = true;

        // If value is true
        if (value == true) {
            System.out.println("It's true");
        }
        // Else
        else {
            System.out.println("It's false");
        }

        // With boolean values you can also write
        if (value) {
            System.out.println("It's true");
        }
        // Else
        else {
            System.out.println("It's false");
        }
    }
}

5.4- switch- case -default 語(yǔ)句

這也是類似上面介紹的 if-else 分支語(yǔ)句:
switch( variable_to_test ) {
  casevalue:
   // code_here;
   break;
  casevalue:
   // code_here;
   break;
  default:
   // values_not_caught_above;
}
  • SwitchExample1.java
package com.yiibai.tutorial.javabasic.controlflow;

public class SwitchExample1 {
    public static void main(String[] args) {

        // Declare a variable age
        int age = 20;

        // Check the value of age
        switch (age) {

        // Case age = 18
        case 18:
            System.out.println("You are 18 year old");
            break;

        // Case age = 20
        case 20:
            System.out.println("You are 20 year old");
            break;

        // Remaining cases
        default:
            System.out.println("You are not 18 or 20 year old");
        }

    }
}
運(yùn)行類 SwitchExample1 的結(jié)果 :
You are 20 year old
請(qǐng)注意case語(yǔ)句是一個(gè)特定的值,不能做下面的操作:
// This is not allowed !!
case(age < 18) :
 
// case only accept a specific value eg:
case18:
  // Do something here
  break;
看下面的一個(gè)例子:
  • SwitchExample2.java
package com.yiibai.tutorial.javabasic.controlflow;

public class SwitchExample2 {
    public static void main(String[] args) {

        // Declare a variable age
        int age = 30;

        // Check the value of age
        switch (age) {

        // Case age = 18
        case 18:
            System.out.println("You are 18 year old");

        // Case age in 20, 30, 40
        case 20:
        case 30:
        case 40:
            System.out.println("You are " + age);
            break;

        // Remaining case:
        default:
            System.out.println("Other age");
        }

    }
}
運(yùn)行結(jié)果:
You are 30

5.5- for循環(huán)

語(yǔ)法:
for( start_value; end_value; increment_number ) {
  // Code here
}
考慮如下一個(gè)例子:
  • ForLoopExample1.java
packagecom.yiibai.tutorial.javabasic.loop;
 
publicclass ForLoopExample1 {
 
    publicstaticvoidmain(String[] args) {
 
        // Declare a variable, step in loop
        intstep = 1;
 
        // Declare a variable value with the start value is 0
        // After each iteration, value will increase 3
        // And the loop will end when the value greater than or equal to 10
        for(intvalue = 0; value < 10; value = value + 3) {
 
            System.out.println("Step ="+ step + "  value = "+ value);
 
            // Increase 1
            step = step + 1;
 
        }
 
    }
 
}
運(yùn)行 ForLoopExample1 類結(jié)果:
Step =1  value = 0
Step =2  value = 3
Step =3  value = 6
Step =4  value = 9
另一實(shí)例中,從1至100的數(shù)字求和:
  • ForLoopExample2.java
package com.yiibai.tutorial.javabasic.loop;

public class ForLoopExample2 {
    public static void main(String[] args) {
        int sum = 0; for (int i = 0; i <= 100; i = i + 1) {
            sum = sum + i;
        }
       System.out.println(sum); }
}
結(jié)果:
5050

5.6- while循環(huán)

這是 while 循環(huán)結(jié)構(gòu):
// While the condition is true, then do something.
while( 條件為真 ) {
  // Do something here.
}
參見(jiàn)圖示
  • WhileExample1.java
publicclassWhileExampe1 {
 
     
    publicstaticvoidmain(String[] args)  {
         
        intvalue = 3;
         
        // While the value is less than 10, the loop is working.
        while( value < 10)  {
             
            System.out.println("Value = "+ value);
             
            // Increase value by adding 2
            value = value + 2;
        }
    }
}

5.7- do-while循環(huán)

下面是do-while循環(huán)的結(jié)構(gòu):
// The do-while loop to work at least one round
// and while the condition is true, it also works to
do{
  // Do something here.
}while( condition );
如下圖的示例:
  • DoWhileExample1.java
package com.yiibai.tutorial.javabasic.loop;

public class DoWhileExample1 {

    public static void main(String[] args) {

        int value = 3;

        // do-while loop will execute at least once
        do {

            System.out.println("Value = " + value);

            // Increase 3
            value = value + 3;

        } while (value < 10);

    }
}
結(jié)果:
Value = 3
Value = 6
Value = 9

6- Java數(shù)組

6.1- 什么是數(shù)組?

數(shù)組是元素存儲(chǔ)在彼此相鄰列表。
讓我們來(lái)看看,一個(gè)數(shù)組有5個(gè)int型的元素。

6.2- 使用數(shù)組

如何在Java中聲明數(shù)組。
// Declare an array, not a specified number of elements.
int[] array1;
 
 
// Initialize the array with 100 elements
// The element has not been assigned a specific value
array1 = newint[100];
 
// Declare an array specifies the number of elements
// The element has not been assigned a specific value
double[] array2 = newdouble[10];
 
// Declare an array whose elements are assigned specific values.
// This array with 4 elements
long[] array3= {10L, 23L, 30L, 11L};
讓我們來(lái)看一個(gè)例子:
  • ArrayExample1.java
package com.yiibai.tutorial.javabasic.array;

public class ArrayExample1 {

    public static void main(String[] args) {

        // Declare an array with 5 elements
        int[] myArray = new int[5];

        // Note: the first element of the array index is 0:

        // Assigning values to the first element (index 0)
        myArray[0] = 10;

        // Assigning values to the second element (index 1)
        myArray[1] = 14;

        myArray[2] = 36;
        myArray[3] = 27;

        // Value for the 5th element (the last element in the array)
        myArray[4] = 18;

        // Print out element count.
        System.out.println("Array Length=" + myArray.length);

        // Print to Console element at index 3 (4th element in the array)
        System.out.println("myArray[3]=" + myArray[3]);

        // Use a for loop to print out the elements in the array.
        for (int index = 0; index < myArray.length; index++) {
            System.out.println("Element " + index + " = " + myArray[index]);
        }
    }
}
結(jié)果:
Array Length=5
myArray[3]=27
Element 0 = 10
Element 1 = 14
Element 2 = 36
Element 3 = 27
Element 4 = 18
舉一個(gè)實(shí)例來(lái)說(shuō)明使用一個(gè)for循環(huán)來(lái)對(duì)元素賦值:
  • ArrayExample2.java
package com.yiibai.tutorial.javabasic.array;

public class ArrayExample2 {
    public static void main(String[] args) {

        // Declare an array with 5 elements
        int[] myArray = new int[5];

        // Print out element count
        System.out.println("Array Length=" + myArray.length);

        // Using loop assign values to elements of the array.
        for (int index = 0; index < myArray.length; index++) {
            myArray[index] = 100 * index * index + 3;
        }

        // Print out the element at index 3
        System.out.println("myArray[3] = "+ myArray[3]);
    }
}
輸出結(jié)果:
Array Length=5
myArray[3] = 903

7- 類, 繼承, 構(gòu)造器

有三個(gè)概念需要進(jìn)行區(qū)分:
  • 構(gòu)造
  • 繼承
當(dāng)我們討論樹(shù),它是抽象的東西,它是一個(gè)類。但是,當(dāng)我們指出了一個(gè)特定的樹(shù)(比如:檳榔樹(shù)),很明顯,那就是實(shí)例。
或者,當(dāng)我們談?wù)摰娜?,這是抽象的,它是一個(gè)類。但是,當(dāng)指向你或我,這是兩種不同的情況下,都是同一個(gè) Person 類。
  • Person.java
package com.yiibai.tutorial.javabasic.javastructure;

public class Person {
    // This is field
    // The name of Person
    public String name;

    // This is a Constructor
    // Use it to initialize the object (Create new object)
    // This constructor has one parameter
    // Constructor always have the same name as the class.
    public Person(String persionName) {
        // Assign the value of the parameter into the 'name' field
        this.name = persionName;
    }

    // This method returns a String ..
    public String getName() {
        return this.name;
    }
}
Person類沒(méi)有任何main函數(shù)。 TestPerson類通過(guò)構(gòu)造函數(shù)初始化Person對(duì)象實(shí)例
  • PersonTest.java
package com.yiibai.tutorial.javabasic.javastructure;

public class PersonTest {

       public static void main(String[] args) {

           // Create an object of class Person
           // Initialize this object via constructor of class Person
           // Specifically, Edison
           Person edison = new Person("Edison");

           // Class Person has the method getName()
           // Use the object to call getName():
           String name = edison.getName();
           System.out.println("Person 1: " + name);

           // Create an object of class Person
           // Initialize this object via constructor of class Person
           // Specifically, Bill Gates
           Person billGate = new Person("Bill Gates");

           // Class Person has field name (public)
           // Use objects to refer to it.
           String name2 = billGate.name;
           System.out.println("Person 2: " + name2);

       }

    }
運(yùn)行示例的結(jié)果如下:
Person 1: Edison
Person 2: Bill Gates

8- 字段

在本節(jié)中,我們將討論一些概念:
字段
  • 一般字段
  • 靜態(tài)字段
  • final字段
  • static final 字段
下面看看字段和靜態(tài)字段的例子。
  • FieldSample.java
package com.yiibai.tutorial.javabasic.javastructure;

public class FieldSample {

    // This is static field.
    public static int MY_STATIC_FIELD = 100;

    // This is normal field.
    public String myValue;


    // Constructor
    public FieldSample(String myValue)  {
        this.myValue= myValue;
    }

}
  • FieldSampleTest.java
package com.yiibai.tutorial.javabasic.javastructure;

public class FieldSampleTest {

    public static void main(String[] args) {

        // Create the first object.
        FieldSample obj1 = new FieldSample("Value1");

        System.out.println("obj1.myValue= " + obj1.myValue);

        // Print out static value, access via instance of class (an object).
        System.out.println("obj1.MY_STATIC_FIELD= " + obj1.MY_STATIC_FIELD);

        // Print out static value, access via class.
        System.out.println("FieldSample.MY_STATIC_FIELD= "
                + FieldSample.MY_STATIC_FIELD);

        // Create second object:
        FieldSample obj2 = new FieldSample("Value2");

        System.out.println("obj2.myValue= " + obj2.myValue);

        // Print out static value, access via instance of class (an object)
        System.out.println("obj2.MY_STATIC_FIELD= " + obj2.MY_STATIC_FIELD);

        System.out.println(" ------------- ");

        // Set new value for static field.
        // (Or using: FieldSample.MY_STATIC_FIELD = 200)
        obj1.MY_STATIC_FIELD = 200;

        // It will print out the value 200.
        System.out.println("obj2.MY_STATIC_FIELD= " + obj2.MY_STATIC_FIELD);
    }
}
運(yùn)行示例的結(jié)果:
obj1.myValue= Value1
obj1.MY_STATIC_FIELD= 100
FieldSample.MY_STATIC_FIELD= 100
obj2.myValue= Value2
obj2.MY_STATIC_FIELD= 100
 ------------- 
obj2.MY_STATIC_FIELD= 200
最后一個(gè)字段是不能一個(gè)新值分配給它的,它就像一個(gè)常數(shù)。
  • FinalFieldExample.java
package com.yiibai.tutorial.javabasic.javastructure;

public class FinalFieldExample {

    // A final field.
    // Final Field does not allow to assign new values.
    public final int myValue = 100;

    // A static final field.
    // Final field does not allow to assign new values.
    public static final long MY_LONG_VALUE = 1234L;
}

9- 方法

有關(guān)方法的種類:
  • 方法
  • 靜態(tài)方法
  • final 方法 (將在類的繼承中說(shuō)明)
  • MethodSample.java
package com.yiibai.tutorial.javabasic.javastructure;

public class MethodSample {

    public String text = "Some text";

    // Default Constructor
    public MethodSample()  {

    }

    // This method return a String
    // and has no parameter.
    public String getText() {
        return this.text;
    }

    // This is a method with one parameter String.
    // This method returns void (not return anything)    
    public void setText(String text) {
        // this.text reference to the text field.
        // Distinguish the text parameter.        
        this.text = text;
    }

    // Static method
    public static int sum(int a, int b, int c) {
        int d =  a + b + c;
        return d;
    }
}
  • MethodSampleTest.java
package com.yiibai.tutorial.javabasic.javastructure;

public class MethodSampleTest {

    public static void main(String[] args) {

        // Create instance of MethodSample
        MethodSample obj = new MethodSample();

        // Call getText() method
        String text = obj.getText();

        System.out.println("Text = " + text);

        // Call method setText(String)
        obj.setText("New Text");

        System.out.println("Text = " + obj.getText());

        // Static method can be called through the class.
        // This way is recommended. (**)
        int sum = MethodSample.sum(10, 20, 30);

        System.out.println("Sum  10,20,30= " + sum);

        // Or call through objects
        // This way is not recommended. (**)        
        int sum2 = obj.sum(20, 30, 40);

        System.out.println("Sum  20,30,40= " + sum2);
    }

}
執(zhí)行上面的程序輸出結(jié)果如下:
Text = Some text
Text = New Text
Sum  10,20,30= 60
Sum  20,30,40= 90

10- 在Java中的繼承

Java允許從其他類擴(kuò)展類。類擴(kuò)展另一個(gè)類稱為子類。 子類必須有繼承父類中的字段和方法的能力。
  • Animal.java
package com.yiibai.tutorial.javabasic.inheritance;

public class Animal {

    public Animal() {

    }

    public void move() {
        System.out.println("Move ...!");
    }

    public void say() {
        System.out.println("<nothing>");
    }

}
  • Cat.java
package com.yiibai.tutorial.javabasic.inheritance;

public class Cat extends Animal {

    // Override method o