我创建了这个愚蠢的程序来玩 wait()
public class WaitTest {
public static void main(String [] args) {
System.out.print("1 ");
synchronized(args){
System.out.print("2 ");
try {
args.wait();
args.notifyAll();
}
catch(InterruptedException e){ System.out.print("exception caught");}
System.out.print("3 ");
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我的机器上,代码永远不会打印3,除非我写wait(100)或其他毫秒数.为什么是这样?
鉴于:
public class LineUp {
public static void main(String[] args) {
double d = 12.345;
// insert code here
}
}
Run Code Online (Sandbox Code Playgroud)
在第4行插入哪个代码片段会产生输出| 12.345|?
Run Code Online (Sandbox Code Playgroud)A. System.out.printf("|%7d| \n", d); B. System.out.printf("|%7f| \n", d); C. System.out.printf("|%3.7d| \n", d); D. System.out.printf("|%3.7f| \n", d); E. System.out.printf("|%7.3d| \n", d); F. System.out.printf("|%7.3f| \n", d); Answer: F
printf语句的解释是什么,为什么是|%7d | 正在给予illegalFormatConversionException ?
谢谢
我正在攻读java认证,我从Mughal的书中看到了这个例子:
public class Smiley extends Thread
{
@Override
public void run()
{
while(true)
{
synchronized(this)
{
try
{
System.out.print(":");
Thread.sleep(100);
System.out.print("-");
Thread.sleep(100);
System.out.println(")");
Thread.sleep(100);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
public static void main(String[] args)
{
new Smiley().start();
new Smiley().start();
}
}
Run Code Online (Sandbox Code Playgroud)
目的是每行打印一个笑脸:-).我的问题是,为什么同步实例(这个)不能实现这一点?为什么我们需要在静态级别上同步?
谢谢,
我读了一本sais的电子书:
int :b;
int e#;
Run Code Online (Sandbox Code Playgroud)
不是合法的标识符,但我不明白为什么":"和"#"不是合法的令牌.你有什么主意吗?
我正在学习SCJP证书,在使用StringBuilder和StringBuffer API时,我发现一些情况我无法理解那里发生了什么.
这是我的示例代码:
StringBuffer sb1 = new StringBuffer("sb1");
StringBuilder sb2 = new StringBuilder("sb2");
StringBuffer sb11 = new StringBuffer(sb1);
StringBuilder sb22 = new StringBuilder(sb2);
StringBuffer sb12 = new StringBuffer(sb2);
StringBuilder sb21 = new StringBuilder(sb1);
System.out.println(sb1);
System.out.println(sb2);
System.out.println(sb11);
System.out.println(sb22);
System.out.println(sb12);
System.out.println(sb21);
System.out.println();
sb1.append("a1");
sb2.append("a2");
System.out.println(sb1);
System.out.println(sb2);
System.out.println(sb11);
System.out.println(sb22);
System.out.println(sb12);
System.out.println(sb21);
Run Code Online (Sandbox Code Playgroud)
这是运行上面代码的结果:
sb1
sb2
sb1
sb2
sb2
sb1
sb1a1
sb2a2
sb1
sb2
sb2
sb1
Run Code Online (Sandbox Code Playgroud)
用于sb11,sb22,sb12和sb21的构造函数未在API中记录.此外,看看结果,似乎对于这4个案例,承认String或CharSequence的构造函数是使用的那个.
如果是,为什么Java会自动转换字符串中的StringBuffer?据我所知,Autoboxing并没有走得那么远.
我错过了什么?
我知道Object是java中所有类的最超级类.但是,在代码之下,我无法理解.请帮帮我.
Object c = new long[4];
Object d = new int[4];
Run Code Online (Sandbox Code Playgroud) 这是一个取自SCJP 6示例的程序.在这里,我们enum使用不同的咖啡大小创建一个,并声明一个私有变量,ounces用于获取枚举值的盎司值.
我无法理解getLidCode被覆盖的方法的使用.如何访问该getLidCode方法?
package com.chapter1.Declaration;
enum CoffeSize {
BIG(8), HUGE(10), OVERWHELMING(20) {
public String getLidCode() {
return "B";
}
};
CoffeSize(int ounce) {
this.ounce = ounce;
}
private int ounce;
public int getOunce() {
return ounce;
}
public void setOunce(int ounce) {
this.ounce = ounce;
}
public String getLidCode() {
return "A";
}
}
public class Prog7 {
CoffeeSize size;
public static void main(String[] args) {
Prog7 p = new Prog7();
p.size …Run Code Online (Sandbox Code Playgroud) import java.util.*;
class U {
int x;
U(int x) {
this.x = x;
}
}
public class G {
public U a = new U(22);
public U b = new U(23);
Integer y = 22;
Integer r = 23;
void a() {
Map<U, Integer> set = new HashMap<U, Integer>();
set.put(a, y);
set.put(a, r);
set.put(b, y);
System.out.print(set.size() + " ");
}
public static void main(String[] args) {
G m = new G();
m.a();
}
}
Run Code Online (Sandbox Code Playgroud)
我总是对地图和列表感到困惑.我知道当map将密钥放入集合时,它会调用hashcode,如果存储桶相同,则调用equal方法.但是,我了解到,如果类重写这两个方法,则不会存储重复的键.例如包装类:String实现自己的hashcode和equal方法.此外,如果您不这样做,则会调用唯一的哈希码,并且重复的密钥将存储在集合中.
但是在上面的例子中,类U没有实现hashcode和equal方法.但是,Map不允许重复键.
我检查了SIZE:它的2应该是3,因为我的U类没有实现hashcode也没有.
请清除我
提前致谢
根据文档:此实现将指定的列表转储到数组中,对数组进行排序,并迭代列表,从数组中的相应位置重置每个元素
鉴于下面的程序,我无法理解排序是因为内部jvm如何判断该字母'A'是小于还是大于字母'a'?由于这是一个字符串,字母不会在ascii值中假定,所以排序是如何发生的?
public class LetterASort {
public static void main(String[] args) {
ArrayList<String> strings = new ArrayList();
strings.add("aAaA");
strings.add("AaA");
strings.add("aAa");
strings.add("AAaa");
Collections.sort(strings);
for (String s : strings)
{
System.out.print(s + " "); //prints AAaa AaA aAa aAaA
}
}
}
Run Code Online (Sandbox Code Playgroud)
我也尝试调试代码,这给我带来了新的疑问:数组的长度变成了4而不是3,因为collections.sort它包含在长度中
public class Chicks {
synchronized void yacks(long id)
{
for(int x = 1; x<3; x++)
{
System.out.println(id + " ");
Thread.yield();
}
}
}
class ChickYacks implements Runnable
{
Chicks c; // No exception if I declare it as static
public static void main(String[] args) {
new ChickYacks().go();
}
public void run()
{
c.yacks(Thread.currentThread().getId()); //Throws a Nullpointer exceptin
}
void go()
{
c = new Chicks();
new Thread(new ChickYacks()).start();
new Thread(new ChickYacks()).start();
}
}
Run Code Online (Sandbox Code Playgroud)
为什么它会抛出一个Nullpointer异常run method().一切看起来都很好.当我宣布Chicks 'c' …