Java快速入门

Java快速入门

创建一个工程

首先,我们使用Eclipse(注意就是Eclise,如果没有安装好,先安装好再接着下一个步骤)创建一个新的项目,这将在本教程中使用。
在这里插入图片描述
输入项目名称:BasicJavaTutorial
在这里插入图片描述 项目已创建:
在这里插入图片描述
注:为了能够在除英语工程其他语言可以使用,应该切换到UTF-8编码。
右键单击该项目并选择属性:
在这里插入图片描述
在这里插入图片描述
原始数据类型
JAVA中有八种基本类型

  1. 对于整数有4种类型:byte, short, int, long

  2. 实数类型: float, double

  3. 字符类型: char

  4. 布尔: 返回 true 或 false 值 (true 或 false)
    在这里插入图片描述
    变量
    选中src右键 选择 “New"再选择"Package”:
    在这里插入图片描述
    新建命名包是:com.cactusdi.damo
    在这里插入图片描述
    选择上面创建的包名 com.cactusdi.damo,右击在弹出的菜单中选择 New 中选择 Class。
    输入类的名称:Damo
    在这里插入图片描述
    Damo.java

    	 public static void main(String[] args) {
            // 声明int类型的变量(整数32位)
            int firstNumber;
            // 将值分配给firstNumber
            firstNumber = 10;
            System.out.println("First Number =" + firstNumber);
            // 声明一个32位实数(浮点数),该数字的值为10.2
            float secondNumber = 10.2f;
            System.out.println("Second Number =" + secondNumber);
            // 声明一个64位实数,该数字的值为10.2
            // 字符d最后告诉Java这是double类型,区别于浮动。
            double thirdNumber = 10.2d;
            System.out.println("Third Number =" + thirdNumber);
            // 声明一个角色
            char ch = 'a';
            System.out.println("Char ch= " + ch);}
    

运行类 Damo:
在 Damo类右键选择 “Run As/Java Application”
运行类,在控制台上看到的结果如下:
在这里插入图片描述
控制流
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:

public static void main(String[] args) {   
    int score = 20;
    System.out.println("Your score =" + score);
    if (score < 50) {
        System.out.println("You are not pass");
    }else if (score >= 50 && score < 80) {
        System.out.println("You are pass");
    }else {
        System.out.println("You are pass, good student!");
    }
}

运行 ElseIfExample1 类的结果:
在这里插入图片描述
改变在上面的例子中,变量“score”的值,然后重新运行ElseIfExample1类:

int score = 80;

在这里插入图片描述
常规操作符

  1. 大于号>
  2. 小于号 <
  3. 大于或等于 >=
  4. 小于或等于 <=
  5. 且 &&
  6. 或 ||
  7. 等于一个值==
  8. 不等于一个值 !=
  9. 非 !

创建一个类 ElseIfExample2

  public static void main(String[] args) {
            int age = 20;
            if (age <= 17) {
                System.out.println("You are 17 or younger");
            } else if (age == 18) {
                System.out.println("You are 18 year old");
            }else if (age > 18 && age < 40) {
                System.out.println("You are between 19 and 39");
            } else {
                if (age != 50) {
                    System.out.println("You are not 50 year old");
                }
                if (!(age == 50)) {
                    System.out.println("You are not 50 year old");
                }
                if (age == 60 || age == 70) {
                    System.out.println("You are 60 or 70 year old");
                }
            }
    }

可以修改 “age” 的值,然后重新运行 ElseIfExample2 类,并查看结果。
布尔值
布尔是一种数据类型,它只有两个值true或false。
创建一个类 BooleanExample

public static void main(String[] args) {
    // 声明boolean类型的变量
    boolean value = true;
    // 如果值为true
    if (value == true) {
        System.out.println("It's true");
    } else {
        System.out.println("It's false");
    }
    // 使用布尔值,您也可以编写
    if (value) {
        System.out.println("It's true");
    }else {
        System.out.println("It's false");
    }
}

switch- case -default 语句
这也是类似上面介绍的 if-else 分支语句:

