Java基础 专题
专题目录
您的位置:java > Java基础专题 > Java快速入门
Java快速入门
作者:--    发布时间:2019-11-20
初学者快速学习java

1- 介绍

2- 创建java工程

3- 原始数据类型

4- 变量

5- 控制流程

5.1- if-else语句

5.2- 一般操作符

5.3- 布尔值

5.4- switch- case -default语句

5.5- for循环

5.6- while循环

5.7- do-while循环

6- java数组

6.1-什么是数组?

6.2- 使用数组

7- 类,承,构造器

8- 字段

9- 方法

10- java继承

1- 介绍

首先,学习java需要什么,先阅这里,这里列出一些开发工具和环境配置:

2- 创建一个工程

首先,我们使用eclipse(注意就是eclise,如果没有安装好,选安装好再接着下一个步骤)创建一个新的项目,这将在本教程中使用。

输入项目名称:
  • basicjavatutorial

项目已创建:

注:为了能够在除英语工程其他语言可以使用,应该切换到utf-8编码。
右键单击该项目并选择属性:


3- 原始数据类型

java中有八种基本类型
  • 对于整数有4种类型:byte, short, int, long
  • 实数类型: float, double
  • 字符类型: char
  • 布尔: 返回 truefalse 值 (true 或 false)
类型 描述 bit 最小值 最大值
byte
8位整数
8  -128 (-2^7) 127 (2^7-1)
short
16位整数
16 -32,768 (-2^15) 32,767 (2^15 -1)
int
32位整数
32 - 2,147,483,648
(-2^31)
2,147,483,647
(2^31 -1)
long
64位整数
64 -9,223,372,036,854,775,808
(-2^63)
9,223,372,036,854,775,807
(2^63 -1)
float
32位实数
32 -3.4028235 x 10^38 3.4028235 x 10^38
double
64位实数
64 -1.7976931348623157 x 10^308 1.7976931348623157 x 10^308
boolean
逻辑类型

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

4- 变量

右键点击 src 并选择 "new/package":

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

选择上面创建的包名 com.h3.tutorial.javabasic.variable,在弹出的菜单中选择 new 中选择 class。
输入类的名称:
  • variableexample1

创建 variableexample1 类如下:

  • variableexample1.java
package com.h3.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);

    }
}
运行类 variableexample1:
在 variableexample1 类右键单击选择 "run as/java application":

运行类,在控制台上看到的结果如下:

您也可以一次声明多个变量,下例说明了这一点:
创建一个新的类 variableexample2

  • variableexample2.java
package com.h3.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);
    }

}
运行类 variableexample2 的结果 :

5- 控制流

5.1- if-else语句

if-else 语句的结构是:
if(condition1 true)  {
 // do something here
}elseif(condition2 true) {
 // do something here
}elseif(condition3 true) {
 // do something here
}else  { // other
 // do something here
}
创建一个类 elseifexample1:

  • elseifexample1.java
package com.h3.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!");
        }

    }
}
运行 elseifexample1 类的结果:

改变在上面的例子中,变量“score”的值,然后重新运行elseifexample1类:
int score = 80;

5.2- 常规操作符

  • > 大于号
  • < 小于号
  • >= 大于或等于
  • <= 小于或等于
  • && 且
  • || 或
  • == 等一个值
  • != 不等于一个值
  • ! 非
创建一个类 elseifexample2
  • elseifexample2.java
package com.h3.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” 的值,然后重新运行 elseifexample2 类,并查看结果。

5.3- 布尔值

布尔是一种数据类型,它只有两个值true或false。
创建一个类 booleanexample
  • booleanexample.java
package com.h3.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 语句

这也是类似上面介绍的 if-else 分支语句:
switch( variable_to_test ) {
  casevalue:
   // code_here;
   break;
  casevalue:
   // code_here;
   break;
  default:
   // values_not_caught_above;
}
  • switchexample1.java
package com.h3.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");
        }

    }
}
运行类 switchexample1 的结果 :
you are 20 year old
请注意case语句是一个特定的值,不能做下面的操作:
// this is not allowed !!
case(age < 18) :
 
// case only accept a specific value eg:
case18:
  // do something here
  break;
看下面的一个例子:
  • switchexample2.java
package com.h3.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");
        }

    }
}
运行结果:
you are 30

