在JAVA中如何取得一个变量的类型?

梦断酒醒 2007-09-11 02:43:59
希望可以通过变量知道它的类型,特别是基本数据类型变量的类型,不知道有没有办法?
...全文
25667 34 打赏 收藏 转发到动态 举报
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
Parson 2007-09-12
  • 打赏
  • 举报
回复
1楼的,拿到JDK 1.5之前的版本,肯定是不行的。
xia20021 2007-09-12
  • 打赏
  • 举报
回复
1楼的最合理..顶下..
TNT_abc 2007-09-12
  • 打赏
  • 举报
回复
反射机制
擒兽 2007-09-12
  • 打赏
  • 举报
回复
对象只是抽象的说,一个类模型实例化对象后里面有(函数或者叫方法或者叫过程)就和面向过程一样,顶多对象引用来引用去罢了,还不一样是有过程,过程也叫算法,程序永远都会有这一个流程

不能面向对象就什么都神了
ycxiangyun 2007-09-11
  • 打赏
  • 举报
回复
支持一楼
CaiKanXP 2007-09-11
  • 打赏
  • 举报
回复
弱类型……,打错字了
CaiKanXP 2007-09-11
  • 打赏
  • 举报
回复
既然是基本数据类型,那么其类型在你声明的时候就已经确定了。
Java又不是像JavaScript那样的若类型语言,1楼的做法是用JDK5把基本数据类型和对象混淆了。
Roy_luo 2007-09-11
  • 打赏
  • 举报
回复
自己写了个工具类,大家给点意见:可以查看类的详细信息的。
package mycom;

import java.lang.reflect.*;

/**
* the tools of looking for the properties of a class
* print the constructor and method
*
* @author Roy
* @since 2007-09-10
* @version 1.0
*/
public class GetProperties {
/**
* return the parameters of the specified methods,constructors or fields fit
* the jdk1.4
*
* @param acceObject AccessibleObject
* @return
* @author Roy
*/
private static String getParameter(AccessibleObject acceObject) {
String result = "";
Class[] parameters = null;
if (acceObject instanceof Method) {
parameters = ((Method) acceObject).getParameterTypes();
}
if (acceObject instanceof Constructor) {
parameters = ((Constructor) acceObject).getParameterTypes();
}
int paramCount = parameters.length;
// if no parameters
if (paramCount < 1) {
return result;
}
StringBuffer str = new StringBuffer();

for (int i = 0; i < paramCount; i++) {
String s = parameters[i].getName();// generally,the s is contain

str.append(getShortName(s)).append(",");
}
if (str.length() > 1)//delete the last ","
result = str.substring(0, str.length() - 1);

return result;
}
/**
* print the field
* @param object Class
*/
public static void printFields(Class object){
System.out
.println("----------------------Fileds----------------------");
StringBuffer str = new StringBuffer();
Field[] field = object.getDeclaredFields();

for(int i=0;i<field.length; i++){
int type =field[i].getModifiers();
str.append(Modifier.toString(type)).append(" ");

Class declared =field[i].getType();
str.append(getShortName(declared.getName())).append(" ");
str.append(field[i].getName()).append("\n");
//str.append(field[i].get(field.[i]).toString()).append("\n");

//System.out.println(field[i].toString());

}
System.out.print(str.toString());
}

/**
* print the construct function and its parameters
*
* @param object Class
* @return
*/
public static void printConstructs(Class object) {
System.out
.println("----------------------Constructors----------------------");

Constructor[] cos = object.getConstructors();
for (int i = 0; i < cos.length; i++) {
System.out.print(cos[i].getName() + "(");
System.out.print(getParameter(cos[i]));
System.out.print(")" + "\n");

}
}

/**
* print the declared method and its parameter
*
* @param object Class
*/
public static void printMethods(Class object) {
System.out
.println("----------------------Methods----------------------");
StringBuffer str = new StringBuffer();
Method[] methods = object.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
int type = methods[i].getModifiers();// Returns the Java language
// modifiers
// The Modifier class should be used to decode the modifiers
if(type != 0)
str.append(Modifier.toString(type)).append(" ");

str.append(getShortName(methods[i].getReturnType().getName()))
.append(" ");
str.append(methods[i].getName()).append("(").append(
getParameter(methods[i]));
str.append(")").append("\n");

}
System.out.println(str);
}

