fruit.substring(2, 5)
将返回什么?推理: substring方法接受两个参数。
boolean b1 = true, b2 = false; int i1 = 1, i2 = 2;
(i1 | i2) == 3
i2 && b1
b1 || !b2
(i1 ^ i2) < 4
推理: i2 && b1在int和boolean之间不允许。
class Main {
public static void main (String[] args) {
int array[] = {1, 2, 3, 4};
for (int i = 0; i < array.size(); i++) {
System.out.print(array[i]);
}
}
}
推理: array.size()是无效的,要获取数组的大小或长度,可以使用array.length。
interface Interface1 {
static void print() {
System.out.print("Hello");
}
}
interface Interface2 {
static void print() {
System.out.print("World!");
}
}
super1.print(); super2.print();
this.print();
super.print();
Interface1.print(); Interface2.print();
String str = "abcde"; str.trim(); str.toUpperCase(); str.substring(3, 4); System.out.println(str);
推理: 您应该将trim的结果分配回String变量。否则,它将无法工作,因为Java中的字符串是不可变的。
class Main {
public static void main (String[] args){
System.out.println(print(1));
}
static Exception print(int i){
if (i>0) {
return new Exception();
} else {
throw new RuntimeException();
}
}
}
interface One {
default void method() {
System.out.println("One");
}
}
interface Two {
default void method () {
System.out.println("One");
}
}
class Three implements One, Two {
public void method() {
super.One.method();
}
}
class Three implements One, Two {
public void method() {
One.method();
}
}
class Three implements One, Two {
}
class Three implements One, Two {
public void method() {
One.super.method();
}
}
class Main {
public static void main (String[] args) {
List list = new ArrayList();
list.add("hello");
list.add(2);
System.out.print(list.get(0) instanceof Object);
System.out.print(list.get(1) instanceof Integer);
}
}
package mypackage;
public class Math {
public static int abs(int num){
return num < 0 ? -num : num;
}
}
package mypackage.elementary;
public class Math {
public static int abs (int num) {
return -num;
}
}
import mypackage.Math;
import mypackage.elementary.*;
class Main {
public static void main (String args[]){
System.out.println(Math.abs(123));
}
}
解释: 答案是 "123"。abs()方法计算为mypackage.Math类中的方法,因为导入语句形式为:
import packageName.subPackage.*
class MainClass {
final String message()
{
return "Hello!";
}
}
class Main extends MainClass {
public static void main(String[] args) {
System.out.println(message());
}
String message() {
return "World!";
}
}
解释: 在第10行有编译错误,因为final方法不能被重写,这里的message()是final方法。此外,请注意,非静态方法message()无法从静态上下文中引用。
int0
是否可以被 5
整除:boolean isDivisibleBy5 = _____
int0 / 5 ? true: false
int0 % 5 == 0
int0 % 5 != 5
Math.isDivisible(int0, 5)
class Main {
public static void main(String[] args){
int a = 123451234512345;
System.out.println(a);
}
}
推理: 在Java中,int类型可用于表示从 -2147483648 到 2147483647 的任何整数。因此,此代码将无法编译,因为分配给'a'的数字大于int类型可以容纳的范围。
class Main {
public static void main (String[] args) {
String message = "Hello world!";
String newMessage = message.substring(6, 12)
+ message.substring(12, 6);
System.out.println(newMessage);
}
}
for (Pencil pencil : pencilCase) {}
for (pencilCase.next()) {}
for (Pencil pencil : pencilCase.iterator()) {}
for (pencil in pencilCase) {}
System.out.print("apple".compareTo("banana"));
0
names.sort(Comparator.comparing(String::toString))
Collections.sort(names)
names.sort(List.DESCENDING)
names.stream().sorted((s1, s2) -> s1.compareTo(s2)).collect(Collectors.toList())
new SimpleDateFormat("yyyy-MM-dd").format(new Date())
new Date(System.currentTimeMillis())
LocalDate.now()
Calendar.getInstance().getTime()
解释: LocalDate是Java 8中添加的最新类。
int0
是否可以被 5
整除:boolean isDivisibleBy5 = _____
int0 / 5 ? true: false
int0 % 5 == 0
int0 % 5 != 5
Math.isDivisible(int0, 5)