是的,Java 和其他编程语言可以结合使用,例如 C、Python 等。下面是一些示例代码:
1. Java 调用 C 函数
Java 中可以使用 JNI(Java Native Interface)调用 C 函数,示例代码如下:
```java
public class TestJNI {
static {
System.loadLibrary("test"); // 加载 C 库
}
public native int add(int a, int b); // 声明 native 方法
public static void main(String[] args) {
TestJNI test = new TestJNI();
int result = test.add(1, 2);
System.out.println("result = " + result);
}
}
```
C 代码:
```c
#include
JNIEXPORT jint JNICALL Java_TestJNI_add(JNIEnv *env, jobject obj, jint a, jint b) {
return a + b;
}
```
2. Python 调用 Java 类
Python 可以使用 Jython 模块来调用 Java 类,示例代码如下:
```python
from java.util import ArrayList
list = ArrayList()
list.add("hello")
list.add("world")
print(list)
```
这段代码创建了一个 Java 的 ArrayList 对象,并向其中添加了两个字符串元素。最后输出结果为:
```
[hello, world]
```
需要注意的是,Jython 模块只能在 Python 2.x 版本中使用,不支持 Python 3.x。