Java basic

Java程序

先创建以.java后缀结尾的空文件

编译为.class文件

在java虚拟机(java vm)上执行文件

The Java VM

java虚拟机可以在任何不同操作系统中执行

The Java Platform

  1. JVM
  2. Java API

Welcome to Java!

1
2
3
4
5
public class Welcome{
public static void main(String[] args){
System.out.println("Welcome to Java!");
}
}

1

Implementation of a Bicycle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class Bicycle{
int cadence = 0;
int speed = 0;
int gear = 1;
Bicycle(){
cadence = 0;
speed = 0;
gear = 1;
}
void changeCadence(int newValue)
{
cadence = newValue;
}
void changeGear(int newValue)
{
gear = newValue;
}
void speedUp(int increment){
speed = speed + increment;
}
void applyBrakes(int decrement){
speed = speed - decrement;
}
void printStates(){
System.out.println("cadence:" + cadence + "speed:" + speed + "gear:" + gear);
}
}

public class BicycleDemo{
public static void main(String[] args){
Bicycle bike1 = new Bicycle();
Bicycle bike2 = new Bicycle();
bike1.speedUp(10);
bike1.printStates();
bike2.changeCadence(50);
bike2.speedUp(15);
bike2.printStates();
}
}

eclipse

使用eclipse来运行java:

在workspace创建新项目

创建新的类class

编写

运行

1

java基础知识pdf

ps:

声明在方法中的变量在使用时必须要初始化;

  • 成员变量(实例变量,属性)

成员变量:(在类中定义, 访问修饰符 修饰符 type name = value)

什么是成员变量?

成员变量就是类中的属性。当new对象的时候,每个对象都有一份属性。一个对象中的属性就是成员变量。

  • 局部变量(本地变量)

局部变量:(修饰符 type name = value)

什么是局部变量?

方法的形式参数以及在方法中定义的变量。

1
2
3
4
5
6
7
public class Welcome{
int j;
public static void main(String[] args){
int i;
System.out.println("Welcome to Java!");
}
}
Donate? comment?