instanceof

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

匿名 (未验证) 提交于 2019-12-02 21:52:03
判断对象是否为数组: 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 */

Netty中的ChannelFuture和ChannelPromise

匿名 (未验证) 提交于 2019-12-02 21:40:30
在Netty使用ChannelFuture和ChannelPromise进行异步操作的处理 这是官方给出的ChannelFutur描述 1 * | Completed successfully | 2 * +---------------------------+ 3 * +----> isDone() = true | 4 * +--------------------------+ | | isSuccess() = true | 5 * | Uncompleted | | +===========================+ 6 * +--------------------------+ | | Completed with failure | 7 * | isDone() = false | | +---------------------------+ 8 * | isSuccess() = false |----+----> isDone() = true | 9 * | isCancelled() = false | | | cause() = non-null | 10 * | cause() = null | | +===========================+ 11 * +--------------------------+ | |

Java常见面试问题: equals()与hashCode()的使用

匿名 (未验证) 提交于 2019-12-02 21:35:18
Ŀ¼ 默认情况下也就是从超类Object继承而来的equals()方法与‘==’是完全等价的, 比较的都是对象的内存地址. 但我们可以重写equals()方法, 使其按照我们的需求的方式进行比较, 比如String类就重写了equals方法, 它比较的是字符的序列, 而不再是内存地址. *自反性、对称性、传递性等都是 中的概念. 自反性: 对于任何非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(z)返回true, 那么x.equals(z)也应返回true. 一致性: 对于任何非null的引用值x与y, 假设对象上equals比较中的信息没有被修改, 则多次调用x.equals(y)始终返回true或者始终返回false. 对于任何非空引用值x, x.equal(null)应返回false. 这个问题主是针对映射相关的操作(Map接口). 我们知道Map接口的类会使用到键(Key)对象的哈希码, 当我们调用put()或get()方法对Map容器进行操作时, 都是根据键对象的哈希码来计算存储位置的,

Checking if a class is java.lang.Enum

时光怂恿深爱的人放手 提交于 2019-12-02 21:33:52
I'm trying to know if a class is an Enum, but I think I'm missing something: if (test.MyEnum.class instanceof Enum<?>.class) obj = resultWrapper.getEnum(i, test.MyEnum.class); else obj = resultWrapper.getObject(i); It gives me an error saying that Enum.class is not valid. So how I can check if a class is a Enum? I'm pretty sure it is possible to determine that, I'm just unable to get it. Thanks The correct syntax would be: Enum.class.isAssignableFrom(test.MyEnum.class) but for enums, here is a more convenient method: if (someObject.getClass().isEnum())) Update: for enum items with a body (e. g

Can I set the type of a Javascript object?

天涯浪子 提交于 2019-12-02 20:39:38
I'm trying to use some of the more advanced OO features of Javascript, following Doug Crawford's "super constructor" pattern. However, I don't know how to set and get types from my objects using Javascript's native type system. Here's how I have it now: function createBicycle(tires) { var that = {}; that.tires = tires; that.toString = function () { return 'Bicycle with ' + tires + ' tires.'; } } How can I set or retrieve the type of my new object? I don't want to create a type attribute if there's a right way to do it. Is there a way to override the typeof or instanceof operators for my custom

Checking if an annotation is of a specific type

怎甘沉沦 提交于 2019-12-02 19:16:26
I am using reflection to see if an annotation that is attached to a property of a class, is of a specific type. Current I am doing: if("javax.validation.Valid".equals(annotation.annotationType().getName())) { ... } Which strikes me as a little kludgey because it relies on a string that is a fully-qualified class-name. If the namespace changes in the future, this could cause subtle errors. I would like to do: if(Class.forName(annotation.annotationType().getName()).isInstance( new javax.validation.Valid() )) { ... } But javax.validation.Valid is an abstract class and cannot be instantiated. Is

What's the difference between isPrototypeOf and instanceof in Javascript?

时光怂恿深爱的人放手 提交于 2019-12-02 17:37:25
In some of my own older code, I use the following: Object.prototype.instanceOf = function( iface ) { return iface.prototype.isPrototypeOf( this ); }; Then I do (for example) [].instanceOf( Array ) This works, but it seems the following would do the same: [] instanceof Array Now, surely this is only a very simple example. My question therefore is: Is a instanceof b ALWAYS the same as b.prototype.isPrototypeOf(a) ? Yes, they do the same thing, both traverse up the prototype chain looking for an specific object in it. The difference between both is what they are , and how you use them, e.g. the

Java之路---Day13

試著忘記壹切 提交于 2019-12-02 17:11:22
2019-10-28-22:40:14 目录   1. Instanceof关键字   2. Final关键字     2.1 Final关键字修饰类     2.2 Final关键字修饰成员方法     2.3 Final关键字修饰局部变量     2.4 Final关键字修饰成员变量   3. 权限修饰符 Instanceof关键字   作用:判断一个父类引用的对象是什么子类   格式:     对象名 instanceof 类名称    1 package demosummary.instanceoftest; 2 3 public class Test { 4 public static void main(String[] args) { 5 //创建一个Dog对象 6 Animal animal = new Dog(); 7 //如果希望调用子类特有方法,需要向下转型 8 //判断一下父类引用animal本来是不是Dog 9 if (animal instanceof Dog){ 10 animal.setName("来福"); 11 Dog dog = (Dog)animal; 12 dog.skill(); 13 } 14 15 } 16 } Final关键字   意义:final关键字代表最终,不可改变的   常见四种用法:        1.可以用来修饰一个类

Javascript !instanceof If Statement

醉酒当歌 提交于 2019-12-02 16:01:50
This is a really basic question really just to satisfy my curiosity, but is there a way to do something like this: if(obj !instanceof Array) { //The object is not an instance of Array } else { //The object is an instance of Array } The key here being able to use the NOT ! in front of instance. Usually the way I have to set this up is like this: if(obj instanceof Array) { //Do nothing here } else { //The object is not an instance of Array //Perform actions! } And its a little annoying to have to create an else statement when I simply want to know if the object is a specific type. Enclose in

JSONObject

寵の児 提交于 2019-12-02 14:27:50
package com.ruoyi.common.json; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.StringTokenizer; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.fasterxml.jackson.databind.ObjectMapper; import com.ruoyi.common.utils.StringUtils; /** * 通用消息对象,基于Map实现的可嵌套数据结构。 支持JSON数据结构。 * * @author ruoyi */ public class JSONObject extends LinkedHashMap<String, Object> { private static final long serialVersionUID = 1L; private static final Pattern arrayNamePattern = Pattern.compile("(