java 判断对象什么类型,在java中,怎样判断一个变量是什么类型的
大家好,今天小编来为大家解答以下的问题,关于java 判断对象什么类型,在java中,怎样判断一个变量是什么类型的这个很多人还不知道,现在让我们一起来看看吧!
java关于instance的定义
java中的instanceof运算符是用来在运行时指出对象是否是特定类的一个实例。instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例。
用法:
result= object instanceof class
参数:
Result:布尔类型。
Object:必选项。任意对象表达式。
Class:必选项。任意已定义的对象类。
说明:
如果 object是 class的一个实例,则 instanceof运算符返回 true。如果 object不是指定类的一个实例,或者 object是 null,则返回 false。
但是instanceof在Java的编译状态和运行状态是有区别的:
在编译状态中,class可以是object对象的父类,自身类,子类。在这三种情况下Java编译时不会报错。
在运行转态中,class可以是object对象的父类,自身类,不能是子类。在前两种情况下result的结果为true,最后一种为false。但是class为子类时编译不会报错。运行结果为false。
扩展资料
例子:
接口Person
public interface Person{
public void eat();
}
实现类People
public class People implements Person{
private int a=0;
@Override
public void eat(){
System.out.println("======"+a);
}
}
子类xiaoming:
public class xiaoming extends People{
private String name;
@Override
public void eat(){
System.out.println("+++++++++");
}
}
主函数
public static void main(String[] args){
People p=new People();
xiaoming x=new xiaoming();
System.out.println(p instanceof Person);
System.out.println(p instanceof xiaoming);-----2
System.out.println(x instanceof Person);
System.out.println(x instanceof People);
}
注意:上面2处的代码在编译时不会报错。
运行结果:
true
false
true
true
参考资料:百度百科:Java
百度百科:instance
在java中,怎样判断一个变量是什么类型的
哥们。。
一般没有在程序里才去判断这个《是什么类型的》。。。而且这样没有意义。
因为类型太多了。一般都是判断《不是什么类型》就会用你说的instanceof做一个if判断。
若想知道一个出现的对象是什么类型。要是你想用程序识别的话你就需要用到映射了。
这个一两句说不清。若是你本人读代码想知道什么类型。那就按CTRL+鼠标点击该对象。。
额 LZ若真想在程序里判断。只好去看看映射的东西了
Java中怎样判断一个变量是否属于哪种类型
变量类型识别有3种方法:
1、通过反射拿到变量的类型;
2、instanceof关键字判断;
3、通过java的多态(方法重载)来DIY类型识别。
举例如下:
packagecom.cxyapi.oo;
/**类型识别工具测试类
*@authorcxy@www.cxyapi.com
*/
publicclassTypeToolsTest
{
publicstaticvoidmain(String[]args)
{
inti=0;
TypeObjectto=newTypeObject();
//1.反射
System.out.println("to的类型:"+to.getClass().getSimpleName());
System.out.println(int.class.getSimpleName());
System.out.println(Integer.class.getSimpleName());
//但是对于一个不确定类型的基本数据类型变量我们没法用反射来获取其类型。
System.out.println("----------------------");
//2.instanceof
if(toinstanceofTypeObject){System.out.println("to是TypeObject类型的");}
//但是这种办法貌似也没法确定基本数据类型
System.out.println("----------------------");
//以上两种方式对于对象,引用类型的都很好用,但是对基本数据类型就不那么好用了。
//3.通过多态(方法的重载)
System.out.println("i是:"+TypeTools.getType(i));
System.out.println("to是:"+TypeTools.getType(to));
System.out.println("\"cxyapi\"是:"+TypeTools.getType("www.cxyapi.com"));
//可以看出来最后一种方式使用多态的方式达到了检测类型(基本类型和引用类型)的目的
//除了弥补其他两种方式不能检测基本数据类型的不足在外,还能自己DIY类型信息
}
}
//定义一个类,为了演示引用类型的类型检测
classTypeObject{}
自定义的类型识别工具:
packagecom.cxyapi.oo;
importjava.util.HashMap;
importjava.util.Map;
/**类型识别工具
*@authorcxy@www.cxyapi.com
*/
publicclassTypeTools
{
//获得类型
publicstaticMap<String,String>getType(Objecto)
{
Map<String,String>typeInfo=newHashMap<String,String>();
typeInfo.put("类型",o.getClass().getSimpleName());
typeInfo.put("描述","引用类型");
returntypeInfo;
}
publicstaticMap<String,String>getType(inti)
{
Map<String,String>typeInfo=newHashMap<String,String>();
typeInfo.put("类型","int");
typeInfo.put("描述","整形");
returntypeInfo;
}
publicstaticMap<String,String>getType(longl)
{
Map<String,String>typeInfo=newHashMap<String,String>();
typeInfo.put("类型","long");
typeInfo.put("描述","长整型");
returntypeInfo;
}
publicstaticMap<String,String>getType(booleanb)
{
Map<String,String>typeInfo=newHashMap<String,String>();
typeInfo.put("类型","boolean");
typeInfo.put("描述","布尔类型");
returntypeInfo;
}
publicstaticMap<String,String>getType(charb)
{
Map<String,String>typeInfo=newHashMap<String,String>();
typeInfo.put("类型","char");
typeInfo.put("描述","字符");
returntypeInfo;
}
publicstaticMap<String,String>getType(floatf)
{
Map<String,String>typeInfo=newHashMap<String,String>();
typeInfo.put("类型","float");
typeInfo.put("描述","单精度浮点型");
returntypeInfo;
}
publicstaticMap<String,String>getType(doubled)
{
Map<String,String>typeInfo=newHashMap<String,String>();
typeInfo.put("类型","double");
typeInfo.put("描述","双精度浮点型");
returntypeInfo;
}
publicstaticMap<String,String>getType(Strings)
{
Map<String,String>typeInfo=newHashMap<String,String>();
typeInfo.put("类型","String");
typeInfo.put("描述","字符串类型");
returntypeInfo;
}
}
java中怎么判断变量是不是int类型的,用typeof报错啊
注意几个问题
1 java中没有typeof这个操作符或者方法,有instanceof。
2 java中的变量全部都是要先声明的。
因此,判断变量是不是int型,那么可以通过查看变量声明的地方,一定有类型的。
所以很明确的就知道这个变量是什么类型。
3使用instanceof,这个是使用在对象中的。判断对象是不是某个类的对象等,
如果要真想测试的话,那么就将你定义的int变量转换成Integer类型的,然后判断这个对象是不是Integer的对象。但是没有多少意义
关于java 判断对象什么类型到此分享完毕,希望能帮助到您。