What is the difference between instanceof and Class.isAssignableFrom(...)?

Which of the following is better? a instanceof B or B.class.isAssignableFrom(a.getClass()) The only difference that I know of is, when 'a' is null, the first returns false, while the second throws an exception. Other than that, do they always give the same result? When using instanceof , you need to know the class of B at compile time. When using isAssignableFrom() it can be dynam

instanceof和Class.isAssignableFrom(...)有什么区别?

以下哪项更好? a instanceof B 要么 B.class.isAssignableFrom(a.getClass()) 我所知道的唯一区别是,当'a'为空时,第一个返回false,第二个抛出异常。 除此之外,他们总是给出相同的结果吗? 使用instanceof ,您需要在编译时了解B的类。 当使用isAssignableFrom()它可以是动态的,并在运行时更改。 instanceof只能用于引用类型,而不能用于基本类型。 isAssignableFrom()可以用于任何类对象: a instanceo

Synchronization vs Lock

java.util.concurrent API provides a class called as Lock , which would basically serialize the control in order to access the critical resource. It gives method such as park() and unpark() . We can do similar things if we can use synchronized keyword and using wait() and notify() notifyAll() methods. I am wondering which one of these is better in practice and why? If you're simply lock

同步与锁定

java.util.concurrent API提供了一个名为Lock的类,该类将基本上序列化控件以访问关键资源。 它提供了诸如park()和unpark() 。 如果我们可以使用synchronized关键字并使用wait()和notify() notifyAll()方法,我们可以做类似的事情。 我想知道在实践中哪一个更好,为什么? 如果你只是锁定一个对象,我宁愿使用synchronized 例: Lock.acquire(); doSomethingNifty(); // Throws a NPE! Lock.release(); // Oh noes, we

In Java critical sections, what should I synchronize on?

