instanceof

instanceof operator in java for comparing different classes

不问归期 提交于 2019-11-28 12:13:50
I was trying to see how instanceof operator in Java works and am facing a very odd issue. public static void main(String[] args) { Map m = new HashMap(); System.out.println("m instanceof Date: " + (m instanceof Date)); } The above returns false as expected. However, public static void main(String[] args) { HashMap m = new HashMap(); System.out.println("m instanceof Date: " + (m instanceof Date)); } This does not even compile. I get an error inconvertible types found : java.util.HashMap required : java.util.Date What am I missing here? I am using IntelliJ Idea 11. From the Java Language

Java判断对象类型是否为数组

不打扰是莪最后的温柔 提交于 2019-11-28 08:57:37
判断对象是否为数组: public static void main(String[] args) { String[] a = ["1","2"]; if(a instanceof String[]){ System.out.println("ss") } if(a.getClass().isArray()){ System.out.println("yy") } } 第一种做法:instanceof java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例。instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例。 用法: result = object instanceof class 参数: Result:布尔类型。 Object:必选项。任意对象表达式。 Class:必选项。任意已定义的对象类。 第二种做法:Class类 isArray() /** * Determines if this {@code Class} object represents an array class. * * @return {@code true} if this object represents an array class; * {@code false} otherwise. * @since JDK1.1 */

判断变量是数组还是对象

混江龙づ霸主 提交于 2019-11-28 05:33:50
① 通过typeof 加 长度判断 ② 通过instanceof判断,不过有判断顺序; 以下为示例: var judgeType1 = function(o){ if(typeof o == 'object'){ if(typeof o.length == 'number'){ alert('变量类型为 Array') return } alert('变量类型为 Object') return } alert('变量类型不为 Object') } var judeType2 = function(o){ if(o instanceOf Array){ alert('变量类型为 Array') }else if(o instanceOf Object){ alert('变量类型为 Object') }else{ alert('变量类型不为 Object') } } 来源: https://www.cnblogs.com/ClaudiaYan/p/11394294.html

Switch over type in java

£可爱£侵袭症+ 提交于 2019-11-28 04:42:51
Before I start, I know there are a bunch of answers to this question that suggest alternate approaches. I'm looking for assistance to this particular approach as to whether it is possible, and if not, similar approaches that might work. I have a method that takes a superclass and calls a method based on the type of the passed object. for instance: public void handle(Object o){ if (o instanceof A) handleA((A)o); else if (o instanceof B) handleB((B)o); else if (o instanceof C) handleC((C)o); else handleUnknown(o); I can't modify the subtypes to override a handle() method, as this answer would

Instanceof and namespaces

大城市里の小女人 提交于 2019-11-28 03:12:14
问题 I am facing an unexpected behaviour trying to use the following: $object instanceof $class 1/ PHP 'instanceof' keyword and namespaces work well together, as explained in the official doc. 2/ Sometimes, however, backslash escaping gives in to more subtle (obscure?) behaviour, as Ben kindly explained in this nice post. Somewhere deep down in my code, y set a couple of dumps as follow: var_dump($object, $class); var_dump($object instanceof $class); which gives me the following output when

Avoiding 'instanceof' in Java

僤鯓⒐⒋嵵緔 提交于 2019-11-28 03:05:42
I have the following (maybe common) problem and it absolutely puzzles me at the moment: There are a couple of generated event objects which extends the abstract class Event and I want to divide them to Session Beans, like public void divideEvent(Event event) { if (event instanceof DocumentEvent) { documentGenerator.gerenateDocument(event); } else if (event instanceof MailEvent) { deliveryManager.deliverMail(event); ... } ... } But there could be more than two event types in future, so the if-else will be long and maybe unreadable. Additionally I think instanceof is not really "best practice"

instanceof Vs getClass( )

血红的双手。 提交于 2019-11-28 02:47:27
I see gain in performance when using getClass() and == operator over instanceOf operator. Object str = new Integer("2000"); long starttime = System.nanoTime(); if(str instanceof String) { System.out.println("its string"); } else { if (str instanceof Integer) { System.out.println("its integer"); } } System.out.println((System.nanoTime()-starttime)); starttime = System.nanoTime(); if(str.getClass() == String.class) { System.out.println("its string in equals"); } else { if(str.getClass() == Integer.class) { System.out.println("its integer"); } } System.out.println((System.nanoTime()-starttime));

Calling method that exists in child classes but not in parent class

久未见 提交于 2019-11-28 01:58:20
public class Parent { .... } public class Child1 extends Parent { .... public void foo() { .... } } public class Child2 extends Parent { .... public void foo() { .... } } Here method foo() only exists in the Child classes and CAN NOT be added to the Parent class (not even abstract method). In this situation when I want to call the foo() method on obj which is Parent class's reference then I need to use intanceof with multiple if..else which I want to avoid. Parent obj = ...// Object of one of the child classes obj.foo(); EDIT: I Need to use type of obj as Parent only. Else I will not be able

关于equals和hashCode

泄露秘密 提交于 2019-11-28 01:47:53
含义 equal和hashCo de都是Object类 中的方法 public boolean equals(Object obj) { return (this == obj); } public native int hashCode(); equals默认是比较对象的指针是否指向同样的内存地址。 hashCode是本地方法 : 会根据内存地址转换而来。 重写equals和hashCode原则: equals一样,则hashCode也必须一致 这个到底很容易想通,对于使用hash散列的数据结构如hashMap,首先会根据对象的hashCode找到桶的位置,再在列表或红黑树中用equals比较对象来判断对象是否已经存在 如果同一个对象,得到的hasCode不一致,则可以在hashMap中存放到多个桶中,导致内存泄漏。 equals是比较对象的地址,如果我们判断两个对象时根据内部某些属性来的话,就要重写equals和hashCode 重写equals和hashCode 原则: 自反性。对于任何非null的引用值x,x.equals(x)应返回true。 对称性。对于任何非null的引用值x与y,当且仅当:y.equals(x)返回true时,x.equals(y)才返回true。 传递性。对于任何非null的引用值x、y与z,如果y.equals(x)返回true,y.equals

不关联生命周期的 observeForever 方法。

大城市里の小女人 提交于 2019-11-28 00:57:37
LiveData.java ------------------------------ @MainThread public void observeForever(@NonNull Observer<? super T> observer) { assertMainThread("observeForever"); AlwaysActiveObserver wrapper = new AlwaysActiveObserver(observer); ObserverWrapper existing = mObservers.putIfAbsent(observer, wrapper); if (existing != null && existing instanceof LiveData.LifecycleBoundObserver) { throw new IllegalArgumentException("Cannot add the same observer" + " with different lifecycles"); } if (existing != null) { return; } wrapper.activeStateChanged(true); } private class AlwaysActiveObserver extends