5.5- for循环

语法:
for( start_value; end_value; increment_number ) {
  // code here
}
考虑如下一个例子:
  • forloopexample1.java
packagecom.h3.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;
 
        }
 
    }
 
}
运行 forloopexample1 类结果:
step =1  value = 0
step =2  value = 3
step =3  value = 6
step =4  value = 9
另一实例中,从1至100的数字求和:
  • forloopexample2.java
package com.h3.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); }
}
结果:
5050

5.6- while循环

这是 while 循环结构:
// while the condition is true, then do something.
while( 条件为真 ) {
  // do something here.
}
参见图示
  • 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循环

下面是do-while循环的结构:
// 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.h3.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);

    }
}
结果:
value = 3
value = 6
value = 9

6- java数组

6.1- 什么是数组?

数组是元素存储在彼此相邻列表。
让我们来看看,一个数组有5个int型的元素。

6.2- 使用数组

如何在java中声明数组。
// 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};
让我们来看一个例子:
  • arrayexample1.java
package com.h3.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]);
        }
    }
}
结果:
array length=5
myarray[3]=27
element 0 = 10
element 1 = 14
element 2 = 36
element 3 = 27
element 4 = 18
举一个实例来说明使用一个for循环来对元素赋值:
  • arrayexample2.java
package com.h3.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]);
    }
}
输出结果:
array length=5
myarray[3] = 903

7- 类, 继承, 构造器

有三个概念需要进行区分:
  • 构造
  • 继承
当我们讨论树,它是抽象的东西,它是一个类。但是,当我们指出了一个特定的树(比如:槟榔树),很明显,那就是实例。
或者,当我们谈论的人,这是抽象的,它是一个类。但是,当指向你或我,这是两种不同的情况下,都是同一个 person 类。
  • person.java
package com.h3.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类没有任何main函数。 testperson类通过构造函数初始化person对象实例
  • persontest.java
package com.h3.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);

       }

    }
运行示例的结果如下:
person 1: edison
person 2: bill gates

8- 字段

在本节中,我们将讨论一些概念:
字段
  • 一般字段
  • 静态字段
  • final字段
  • static final 字段
下面看看字段和静态字段的例子。
  • fieldsample.java
package com.h3.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.h3.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);
    }
}
运行示例的结果:
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
最后一个字段是不能一个新值分配给它的,它就像一个常数。
  • finalfieldexample.java
package com.h3.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- 方法

有关方法的种类:
  • 方法
  • 静态方法
  • final 方法 (将在类的继承中说明)
  • methodsample.java
package com.h3.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.h3.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);
    }

}
执行上面的程序输出结果如下:
text = some text
text = new text
sum  10,20,30= 60
sum  20,30,40= 90

10- 在java中的继承

java允许从其他类扩展类。类扩展另一个类称为子类。 子类必须有继承父类中的字段和方法的能力。
  • animal.java
package com.h3.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.h3.tutorial.javabasic.inheritance;

public class cat extends animal {

    // override method of the animal class.
    public void say() {
        system.out.println("i am cat");
    }

}
  • dog.java
package com.h3.tutorial.javabasic.inheritance;

public class dog extends animal {

    // override method of the animal class.
    public void say() {
        system.out.println("i am dog");
    }
}
  • ant.java
package com.h3.tutorial.javabasic.inheritance;

public class ant extends animal {

}
  • animaltest.java
package com.h3.tutorial.javabasic.inheritance;

public class animaltest {

    public static void main(string[] args) {

        // declaring a cat object.
        cat cat = new cat();

        // check 'cat' instance of animal.
        // the result is clearly true.
        boolean isanimal = cat instanceof animal;
        system.out.println("cat instanceof animal?"+ isanimal);

        // ==> meo
        // call the method say() of the cat.
        cat.say();


        // declare an object animal
        // initialize the object through the constructor of the cat.
        animal cat2 = new cat();

        // ==> meo
        // call to say() of cat (not animal)
        cat2.say();

        // create the object animal
        // through the constructor of the class ant.        
        animal ant = new ant();

        // ant has no say() method.
        // it call to say() method that inherited from the parent class (animal)        
        ant.say();
    }
}
运行示例的结果如下:
cat instanceof animal?true
i am cat
i am cat
<nothing>


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册