boolean myBoolean;
if (myBoolean = true) {
doSomething;
}
看到没?无论如何,这个块都是真的,因为你将它指定为真...并且IED工具不会报红色的编译错误,这个容易给跪吧。哈哈
==
运算符。Equals检查字符串的内容,而==
操作符检查对象的引用是否相等。==
,但实际上并不依赖于它。使用.equals。
$title(Test.java)
//File name: Test.java
public class CodeGymTest {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
你会得到下一个输出:
Test.java:9: error: class CodeGymTest is public, should be
declared in a file named CodeGymTest.java
public class CodeGymTest
^
1 error
顺便说一句,如果 从类名中删除public修饰符,程序将运行。
main()method (which is static!), when novice programmer attempts
访问实例变量或方法中。如果您尝试这样的代码
public class MyExample {
public String myString;
public static void main(String[] args) {
myString = “non static String”;
}
}
我们有编译错误:
on-static variable number cannot be referenced from a static context
我们该怎么做才能避免这种情况?首先,我们可以将变量设为静态,但它并不总是符合程序目的。其中一个解决方案是创建我们类的对象:
public class MyExample {
public String myString;
public static void main(String[] args) {
MyExample myExample = new myExample();
myString = “non static String”;
}
}
请记住,您可以使用静态或非静态上下文中的静态变量或方法。您可以使用对象引用处理非静态元素。
String[] name = { "Snoopy", "Woodstock", "Charlie Brown" };
for (int i = 0; i <= name.length; i++) {
System.out.println(name[i]);
}
循环从零元素“史努比”开始,然后转到第一个“伍德斯托克”和第二个“查理布朗”......并尝试打印第三个元素,但我们没有它(我们的“第三个”是一个“第二”如“第一”是数字零)。这里的错误非常明显,但并不总是这样。 在下面的块中,您不打印阵列的任何成员。首先,循环以“;”结束,所以没有任何反应。更重要的是:程序无法工作,因为你的int只能在循环内部工作,并以“;”结束。
public static void main(String[] args) {
int [] myArray = {2,5,7};
for (int i=0; i<3; i++);
{
System.out.println(myArray[i]);
}
}
如果您尝试这样的事情:
public static void main(String[] args) {
int [] myArray = {2,5,7};
int i = 0;
for (i=0; i<3; i++);
{
System.out.println(myArray[i]);
}
}
你有java.lang.ArrayIndexOutOfBoundsException,因为System.out.println试图打印元素#3而只有这个,但是我们没有这样的数组成员。最后一个是第二个,它是7。 public static void main(String[] args) {
int [] myArray = {2,5,7};
if (myArray[0] > myArray[1]);
System.out.println("it is");
}
}
这里的条件不满意,但是“它是”将被打印,因为“;”如果构造。
public static void switchCasePrimer() {
int counting = 0;
switch (counting) {
case 0:
System.out.println("0");
case 1:
System.out.println("1");
break;
case 2:
System.out.println("2");
break;
default:
System.out.println("Default");
}
}
这条规则有例外。切换块可以使用返回运算符完成其工作
public class test {
public static void main(String[] args) {
System.out.println(showSwitch(1));
}
public static int showSwitch(int condition) {
switch (condition) {
case 1:
System.out.println(1);
return 1;
case 2:
System.out.println(2);
return 4;
case 3:
System.out.println(3);
return 8;
default:
System.out.println("default");
return 256;
}
}
}
在这种情况下,交换机在案例1之后立即完成其工作。
public class Main {
public static void swap(int i, int j)
{
int temp = i;
i = j;
j = temp;
System.out.println("from swap method i = " + i + ", j = " + j);
}
public static void main(String[] args) {
int i1 = 1;
int j1 = 2;
swap(i1,j1);
System.out.println("from main method i = " + i1 + ", j = " + j1);
}
}
public class Main {
static int[] outer = new int[3];
public static void swap(int[] array) {
System.out.println("(swap) Before swap :" + Arrays.toString(array));
int first = array[0];
int last = array[array.length - 1];
array[0] = last;
array[array.length - 1] = first;
System.out.println("(swap) After swap :" + Arrays.toString(array));
}
public static void main(String[] args) {
outer[0] = 0;
outer[1] = 1;
outer[2] = 2;
swap(outer);
System.out.println("(main) After swap :" + Arrays.toString(outer));
}
public class Test2 {
static String test;
public static void main(String[] args) {
System.out.println(test);
}
}
输出是:
Null
使用本地方法变量时,需要手动初始化它们,否则会出现编译错误。
public class Test2 {
public static void main(String[] args) {
String test2;
System.out.println(test2);
}
}
输出是:
Error:(6, 28) java: variable test2 might not have been initialized
public static void main(String[] args) {
int i = 1;
int j = 2;
int k = i/j;
}
public class Test3 {
static class Parent {
Parent() {
System.out.println("a Parent is created");
}
}
static class Child extends Parent {
Child(){
System.out.println("a Child is created");
}
}
public static void main(String[] args) {
Child child = new Child();
}
}
a Parent is created
a Child is created
https://www.leftso.com/article/562.html