我试图了解java关键字this实际上做了什么.我一直在阅读Sun的文档,但我仍然对this实际操作有些模糊.
String s1 = "BloodParrot is the man";
String s2 = "BloodParrot is the man";
String s3 = new String("BloodParrot is the man");
System.out.println(s1.equals(s2));
System.out.println(s1 == s2);
System.out.println(s1 == s3);
System.out.println(s1.equals(s3));
Run Code Online (Sandbox Code Playgroud)
//输出
真
真
假
真
如果所有三个字符串具有相同的内容,为什么所有字符串都没有在内存中相同的位置?
这就是我要做的事情:我正在从命令行读取文件.该文件包含一个数据列表,下面这段是它的样子.我遇到的问题是if语句.
import java.util.*;
import java.io.*;
public class VehicleTest {
public static void main(String[] args) throws FileNotFoundException {
String vehicle = "vehicle";
String car = "car";
String americanCar = "american car";
String foreignCar = "foreign car";
String truck = "truck";
String bicycle = "bicycle";
File file = new File(args[0]);
Scanner input = new Scanner(file);
String[] autos = new String[100];
ArrayList allVehicles = new ArrayList();
for (int i = 0; i < autos.length; i++) {
autos[i] = input.nextLine();
}
int j = …Run Code Online (Sandbox Code Playgroud) 在java中为什么编译器不允许以下代码?
public class Test {
public static void main(String[] args) {
int x;
int x = 4;// the error is generated here
}
}
Run Code Online (Sandbox Code Playgroud)