序列化类时是否必须实现 Serializable 类。我尝试将对象数据放入带有和不带有可序列化实现的文件中,发现绝对没有区别。
import java.io.*;
import java.net.*;
public class SerializableTest {
int a= 10;
String test="Serialize test";
public static void main(String [] args){
SerializableTest test =new SerializableTest();
test.save();
}
public void save(){
try{
FileOutputStream fs = new FileOutputStream("save.res");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(test);
os.close();
}
catch (Exception ex ){
System.out.println("Error in opening or saving file");
}
System.out.println("Complete");
}
}
Run Code Online (Sandbox Code Playgroud)
import java.io.*;
import java.net.*;
public class SerializableTest **implements Serializable**{
int a= 10;
String test="Serialize test";
public static void …Run Code Online (Sandbox Code Playgroud) 我目前正在开发具有XMLBeans绑定而不是默认JAXB绑定的Apache CXF Web服务。我正在使用Java 1.6编译和运行代码。对于以下代码段,在运行时出现“ DOM Level 3 Not Implemented”错误:
ExtType[] extTypeList = p.getExtArray();
for (ExtType extType : extTypeList) {
Node node = extType.getDomNode();
NodeList objList = node.getChildNodes();
for (int i = 0; i < objList.getLength(); ++i) {
Node text = (Node) objList.item(i);
if (text.getNodeName() != null
&& text.getNodeName() == XmlConstant.NODE_NAME) {
info.setDuration(text
.getTextContent());
}
}
}
Run Code Online (Sandbox Code Playgroud)
在JBoss中显示的确切错误如下:
java.lang.RuntimeException: DOM Level 3 Not implemented
at org.apache.xmlbeans.impl.store.DomImpl._node_getTextContent(DomImpl.java:2516)
at org.apache.xmlbeans.impl.store.Xobj$NodeXobj.getTextContent(Xobj.java:2607)
Run Code Online (Sandbox Code Playgroud)
从以上错误消息中可以明显看出,由于在运行时未找到DOM 3级API,因此getTextContent方法引起了异常。如何消除此错误?我猜我将不得不弄清楚哪个jar包含DOM API,并从该jar中删除所有与dom相关的类,以便代替使用与jdk一起提供的默认DOM API。或者,是否有一种方法可以使用DOM而不依靠getTextContent方法来获取xml标记的文本内容?
在阅读Java 8中引入的新功能的同时,我发现了Predicates的概念.我注意到互联网和书籍中提供的大多数示例都使用静态函数来创建谓词.
以下面的Apple类为例:
public class Apple {
private String color;
private int weight;
private static final int SMALL_APPLE_MAX_WEIGHT = 150;
public Apple(String color, int weight) {
this.color = color;
this.weight = weight;
}
public static boolean isGreenApple(Apple apple) {
return null!=apple && null!=apple.getColor() && "green".equals(apple.getColor());
}
public boolean isBigApple() {
return this.getWeight() > SMALL_APPLE_MAX_WEIGHT;
}
}
Run Code Online (Sandbox Code Playgroud)
我现在可以创建一个新的谓词如下:
Predicate<Apple> isGreenApple = Apple::isGreenApple;
Predicate<Apple> isBigApple = Apple::isBigApple;
Run Code Online (Sandbox Code Playgroud)
如上所示,我可以使用静态和实例方法创建谓词.哪种方法是首选方法,为什么?
Java 8允许您在不同的上下文中使用相同的lambda表达式。考虑下面的代码来演示这一点:
public class LambdasTypeCheck {
public static void main(String []args) {
//Same lambda expression can be used in different contexts.
Predicate<List<String>> predicate = (l) -> l.add("1");
Consumer<List<String>> consumer = (l) -> l.add("1");
}
}
Run Code Online (Sandbox Code Playgroud)
lambda表达式(l) -> l.add("1");在两个地方重复。如何避免这种重复?我可以重用此lambda表达式的一种方法是,是否有一些接口/类是所有lambda表达式的父级,这样我就可以将任何lambda表达式分配给此类接口/类的引用。
我试图理解java中的同步。我有以下示例
public class TestThr implements Runnable {
public static void main(String[] args) {
Thread t=new Thread(new TestThr());
Thread t1=new Thread(new TestThr());
t.start();
t1.start();
}
@Override
public void run() {
sync();
}
public synchronized void sync(){
for (int i=0;i<10;i++){
System.out.println("Running "+Thread.currentThread().getName());
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出: 运行线程-0 运行线程1 运行线程-0 运行线程1 运行线程-0 运行线程1 运行线程1 运行线程1 运行线程-0 运行线程1 运行线程1 运行线程1 运行线程1 运行线程1 运行线程-0 运行线程-0 运行线程-0 运行线程-0 运行线程-0 运行线程-0
From above example I was expecting one thread(whoever enter first) will complete the iteration and then …
以下类中每个表达式的术语是什么:
例如:
class Test {
int a=0;
void method(boolean boo){
String b="";
try
{
new Thread().sleep(1000);
}
catch(InterruptedException e){}
JOptionPane.showMessageDialog(null,"test");
BufferedImage image=ImageIO.read(new File("C:\\file.png"));
}
}
Run Code Online (Sandbox Code Playgroud)
从我所知道的a是一个字段,boo是一个参数,b并且image是局部变量.
用于什么术语
new Thread().sleep()JOptionPane.showMessageDialog()ImageIO.read()new File()InterruptedException