switch( variable_to_test ) {
  casevalue:
   // code_here;
   break;
  casevalue:
   // code_here;
   break;
  default:
   // values_not_caught_above;
}

public static void main(String[] args) {
    // 声明变量年龄
    int age = 20;
    //检查年龄的值
    switch (age) {
    // 年龄= 18岁
    case 18:
        System.out.println("You are 18 year old");
        break;
    // 年龄= 20岁
    case 20:
        System.out.println("You are 20 year old");
        break;
    // 其他
    default:
        System.out.println("You are not 18 or 20 year old");
    }
}

运行类 的结果 :
You are 20 year old
请注意case语句是一个特定的值,不能做下面的操作:

//这是不允许的!!
case(age < 18) :
  // case仅接受特定值,例如
case18:
  // Do something here
  break;

for循环
语法:

for( start_value; end_value; increment_number ) {
  // Code here
}

例子:

public static void main(String[] args) {
        intstep = 1;
        // 声明一个变量值,起始值为0,每次迭代后,值将增加3,当值大于或等于10时,循环将结束
        for(intvalue = 0; value < 10; value = value + 3) {
            System.out.println("Step ="+ step + "  value = "+ value);
            // 增加1
            step = step + 1;
        }
    }

运行结果:
Step =1 value = 0
Step =2 value = 3
Step =3 value = 6
Step =4 value = 9
另一实例,从1至100的数字求和:

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
while循环
这是 while 循环结构:

//当 条件为真,do something
while( 条件为真 ) {
  // Do something here.
}

例如:

public static void main(String[] args)  {
        int value = 3;
        // 当值小于10时,循环
        while( value < 10)  {
            System.out.println("Value = "+ value);
            //通过+2来增加值
            value = value + 2;
        }
    }

do-while循环
下面是do-while循环的结构:

//do-while循环至少一轮; 如果条件为真,继续循环
do{
  // Do something here.
}while( 条件 );

示例:

public static void main(String[] args) {
        int value = 3;
        //do-while循环至少执行一次
        do {
            System.out.println("Value = " + value);
            // +3
            value = value + 3;
        } while (value < 10);
    }

结果:
Value = 3
Value = 6
Value = 9

Java数组

数组是元素存储在彼此相邻列表。
在Java中声明数组。

//声明一个数组,而不是指定数量的元素
int[] array1;
//使用100个元素初始化数组, 该元素尚未分配特定值
array1 = newint[100];
// 声明一个数组指定元素的数量,该元素尚未分配特定值
double[] array2 = newdouble[10];
// 声明一个数组,其元素被赋予特定的值,这个数组有4个元素
long[] array3= {10L, 23L, 30L, 11L};

示例:

public static void main(String[] args) {
        // 声明一个包含5个元素的数组
        int[] myArray = new int[5];
        // 注意:数组索引的第一个元素是0:
        //将值分配给第一个元素(索引0)
        myArray[0] = 10;
        // 将值分配给第二个元素(索引1)
        myArray[1] = 14;
        //以此类推
        myArray[2] = 36;
        myArray[3] = 27;
        //第5个元素的值(数组中的最后一个元素)
        myArray[4] = 18;
        // 打印元素计数
        System.out.println("Array Length=" + myArray.length);
        //索引3打印到控制台(数组中的第4个元素)
        System.out.println("myArray[3]=" + myArray[3]);
        //使用for循环打印出数组中的元素
        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
类, 继承, 构造器
当我们讨论树,它是抽象的东西,它是一个类。但是,当我们指出了一个特定的树(比如:槟榔树),很明显,那就是实例。
或者,当我们谈论的人,这是抽象的,它是一个类。但是,当指向你或我,这是两种不同的情况下,都是同一个 Person 类。
Person.java

  public class Person {
        // “名称”字段
        public String name;
        // 这是一个有一个参数的构造函数,用它来初始化对象(创建新对象)
        // 构造函数始终与类具有相同的名称
        public Person(String persionName) {
            //将参数的值分配到“名称”字段中
            this.name = persionName;
        }
        // 此方法返回一个String
        public String getName() {
            return this.name;
        }
    }

Person类没有任何main函数。 TestPerson类通过构造函数初始化Person对象实例
PersonTest.java

public class PersonTest {
       public static void main(String[] args) {
           // 创建Person类的对象
           // 通过类Person的构造函数初始化此对象
           // 具体来说Edison
           Person edison = new Person("Edison");
           // Class Person的方法是getName()
           // 使用该对象调用getName():
           String name = edison.getName();
           System.out.println("Person 1: " + name);
           //创建Person类的对象
           // 通过类Person的构造函数初始化此对象
           //  具体来说Bill Gates
           Person billGate = new Person("Bill Gates");
           // 班级人员有字段名称(public)
           // 使用对象来引用它
           String name2 = billGate.name;
           System.out.println("Person 2: " + name2);
       }
    }

运行示例的结果如下:
Person 1: Edison
Person 2: Bill Gates
字段
一般字段

public String myValue;

静态字段

  public static int MY_STATIC_FIELD = 100;

final字段, static final 字段

    // final字段不允许分配新值
    public final int myValue = 100;
    // 静态final字段.不允许分配新值
    public static final long MY_LONG_VALUE = 1234L;

方法
方法 静态方法 final 方法 (将在类的继承中说明)
MethodSample.java

public class MethodSample {
    public String text = "Some text";
    // 默认构造函数
    public MethodSample()  {
    }
    // 此方法返回一个String,无参
    public String getText() {
        return this.text;
    }
    // 这是一个带有一个参数String的方法;此方法返回void(不返回任何内容
    public void setText(String text) {
        //this.text对文本字段的引用,区分文本参数
        this.text = text;
    }
    // 静态方法
    public static int sum(int a, int b, int c) {
        int d =  a + b + c;
        return d;
    }
 }

MethodSampleTest.java

public class MethodSampleTest {
	public static void main(String[] args) {
    // 创建MethodSample的实例
    MethodSample obj = new MethodSample();
    // 调用getText()方法
    String text = obj.getText();
    System.out.println("Text = " + text);
    // 调用方法setText(String)
    obj.setText("New Text");
    System.out.println("Text = " + obj.getText());
    // 可以通过类调用静态方法,建议采用这种方式(**)
    int sum = MethodSample.sum(10, 20, 30);
    System.out.println("Sum  10,20,30= " + sum);
    // 或者通过对象调用,不推荐这种方式(**)
    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
在Java中的继承
Java允许从其他类扩展类。类扩展另一个类称为子类。 子类必须有继承父类中的字段和方法的能力。
Animal.java

public class Animal {
    public Animal() {
    }
    public void move() {
        System.out.println("Move ...!");
    }
    public void say() {
        System.out.println("<nothing>");
    }
}

Cat.java

public class Cat extends Animal {
    // 覆盖Animal类的方法.
    public void say() {
        System.out.println("I am Cat");
    }
}

Dog.java

public class Dog extends Animal {
    // 覆盖Animal类的方法
    public void say() {
        System.out.println("I am Dog");
    }
}

Ant.java

public class Ant extends Animal {
}

AnimalTest.java

public class AnimalTest {
    public static void main(String[] args) {
        // 声明Cat对象
        Cat cat = new Cat();
        // 检查Animal的'cat'实例,结果为true
        boolean isAnimal = cat instanceof Animal;
        System.out.println("cat instanceof Animal?"+ isAnimal);
        // 调用Cat的方法say()
        cat.say();
        // 声明一个对象Animal,通过Cat的构造函数初始化对象
        Animal cat2 = new Cat();
        // 调用Cat的方法say()
        cat2.say();
        // 创建对象Animal,通过Ant类的构造函数
        Animal ant = new Ant();
        // Ant没有say()方法
        // 它调用从父类继承的say()方法(Animal)        
        ant.say();
    }
}

运行示例的结果如下:
cat instanceof Animal?true
I am Cat
I am Cat

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值