1. 变量
赋值数据类型 2. 符号
计算运算符比较运算符代码符注释文本符 3. if
一行if一次判断多次判断 4. for5. while6. 数组7. 程序结构8. 输入输出
输出输入 9. 异常捕获
1. 变量 赋值| 项目 | Java | Python | Javascript | VBA |
|---|---|---|---|---|
| 必须先声明 | 是 | 否 | 否 | 否 |
| 声明 | int x; | 无 | 无 | dim x% |
| 赋值 | x=1; | x=1 | x=1 | x=1 |
| 声明并赋值 | int x=1; | x=1 | x=1 | 无 |
| 空 | null | None | null undefined | Null |
| 项目 | Java | Python | Javascript | VBA |
|---|---|---|---|---|
| 整数 | int x=1; | x=1 | x=1 | x=1 |
| 字符 | char a='A'; | 无 | 无 | 无 |
| 字符串 | String a="A"; | a="A" a='A' | a="A" a='A' | a="A" |
| 小数 | float f=3.14f; double d=1.7d | f=3.14 | f=3.14 | f=3.14 |
| 布尔 | boolean b=true; | b=True | b=true | b=True |
| 常量 | final double PI=3.14; | PI=3.14 | const PI=3.14 | Const PI=3.14 |
| 对象 | StringBuilder sb = new StringBuilder(); var sb = new StringBuilder(); | sb = ShaBi() | sb = new Shabi() | x = CreateObject("scripting.Dictionary") |
| 类型转换 | 只允许向上转换 | 允许 | 允许 | 允许 |
2. 符号 计算运算符
| 运算符 | Java | Python | Javascript | VBA |
|---|---|---|---|---|
| 加 | + | + | + | + |
| 减 | - | - | - | - |
| 乘 | * | * | * | * |
| 除 | / | / | / | / |
| 求余 | % | % | % | mod |
| 次幂 | 无 | 3**2 | 3**2 | 无 |
| 自增 | ++ | ++ | 无 | 无 |
| 自减 | -- | -- | 无 | 无 |
| 叠加 | += | += | += | 无 |
| 叠减 | -= | -= | -= | 无 |
| 叠乘 | *= | *= | *= | 无 |
| 叠除 | /= | /= | /= | 无 |
| 括号 | () | () | () | () |
| 字符串连接 | + | + | + | + |
| 运算符 | Java | Python | Javascript | VBA |
|---|---|---|---|---|
| 大于 | > | > | > | > |
| 大于等于 | >= | >= | >= | >= |
| 小于 | < | < | < | < |
| 小于等于 | <= | <= | <= | <= |
| 等于 | == | == | == | == |
| 不等于 | != | != | != | != |
| and | && | and | && | and |
| or | || | or | || | or |
| not | ! | not | ! | not |
| 符号 | Java | Python | Javascript | VBA |
|---|---|---|---|---|
| 转义符 | “” | |||
| 换行符 | ; | : | ; | : |
| 换行符是否可省略 | 不可省略 | 大部分可省略 | 大部分可省略 | 可 |
| 符号 | Java | Python | Javascript | VBA |
|---|---|---|---|---|
| 单行注释 | // | # | // | ’ |
| 多行注释 | “”"…""" ’’’…’’’ | 无 |
| 符号 | Java | Python | Javascript | VBA |
|---|---|---|---|---|
| 单行字符 | ’ | " ’ | " ’ | " |
| 单行字符串 | " | " ’ | " ’ | " |
| 多行字符串 | “”"…""" | “”"…""" ’’’…’’’ | 无 | 无 |
3. if 一行if
// Java x = a > b ? c : d;
# Python x = c if a > b else d
// Javascript x = a > b ? c : d
' VBA if a > b Then x = c Else x = d一次判断
// Java
if (a > b) {
x = c;
} else {
x = d;
}
# Python if a > b: x = c else: x = d
// Javascript
if (a > b) {
x = c
} else {
x = d
}
' VBA If a > b Then x = c Else x = d End If多次判断
// Java
if (a > b) {
x = c;
} else if (a > bb) {
x = cc;
} else {
x = d;
}
# Python if a > b: x = c elif a > bb: x = cc else: x = d
// Javascript
if (a > b) {
x = c
} else if (a > bb) {
x = cc
} else {
x = d
}
' VBA If a > b Then x = c ElseIf a > bb Then x = cc Else x = d End If
4. for
下标循环
// Java
for (int i=0;i<100;i++) {
System.out.println(i);
}
# Python for i in range(100): print(i)
// Javascript
for (var i=0;i<100;i++) {
console.log(i)
}
' VBA For i = 1 to 100 step 1 Debug.Print i next
数组遍历循环
// Java
for (int a:arr) {
System.out.print(a);
}
# Python for a in arr: print(a)
// Javascript
for (a in arr) {
console.log(a)
}
' VBA For Each a in arr Debug.Print a Next
| 项目 | Java | Python | Javascript | VBA |
|---|---|---|---|---|
| 中断循环 | break | break | break | Exit For |
| 跳过循环 | continue | continue | continue | goto |
// Java
int i;
while (i < 100) {
System.out.println(i);
i++;
}
// java的另一个while
int i;
do {
System.out.println(i);
i++;
} while (i < 99);
# Python i = 0 while True: if i < 100: print(i) else: break
// Javascript
i = 0
while (i < 100) {
console.log(i)
i++
}
' VBA ' 1 i = 0; While i < 100 Debug.Print(i) Wend
' VBA ' 2 i = 0; Do While i < 100 Debug.Print(i) Loop
' VBA ' 3 i = 0; Do Debug.Print(i) Loop While i < 99
' VBA ' 4 i = 0; Do Until i >= 100 Debug.Print(i) Loop
' VBA ' 5 i = 0; Do Debug.Print(i) Loop Until i >= 99
| 项目 | Java | Python | Javascript | VBA |
|---|---|---|---|---|
| 中断循环 | break | break | break | Exit Do |
| 跳过循环 | continue | continue | continue | goto |
| 项目 | Java | Python | Javascript | VBA |
|---|---|---|---|---|
| 定义 | int[] x = {1,2,3,4,5}; | x = [1,2,3,4,5] | x = [1,2,3,4,5] | dim Arr() |
| 符号 | {} | [] {} () | [] | Array() |
| 索引 | x[0]; | x[0] | x[0] | Arr(0) |
| 类型混用 | 不允许 | x=[1,'a'] | x=[1,'a'] | Arr=Array(1,"a") |
| 增 | 不允许 | x.append('b') x.insert(0,'c') | x.push('b') | Redim Preserve Arr(4) Arr(4) = 3 |
| 删 | 不允许 | x.pop(1) del x[1] | x.pop(1) | Redim Arr(1) |
| 改 | x[0] = 6; | x[0] = 6 | x[0] = 6 | Arr(0)=6 |
Java
public class Hello {
public static void main(String[] args) {
// 主程序说明
userFunction usf = new userFunction();
usf.setArg("Hello");
System.out.println(usf.getArg());
}
}
class userFunction {
private String arg;
public void setArg(String arg) {
// 设置
this.arg = arg;
}
public String getArg() {
// 返回
return this.arg;
}
}
Python
'''
文档说明
'''
class userFunction:
def __init__(self):
pass
def setArg(self,arg):
self.arg = arg
def getArg(self):
return self.arg
if __name__ == '__main__':
usf = userFunction()
usf.setArg("Hello")
print(usf.getArg())
Javascript
function userFunction(args) {
x = process(args)
return x
}
VBA
Sub userSub() x = userFunction(args) Debug.Print x End Sub Function userFunction(args) as String userFunction = process(args) End Function
8. 输入输出 输出
| 项目 | Java | Python | Javascript | VBA |
|---|---|---|---|---|
| 输出 | System.out.println System.out.print | console.log | Debug.Print | |
| 格式化输出 | System.out.printf System.out.format | format | 无 | 无 |
| 快速格式化 | 无 | f'{d} is a number' | `${d} is a number` | 无 |
| 项目 | Java | Python | Javascript | VBA |
|---|---|---|---|---|
| 输入 | import java.util.Scanner … Scanner scanner = new Scanner(System.int); String ipt = scanner.nextLine(); | ipt = input('请输入:') | var ipt = prompt('请输入','预设值') | ipt = InputBox("请输入",,"预设值") |
9. 异常捕获
| 项目 | Java | Python | Javascript | VBA |
|---|---|---|---|---|
| 异常捕获 | try {..} catch {...} finally {...} | try: except: finally: | try {..} catch {...} finally {...} | On error goto tag |
根据以往的代码来学新个人觉得会更高效一点
初学Java也有很多不懂,如有错误,请不吝指正