In Java, the idiomatic way to declare critical sections in the code is the following: private void doSomething() { // thread-safe code synchronized(this) { // thread-unsafe code } // thread-safe code } Almost all blocks synchronize on this , but is there a particular reason for this? Are there other possibilities? Are there any best practices on what object to synchronize on? (su

在Java关键部分中,我应该同步什么?

在Java中,在代码中声明关键部分的惯用方式如下: private void doSomething() { // thread-safe code synchronized(this) { // thread-unsafe code } // thread-safe code } 几乎所有的程序块都在this同步,但是这有什么特别的原因吗? 还有其他的可能吗? 是否有什么最佳做法在什么对象上进行同步? (比如Object私有实例?) 首先,请注意以下代码片段是相同的。 public void foo() { synchronized (t

Preconditions library to throw IllegalArgumentException for notNull check

Do you know some nice alternative to Apache Commons Validate or Guava Preconditions that would throw IllegalArgumentException instead of NullPointerException when checking if object is not null (except Spring Assert)? I'm aware that Javadocs say: Applications should throw instances of this class [NullPointerException] to indicate other illegal uses of the null object. Nevertheless, I ju

先决条件库抛出IllegalArgumentException为notNull检查

你知道一些很好的替代Apache Commons Validate或Guava Preconditions,当检查对象是否为空(Spring Assert除外)时,会抛出IllegalArgumentException而不是NullPointerException? 我知道Javadocs说: 应用程序应该抛出此类的实例[NullPointerException]来指示其他非法使用null对象。 不过,我只是不喜欢它。 对我而言,NPE总是意味着我忘记了在某处放置空引用。 我的眼睛是如此训练有素,我可以发现它以每秒几页的速度

Is it a bad idea if equals(null) throws NullPointerException instead?

The contract of equals with regards to null , is as follows: For any non-null reference value x , x.equals(null) should return false . This is rather peculiar, because if o1 != null and o2 == null , then we have: o1.equals(o2) // returns false o2.equals(o1) // throws NullPointerException The fact that o2.equals(o1) throws NullPointerException is a good thing, because it alerts us of program

如果equals(null)抛出NullPointerException而不是它是一个坏主意?

equals null的合同如下: 对于任何非空引用值x , x.equals(null)应该return false 。 这是相当奇特的,因为如果o1 != null和o2 == null ,那么我们有: o1.equals(o2) // returns false o2.equals(o1) // throws NullPointerException o2.equals(o1) throws NullPointerException是一件好事,因为它会提醒我们程序员错误。 然而,如果出于各种原因,我们只是将它切换到o1.equals(o2) ,那么这个错误就不会被o1.equals(o2

What is a NullPointerException, and how do I fix it?

What are Null Pointer Exceptions ( java.lang.NullPointerException ) and what causes them? What methods/tools can be used to determine the cause so that you stop the exception from causing the program to terminate prematurely? When you declare a reference variable (ie an object) you are really creating a pointer to an object. Consider the following code where you declare a variable of primiti

什么是NullPointerException,以及如何解决它?

什么是空指针异常( java.lang.NullPointerException )以及导致它们的原因? 可以使用哪些方法/工具来确定原因,以便阻止异常导致程序过早终止? 当你声明一个引用变量(即一个对象)时,你真的创建了一个指向对象的指针。 在声明基本类型int的变量时考虑以下代码: int x; x = 10; 在这个例子中,变量x是一个int ,Java会将它初始化为0。 当你在第二行中将它赋值为10时,你的值10被写入x所指向的内存位置。 但是,当

Check for null object

This question already has an answer here: Avoiding != null statements 58 answers This question was answered here, the title is not the same but the content is basically the same : Avoiding != null statements You will find the information you need in order to deal with your problem and several guidelines. It's a common problem.

检查空对象

这个问题在这里已经有了答案: 避免!= null语句58个答案 这个问题在这里得到了回答,标题是不一样的,但内容基本相同:避免!= null语句您将找到所需的信息来处理您的问题和几条准则。 这是一个常见问题。

how to handle nulls gracefully in Java

This question already has an answer here: Avoiding != null statements 58 answers Apache commons-lang3 offers this: b.setC(ObjectUtils.defaultIfNull(a.getC(), b.getC())); But I have to admit that I'm not sure if this would really be an improvement. You could make a method using var args that checks all the args for your if statement: public boolean argsNotNull(Object ... objs) { b

如何在Java中优雅地处理空值

这个问题在这里已经有了答案: 避免!= null语句58个答案 Apache commons-lang3提供了这个功能: b.setC(ObjectUtils.defaultIfNull(a.getC(), b.getC())); 但我不得不承认,我不确定这是否会有所改善。 你可以使用var args来检查你的if语句的所有参数: public boolean argsNotNull(Object ... objs) { boolean b = true; for(Object o : objs) { if(o == null) { b = false;

Optimistic way of checking null references in java

This question already has an answer here: Avoiding != null statements 58 answers add("x_Amount", amount); add("x_Currency_Code", currency); add("x_Exp_Date", expDate); void add(String name, String value) { if(value!=null && !value.isEmpty()) AIMRequest.put(name, value); } According your if 's, conditions you are comparing String s, so make a method: public boolean i

在java中检查空引用的乐观方式

这个问题在这里已经有了答案: 避免!= null语句58个答案 add("x_Amount", amount); add("x_Currency_Code", currency); add("x_Exp_Date", expDate); void add(String name, String value) { if(value!=null && !value.isEmpty()) AIMRequest.put(name, value); } 根据您if的,条件你是比较String S,所以请的方法: public boolean isValid(String s) { return s != null && s != "" &a

How to avoid null insertion in ArrayList?

This question already has an answer here: Avoiding != null statements 58 answers Avoiding null can be harmful sometimes and it could hide possible bugs. If you're worried about getting NullPointerException in some stage, you can simply check if the item stored in the ArrayList is null . You cannot disallow inserting null to ArrayList . You can try something like that, But if you wan

如何避免在ArrayList中的空插入?

这个问题在这里已经有了答案: 避免!= null语句58个答案 避免使用null有时可能是有害的,它可能会隐藏可能的错误。 如果您担心在某个阶段获取NullPointerException ,那么您可以简单地检查存储在ArrayList的项是否为null 。 你不能不允许向ArrayList插入null 。 你可以尝试类似的东西,但是如果你想要正确地做你正在尝试的东西,你必须在ArrayList类中重写add() 。 使用这个验证你可以避免null public static void m