四种编程语言常用函数对比表
基础输入输出函数
| 功能 | Java | Python | C# | C++ | 
|---|---|---|---|---|
| 控制台输出 | System.out.println() | print() | Console.WriteLine() | cout << | 
| 控制台输入 | Scanner.nextLine() | input() | Console.ReadLine() | cin >> | 
| 格式化输出 | String.format() | f-string | String.Format() | printf() | 
代码示例:
            
            
              java
              
              
            
          
          // Java
System.out.println("Hello");
String input = scanner.nextLine();
String formatted = String.format("Value: %d", 10);
            
            
              python
              
              
            
          
          # Python
print("Hello")
input_str = input("Enter: ")
formatted = f"Value: {10}"
            
            
              csharp
              
              
            
          
          // C#
Console.WriteLine("Hello");
string input = Console.ReadLine();
string formatted = String.Format("Value: {0}", 10);
            
            
              cpp
              
              
            
          
          // C++
cout << "Hello" << endl;
string input;
cin >> input;
printf("Value: %d\n", 10);字符串操作函数
| 功能 | Java | Python | C# | C++ | 
|---|---|---|---|---|
| 字符串长度 | str.length() | len(str) | str.Length | str.length() | 
| 拼接 | str1 + str2 | str1 + str2 | String.Concat() | str1 + str2 | 
| 子字符串 | str.substring() | str[start:end] | str.Substring() | str.substr() | 
| 查找 | str.indexOf() | str.find() | str.IndexOf() | str.find() | 
| 分割 | str.split() | str.split() | str.Split() | 需要手动处理 | 
| 大小写转换 | str.toUpperCase() | str.upper() | str.ToUpper() | 需要手动处理 | 
代码示例:
            
            
              java
              
              
            
          
          // Java
String str = "Hello";
int len = str.length();
String sub = str.substring(1, 3);
int pos = str.indexOf("e");
            
            
              python
              
              
            
          
          # Python
str = "Hello"
len_str = len(str)
sub = str[1:3]
pos = str.find("e")
            
            
              csharp
              
              
            
          
          // C#
string str = "Hello";
int len = str.Length;
string sub = str.Substring(1, 2);
int pos = str.IndexOf("e");
            
            
              cpp
              
              
            
          
          // C++
string str = "Hello";
size_t len = str.length();
string sub = str.substr(1, 2);
size_t pos = str.find("e");数组/集合操作
| 功能 | Java | Python | C# | C++ | 
|---|---|---|---|---|
| 创建数组 | new int[5] | [1,2,3] | new int[5] | int arr[5] | 
| 列表长度 | list.size() | len(list) | list.Count | vector.size() | 
| 添加元素 | list.add() | list.append() | list.Add() | vector.push_back() | 
| 排序 | Collections.sort() | list.sort() | List.Sort() | sort() | 
| 查找 | list.contains() | element in list | list.Contains() | find() | 
代码示例:
            
            
              java
              
              
            
          
          // Java
List<Integer> list = new ArrayList<>();
list.add(1);
int size = list.size();
Collections.sort(list);
            
            
              python
              
              
            
          
          # Python
list = [1, 2, 3]
list.append(4)
size = len(list)
list.sort()
            
            
              csharp
              
              
            
          
          // C#
List<int> list = new List<int>();
list.Add(1);
int size = list.Count;
list.Sort();
            
            
              cpp
              
              
            
          
          // C++
vector<int> vec;
vec.push_back(1);
size_t size = vec.size();
sort(vec.begin(), vec.end());数学函数
| 功能 | Java | Python | C# | C++ | 
|---|---|---|---|---|
| 绝对值 | Math.abs() | abs() | Math.Abs() | abs() | 
| 平方根 | Math.sqrt() | math.sqrt() | Math.Sqrt() | sqrt() | 
| 幂运算 | Math.pow() | pow() | Math.Pow() | pow() | 
| 四舍五入 | Math.round() | round() | Math.Round() | round() | 
| 最大值 | Math.max() | max() | Math.Max() | max() | 
| 随机数 | Math.random() | random.random() | Random.Next() | rand() | 
代码示例:
            
            
              java
              
              
            
          
          // Java
double abs = Math.abs(-5.5);
double sqrt = Math.sqrt(16);
double power = Math.pow(2, 3);
            
            
              python
              
              
            
          
          # Python
import math
abs_val = abs(-5.5)
sqrt_val = math.sqrt(16)
power_val = pow(2, 3)
            
            
              csharp
              
              
            
          
          // C#
double abs = Math.Abs(-5.5);
double sqrt = Math.Sqrt(16);
double power = Math.Pow(2, 3);
            
            
              cpp
              
              
            
          
          // C++