/**
* if the return type is a array,the str is "[*", for example the
* function" int[] getNumber(){..}" the
* "methods[i].getReturnType().getName()" equals "[I"
*
* @param str String
*/
private static String getShortName(String str) {

StringBuffer result = new StringBuffer();
int i = -50;
if (str.indexOf("[") != -1) {//array type
if (str.length() == 2) {
switch (str.charAt(1)) {
case 'B':
result.append("byte[]");
break;
case 'S':
result.append("short[]");
break;
case 'I':
result.append("int[]");
break;
case 'C':
result.append("char[]");
break;
case 'J':
result.append("long[]");
break;
case 'F':
result.append("float[]");
break;
case 'D':
result.append("double[]");
break;
}
} else {
if ((i = str.lastIndexOf(".")) != -1) {//because the end of the str is ";"
result.append(str.substring(i + 1,str.length()-1)).append("[]");
}
}
} else {//no array type
if ((i = str.lastIndexOf(".")) != -1) {
return str.substring(i + 1);
}
return str;
}
return result.toString();
}

public static void main(String[] args) throws Exception {
if(args.length<1){
StringBuffer str=new StringBuffer();
str.append("usage:java GetProperties ARGS").append("\n");
str.append(" the args contain the package:").append("\n");
str.append(" example: java GetProperties java.lang.String");
System.out.println(str.toString());
System.exit(0);
}

//test:
//Class object = Class.forName("java.lang.reflect.Modifier");

Class object = Class.forName(args[0]);
printFields(object);
printConstructs(object);
printMethods(object);
}
}
zephyr_cc 2007-09-11
  • 打赏
  • 举报
回复
感动~ @_@
malligator 2007-09-11
  • 打赏
  • 举报
回复
java.lang.Integer是基本数据类型??

zephyr_cc的方法能区分int 和Integer,有何不好?(LZ不就是要“知道它的类型,特别是基本数据类型变量的类型”么)
bunrise 2007-09-11
  • 打赏
  • 举报
回复
不好意思修改以上代码:
public static void getType(Object object) {
int length = object.getClass().getName().lastIndexOf(".");
String type = object.getClass().getName().substring(length + 1);
System.out.println(type);
}
基本类型不能得到
cxxlp 2007-09-11
  • 打赏
  • 举报
回复
???????????????????????????????????????
bunrise 2007-09-11
  • 打赏
  • 举报
回复
public void getType(Object object){
int length= object.getClass().getClassName().lastIndexOf(".");
String type = object.getClass().getClassName().subSstring(length+1);
System.out.prinfln(type);
}
blliy117 2007-09-11
  • 打赏
  • 举报
回复
补充:
如果是基础类型的话用一楼的方法会得到其包装类.
blliy117 2007-09-11
  • 打赏
  • 举报
回复
1楼的是最佳的方法
fire4cwy 2007-09-11
  • 打赏
  • 举报
回复
貌似基本数据类型不可以~
只能知道某些变量来自哪个类
beibeiG 2007-09-11
  • 打赏
  • 举报
回复
zephyr_cc()

判断基本类型的是需要加上这些方法
beibeiG 2007-09-11
  • 打赏
  • 举报
回复
zephyr_cc() 写那么东西没用

obj.getClass().getName() ===〉 java.lang.Integer
obj.getClass().toString() ===〉 class java.lang.Integer

zephyr_cc 2007-09-11
  • 打赏
  • 举报
回复
楼主的意思不是要知道是不是基础类型嘛?
只用Object做参数你怎么区分基础类型和包装类?
oracle_chinayi 2007-09-11
  • 打赏
  • 举报
回复
同意一楼
加载更多回复(14)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