The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
What is the initial capacity of the following string builder?以下字符串生成器的初始容量是多少?
StringBuilder sb = new StringBuilder("Able was I ere I saw Elba.");
Consider the following string:考虑以下字符串:
String hannah = "Did Hannah see bees? Hannah did.";
What is the value displayed by the expression 表达式hannah.length()
?hannah.length()
显示的值是多少?
What is the value returned by the method call 方法调用hannah.charAt(12)
?hannah.charAt(12)
返回的值是多少?
Write an expression that refers to the letter 写一个表达式,引用b
in the string referred to by hannah
.hannah
引用的字符串中的字母b
。
How long is the string returned by the following expression?以下表达式返回的字符串有多长?What is the string?绳子是什么?
"Was it a car or a cat I saw?".substring(9, 12)
In the following program, called 在以下名为ComputeResult
, what is the value of result
after each numbered line executes?ComputeResult
的程序中,执行每个编号行后的result
值是多少?
public class ComputeResult { public static void main(String[] args) { String original = "software"; StringBuilder result = new StringBuilder("hi"); int index = original.indexOf('a'); /*1*/ result.setCharAt(0, original.charAt(0)); /*2*/ result.setCharAt(1, original.charAt(original.length()-1)); /*3*/ result.insert(1, original.charAt(4)); /*4*/ result.append(original.substring(1,4)); /*5*/ result.insert(3, (original.substring(index, index+2) + " ")); System.out.println(result); } }
Show two ways to concatenate the following two strings together to get the string 显示将以下两个字符串连接在一起以获得字符串"Hi, mom."
:"Hi, mom."
的两种方法:
String hi = "Hi, "; String mom = "mom.";
Write a program that computes your initials from your full name and displays them.编写一个程序,根据您的全名计算您的姓名首字母并显示它们。
An anagram is a word or a phrase made by transposing the letters of another word or phrase; for example, "parliament" is an anagram of "partial men," and "software" is an anagram of "swear oft".字谜是将另一个单词或短语的字母转置而成的单词或短语;例如,“parliament”是一个“partial men”的字谜,“software”是一个“swear oft”的字谜。Write a program that figures out whether one string is an anagram of another string.编写一个程序,计算一个字符串是否是另一个字符串的字谜。The program should ignore white space and punctuation.程序应该忽略空格和标点符号。