#include <cmath>
double abs_val = abs(-5.5);
double sqrt_val = sqrt(16);
double power_val = pow(2, 3);文件操作函数
| 功能 | Java | Python | C# | C++ | 
|---|---|---|---|---|
| 读取文件 | Files.readAllLines() | open().read() | File.ReadAllText() | ifstream | 
| 写入文件 | Files.write() | open().write() | File.WriteAllText() | ofstream | 
| 检查存在 | Files.exists() | os.path.exists() | File.Exists() | filesystem::exists() | 
| 删除文件 | Files.delete() | os.remove() | File.Delete() | remove() | 
代码示例:
            
            
              java
              
              
            
          
          // Java
List<String> lines = Files.readAllLines(Paths.get("file.txt"));
Files.write(Paths.get("output.txt"), lines);
            
            
              python
              
              
            
          
          # Python
with open("file.txt", "r") as f:
    content = f.read()
with open("output.txt", "w") as f:
    f.write("Hello")
            
            
              csharp
              
              
            
          
          // C#
string content = File.ReadAllText("file.txt");
File.WriteAllText("output.txt", "Hello");
            
            
              cpp
              
              
            
          
          // C++
ifstream input("file.txt");
string line;
getline(input, line);
ofstream output("output.txt");
output << "Hello" << endl;日期时间函数
| 功能 | Java | Python | C# | C++ | 
|---|---|---|---|---|
| 当前时间 | LocalDateTime.now() | datetime.now() | DateTime.Now | chrono::system_clock | 
| 格式化日期 | DateTimeFormatter | strftime() | ToString() | strftime() | 
| 时间差 | Duration.between() | timedelta | TimeSpan | chrono::duration | 
代码示例:
            
            
              java
              
              
            
          
          // Java
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formatted = now.format(formatter);
            
            
              python
              
              
            
          
          # Python
from datetime import datetime
now = datetime.now()
formatted = now.strftime("%Y-%m-%d")
            
            
              csharp
              
              
            
          
          // C#
DateTime now = DateTime.Now;
string formatted = now.ToString("yyyy-MM-dd");
TimeSpan diff = end - start;
            
            
              cpp
              
              
            
          
          // C++
#include <chrono>
auto now = chrono::system_clock::now();
time_t time = chrono::system_clock::to_time_t(now);异常处理
| 功能 | Java | Python | C# | C++ | 
|---|---|---|---|---|
| 抛出异常 | throw new Exception() | raise Exception() | throw new Exception() | throw exception() | 
| 捕获异常 | try-catch | try-except | try-catch | try-catch | 
| 最终操作 | finally | finally | finally | 无直接对应 | 
代码示例:
            
            
              java
              
              
            
          
          // Java
try {
    int result = 10 / 0;
} catch (ArithmeticException e) {
    System.out.println("Error: " + e.getMessage());
} finally {
    System.out.println("Cleanup");
}
            
            
              python
              
              
            
          
          # Python
try:
    result = 10 / 0
except ZeroDivisionError as e:
    print(f"Error: {e}")
finally:
    print("Cleanup")
            
            
              csharp
              
              
            
          
          // C#
try {
    int result = 10 / 0;
} catch (DivideByZeroException e) {
    Console.WriteLine($"Error: {e.Message}");
} finally {
    Console.WriteLine("Cleanup");
}
            
            
              cpp
              
              
            
          
          // C++
try {
    int result = 10 / 0;
} catch (const exception& e) {
    cout << "Error: " << e.what() << endl;
}类型转换函数
| 功能 | Java | Python | C# | C++ | 
|---|---|---|---|---|
| 字符串转数字 | Integer.parseInt() | int() | int.Parse() | stoi() | 
| 数字转字符串 | String.valueOf() | str() | ToString() | to_string() | 
| 类型检查 | instanceof | isinstance() | is | typeid() | 
代码示例:
            
            
              java
              
              
            
          
          // Java
int num = Integer.parseInt("123");
String str = String.valueOf(123);
boolean check = obj instanceof String;
            
            
              python
              
              
            
          
          # Python
num = int("123")
str_val = str(123)
check = isinstance(obj, str)
            
            
              csharp
              
              
            
          
          // C#
int num = int.Parse("123");
string str = 123.ToString();
bool check = obj is string;
            
            
              cpp
              
              
            
          
          // C++
int num = stoi("123");
string str = to_string(123);
bool check = typeid(obj) == typeid(string);关键差异总结
| 特性 | Java | Python | C# | C++ | 
|---|---|---|---|---|
| 语法风格 | verbose | 简洁 | 类似Java | 灵活但复杂 | 
| 内存管理 | 自动GC | 自动GC | 自动GC | 手动/RAII | 
| 性能 | 较高 | 较低 | 较高 | 最高 | 
| 学习曲线 | 中等 | 简单 | 中等 | 陡峭 | 
| 主要用途 | 企业应用 | 数据科学/Web | Windows应用 | 系统/游戏 | 
这个对比表展示了四种语言在常用功能上的语法差异,帮助开发者在不同语言间切换时快速找到对应的函数和方法。