basetime=integer.parseint参数((String)b.getString("me

java - [Ljava.lang.O cannot be cast to - Stack Overflow
to customize your list.
Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
J it only takes a minute:
I want to get value from the database, in my case I use List to get the value from the database but I got this error
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.O cannot be cast to id.co.bni.switcherservice.model.SwitcherServiceSource
at id.co.bni.switcherservice.controller.SwitcherServiceController.LoadData(SwitcherServiceController.java:48)
at id.co.bni.switcherservice.controller.SwitcherServiceController.main(SwitcherServiceController.java:62)
this is my code
Query LoadSource = session_source.createQuery("select CLIENT,SERVICE,SERVICE_TYPE,PROVIDER_CODE,COUNT(*) FROM SwitcherServiceSource" +
" where TIMESTAMP between :awal and :akhir" +
" and PROVIDER_CODE is not null group by CLIENT,SERVICE,SERVICE_TYPE,PROVIDER_CODE order by CLIENT,SERVICE,SERVICE_TYPE,PROVIDER_CODE");
LoadSource.setParameter("awal", fromDate);
LoadSource.setParameter("akhir", toDate);
List&SwitcherServiceSource& result_source = (List&SwitcherServiceSource&) LoadSource.list();
for(SwitcherServiceSource tes : result_source){
System.out.println(tes.getSERVICE());
any help will be pleasure :)
@raffian, did you mean like this??
List&Switcher& result = (List&Switcher&) LoadSource.list();
for(Switcher tes : result){
System.out.println(tes.getSERVICE());
java.lang.ClassCastException: [Ljava.lang.O cannot be cast to id.co.bni.switcherservice.model.SwitcherServiceSource
Problem is
(List&SwitcherServiceSource&) LoadSource.list();
This will return a List of Object arrays (Object[]) with scalar values for each column in the SwitcherServiceSource table. Hibernate will use ResultSetMetadata to deduce the actual order and types of the returned scalar values.
List&Object& result = (List&Object&) LoadSource.list();
Iterator itr = result.iterator();
while(itr.hasNext()){
Object[] obj = (Object[]) itr.next();
//now you have one array of Object for each row
String client = String.valueOf(obj[0]); // don't know the type of column CLIENT assuming String
Integer service = Integer.parseInt(String.valueOf(obj[1])); //SERVICE assumed as int
//same way for all obj[2], obj[3], obj[4]
Related link
1,18321427
7,77973551
Your query execution will return list of Object[].
List result_source = LoadSource.list();
for(Object[] objA : result_source) {
// read it all
8,37621532
I've faced such an issue and dig tones of material. So, to avoid ugly iteration you can simply tune your hql:
You need to frame your query like this
select entity from Entity as entity where ...
Also check such case, it perfectly works for me:
public List&User& findByRole(String role) {
Query query = sessionFactory.getCurrentSession().createQuery("select user from User user join user.userRoles where role_name=:role_name");
query.setString("role_name", role);
@SuppressWarnings("unchecked")
List&User& users = (List&User&) query.list();
So here we are extracting object from query, not a bunch of fields. Also it's looks much more pretty.
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabled3675人阅读
java(73)
当我们需要将一个字符串转换成整型时可能会使用以下的方法:
Java代码 &
String&str&=&&12345&; &&int&one&=&Integer.parseInt(str); &&int&two&=&Integer.valueOf(str);&&
String str = &12345&;
int one = Integer.parseInt(str);
int two = Integer.valueOf(str);
使用parseInt()和valueOf()有何区别?我们来看JDK对这两个方法的解析:
public static int parseInt(&s)
将字符串参数作为有符号的十进制整数进行分析。除了第一个字符可以是用来表示负值的 ASCII 减号
'-' ('\u002D') 外,字符串中的字符都必须是十进制数字。返回得到的整数值,就好像将该参数和基数 10 作为参数赋予
方法一样。
参数: s - 包含要分析的 int 表示形式的 String。 返回: 用十进制参数表示的整数值。 抛出:
- 如果字符串不包含可分析的整数。
public static
valueOf(&s)
返回保持指定的 String 的值的 Integer 对象。将该参数解释为表示一个有符号的十进制整数, 就好像将该参数赋予
方法一样。结果是一个表示字符串指定的整数值的 Integer 对象。
换句话说,该方法返回一个等于以下值的 Integer 对象:
new Integer(Integer.parseInt(s))
参数: s - 要解析的字符串。 返回: 保持字符串参数表示的值的 Integer 对象。 抛出:
- 如果字符串不能分析为一个整数。
public static
valueOf(int&i)
返回一个表示指定的 int 值的 Integer 实例。如果不需要新的 Integer 实例,则通常应优先使用该方法,而不是构造方法
,因为该方法有可能通过缓存经常请求的值而显著提高空间和时间性能。
参数: i - 一个 int 值。 返回: 表示 i 的 Integer 实例。 从以下版本开始: 1.5
&& 4.简单地说就是:
static int parseInt(String s):&&&&&& 将字符串参数作为有符号的十进制整数进行分析,返回一个int值。
static Integer valueOf(int i)&:&&&&& 返回一个表示指定的 int 值的 Integer 实例。
static Integer valueOf(String s)&:&&&&& 返回保持指定的 String 的值的 Integer 对象(实例)。
&&& 从返回值可以看出它们的区别&: parseInt()返回的是基本类型int, 而valueOf()返回的是包装类Integer。Integer可以使用对象方法, 而int类型不能和Object类型进行互相转换。
& 既然 static Integer valueOf(String s)返回的是一个Integer对象,却可以像这样用:
&& int i = Integer.valueOf(&2012&); 因为JDK1.5后的自动装箱和拆箱操作使用Integer和int可以自动转换,就像Integer i = 5;&& int j& =一样在编译时是没有错误的
& 5.从JDK的解析中我们能发现一个问题:
&& 当使用parseInt(String str)方法时如果字符串str中的字符有某一个不是十进制的数字时就会抛出NumberFormatException异常,例如对身份证号码242586X使用parseInt(String str)进行转换时就会抛出NumberFormatException ,解决的方法就是改用Integer.valueOf(String
str)方法(现在好像也不行了,未测试过),因为Integer.valueOf(String str)方法返回的是一个保持字符串参数表示的值的
Integer 对象, Integer.parseInt(String str)返回一个基本类型。但是Integer.valueOf(String str)方法对完全非数字的字符串也是无能为力的,例如使用Integer.valueOf(String str)方法对 str = &abc&进行转换进就直接报NumberFormatException 异常。
&& 可以看下面的例子:
设有下面两个赋值语句:
a = Integer.parseInt(“123”);
b = Integer.valueOf(“123”).intValue();
下述说法正确的是(& d& )。
A、a是整数类型变量,b是整数类对象。
B、a是整数类对象,b是整数类型变量。
C、a和b都是整数类对象并且值相等。&
D、a和b都是整数类型变量并且值相等。
详细解析:
parseInt(String s )方法是类Integer的静态方法,它的作用就是将形参 s 转化为整数,比如:
Interger.parseInt(&1&)=1;
Integer.parseInt(&20&)=20;
Integer.parseInt(&324&)=324;
当然,s 表示的整数必须合法,不然是会抛异常的。
valueOf(String s )也是Integer类的静态方法,它的作用是将形参 s 转化为Integer对象,Integer对象就是基本数据类型int型包装类,就是将int包装成一个类,这样在很多场合下是必须的。Interger.valueOf(&123&)=Integer(123)这时候Integer(123)就是整数123的对象表示形式,它再调用intValue()方法,就是将123的对象表示形式转化为int基本数据123,所以,选择D
6.关于整型的比较还有特别要说明的地方:
& 首先,定义一个整型的方式有:
& (1) &int i = 10;
&& (2)&&& Integer j =& new Integer(10);
&& (3)&&&&Integer k = Integer.valueOf(10);&&
& 我们来比较以下的结果:
Java代码 &
   Integer&a=Integer.valueOf(<span style="color:#c0);&&& &&&&&&&&&&Integer&b=Integer.valueOf(<span style="color:#c0);&&& &&&& &&&&&&&&&&Integer&aa=Integer.valueOf(<span style="color:#c0);&&& &&&&&&&&&&Integer&bb=Integer.valueOf(<span style="color:#c0);&&& &&&&&& &&&& &&  &&Integer&c=&<span style="color:#c0;&&& &&&&&&&&&&Integer&d=<span style="color:#c0;&&&&&&&&&&&& &&&&&&& &&&& &&  &&Integer&cc=&<span style="color:#c0;&&& &&&&&&&&&&Integer&dd=<span style="color:#c0;&&& &&&&&&&&&&&&& &&  &Integer&e=&new&Integer(<span style="color:#c0);&&& &&&&&&&&&&Integer&&f=new&Integer(<span style="color:#c0);&&& &&&&&&&&&&&&& &&&&&&&&&&&&& &&&&&&&&&&System.out.println(&(a==b)&?&&&#43;(a==b));&&& &&&&&&&&&&System.out.println(&(c==d)&?&&&#43;(c==d));&&& &&&&&&&&&&System.out.println(&(aa==bb)&?&&&#43;(aa==bb));&&& &&&&&&&&&&System.out.println(&(cc==dd)&?&&&#43;(cc==dd));&&& &&&&&&&&&&System.out.println(&(e==f)&?&&&#43;(e==f));&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&
&&&&&&&&&&&&
   Integer a=Integer.valueOf(127);
Integer b=Integer.valueOf(127);
Integer aa=Integer.valueOf(128);
Integer bb=Integer.valueOf(128);
Integer c= 127;
Integer d=127;
Integer cc= 128;
Integer dd=128;
   Integer e= new Integer(234);
f=new Integer(234);
System.out.println(&(a==b) ? &+(a==b));
System.out.println(&(c==d) ? &+(c==d));
System.out.println(&(aa==bb) ? &+(aa==bb));
System.out.println(&(cc==dd) ? &+(cc==dd));
System.out.println(&(e==f) ? &+(e==f));
//输出结果如下 :
(a==b) ? true
(c==d) ? true
(aa==bb) ? false
(cc==dd) ? false
(e==f) ? false
解析如下:
(1)new产生的Integer对象
new声明的就是要生成一个新的对象,这是两个对象,地址肯定不等,比较结果为false。
(2)int i=10与使用valueOf()方法的效果是相同的,我们来看JDK中关于Integer.valueOf的实现代码:
Java代码 &
public&static&Integer&valueOf(int&i)&{&&&& &&&&&&&final&int&offset&=&<span style="color:#c0;&& &&&&&&&if&(i&&=&-<span style="color:#c0&&&&i&&=&<span style="color:#c0)&{ &&&&&&&&&&&&&&&&&&&&&&&return&IntegerCache.cache[i&&#43;&offset];& &&&&&&&&} &&&&&&&return&new&Integer(i); &&&}&&&
public static Integer valueOf(int i) {
final int offset = 128;
if (i &= -128 && i &= 127) {
// must cache
return IntegerCache.cache[i + offset];
return new Integer(i);
& 这段代码的意思已经很明了了,如果是-128到127之间的int类型转换为Integer对象,则直接从cache数组中获得,那 cache数组里是什么东西,代码如下:
Java代码 &
static&final&Integer&cache[]&=&new&Integer[-(-<span style="color:#c0)&&#43;&<span style="color:#c0&&#43;&1];&
&&static&{&& &&&&&for(int&i&=&0;&i&&&cache.&i&#43;&#43;) &&&&&&cache[i]&=&new&Integer(i&-&<span style="color:#c0); &&&}&&&
static final Integer cache[] = new Integer[-(-128) + 127 + 1];
for(int i = 0; i & cache. i++)
cache[i] = new Integer(i - 128);
& cache是IntegerCache内部类的一个静态数组,容纳的是﹣128到127之间的Integer对象。通过valueOf产生包装对象时,如果int参数在﹣128和127之间,则直接从整型池中获得对象,不在该范围的int类型则通过new生成包装对象。
& 127的包装对象是直接从整型池中获得的,不管你输入多少次127这个数字,获得的对象都是同一个,那地址当然都是相等的。而128、555超出了整型池范围,是通过new产生一个新的对象,地址不同,当然也就不相等了。
& 1./art/488.htm
& 2.http://xinxinyin666./blog/view/.htm
Integer.valueof(String s)是将一个包装类是将一个实际&#20540;为数字的变量先转成string型再将它转成Integer型的包装类对象(相当于转成了int的对象)这样转完的对象就具有方法和属性了。
Integer.parseInt(String s)只是将是数字的字符串转成数字,注意他返回的是int型变量不具备方法和属性。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:417483次
积分:5235
积分:5235
排名:第3924名
原创:81篇
转载:233篇
评论:29条
微信公众平台:胖蛇的异想世界
扫一扫,也不会怀孕
(2)(3)(13)(1)(1)(4)(4)(16)(10)(4)(11)(4)(10)(10)(6)(15)(44)(22)(5)(8)(18)(15)(4)(2)(11)(7)(3)(16)(23)(4)(2)(1)(1)(2)(3)(12)From Wikipedia, the free encyclopedia
This article has multiple issues. Please help
or discuss these issues on the . ()
This article may need to be rewritten entirely to comply with Wikipedia's . . The
may contain suggestions. (February 2013)
This article needs additional citations for . Please help
by . Unsourced material may be challenged and removed. (February 2010) ()
A computer number format is the internal representation of numeric values in digital
hardware and software. Normally, numeric values are stored as groupings of , named for the number of bits that compose them. The encoding between numerical values and bit patterns is chosen for convenience of the opera the bit format used by the computer's instruction set generally requires conversion for external use such as printing and display. Different types of processors may have different internal representations of numerical values. Different conventions are used for integer and real numbers. Most calculations are carried out with number formats that fit into a processor register, but some software systems allow representation of arbitrarily large numbers using multiple words of memory.
Computers represent data in sets of binary digits. The representation is composed of bits, which in turn are grouped into larger sets such as bytes.
Table 1: Binary to Octal
Binary String
Octal value
Table 2: Number of values for a bit string.
Length of Bit String (b)
Number of Possible Values (N)
{\displaystyle b}
{\displaystyle 2^{b}=N}
that represents one of two . The concept of a bit can be understood as a value of either 1 or 0, on or off, yes or no, true or false, or
by a switch or
of some kind.
While a single bit, on its own, is able to represent only two values, a
may be used to represent larger values. For example, a string of three bits can represent up to eight distinct values as illustrated in Table 1.
As the number of bits composing a string increases, the number of possible 0 and 1 combinations increases . While a single bit allows only two value-combinations and two bits combined can make four separate values and so on. The amount of possible combinations doubles with each binary digit added as illustrated in Table 2.
Groupings with a specific number of bits are used to represent varying things and have specific names.
is a bit string containing the number of bits needed to represent a . On most modern computers, this is an eight bit string. Because the definition of a byte is related to the number of bits composing a character, some older computers have used a different bit length for their byte. In many , the byte is used to
specific areas of memory. For example, even though 64-bit processors may address memory sixty-four bits at a time, they may still split that memory into eight-bit pieces. This is called byte-addressable memory. Historically, many
read data in some multiple of eight bits. Because the byte size of eight bits is so common, but the definition is not standardized, the term
is sometimes used to explicitly describe an eight bit sequence.
(sometimes nybble), is a number composed of four bits. Being a , the nibble was named as a play on words. A person may need several nibbles for one similarly, a nybble is a part of a byte. Because four bits allow for sixteen values, a nibble is sometimes known as a .
Octal and hex are convenient ways to represent binary numbers, as used by computers. Computer engineers often need to write out binary quantities, but in practice writing out a binary number such as 0001 is tedious and prone to errors. Therefore, binary quantities are written in a base-8, or "octal", or, much more commonly, a base-16, "hexadecimal" or "hex", number format. In the decimal system, there are 10 digits, 0 through 9, which combine to form numbers. In an octal system, there are only 8 digits, 0 through 7. That is, the value of an octal "10" is the same as a decimal "8", an octal "20" is a decimal "16", and so on. In a hexadecimal system, there are 16 digits, 0 through 9 followed, by convention, with A through F. That is, a hex "10" is the same as a decimal "16" and a hex "20" is the same as a decimal "32". An example and comparison of numbers in different bases is described in the chart below.
When typing numbers, formatting characters are used to describe the number system, for example 000_0000B or 0b000_00000 for binary and 0F8H or 0xf8 for hexadecimal numbers.
Table 3: Comparison of Values in Different Bases
Decimal Value
Binary Value
Octal Value
Hexadecimal Value
Main article:
Each of these number systems are positional systems, but while decimal weights are powers of 10, the octal weights are powers of 8 and the hex weights are powers of 16. To convert from hex or octal to decimal, for each digit one multiplies the value of the digit by the value of its position and then adds the results. For example:
octal&#xA0;
{\displaystyle {\text{octal }}756}
{\displaystyle =(7*8^{2})+(5*8^{1})+(6*8^{0})}
{\displaystyle =(7*64)+(5*8)+(6*1)}
{\displaystyle =448+40+6}
decimal&#xA0;
{\displaystyle ={\text{decimal }}494}
{\displaystyle {\text{hex }}3b2}
{\displaystyle =(3*16^{2})+(11*16^{1})+(2*16^{0})}
{\displaystyle =(3*256)+(11*16)+(2*1)}
{\displaystyle =768+176+2}
decimal&#xA0;
{\displaystyle ={\text{decimal }}946}
formatting can be useful to represent fractions in binary.
The number of bits needed for the precision and range desired must be chosen to store the fractional and integer parts of a number. For instance, using a 32-bit format, 16 bits may be used for the integer and 16 for the fraction.
The eight's bit is followed by the four's bit, then the two's bit, then the one's bit. The fractional bits continue the pattern set by the integer bits. The next bit is the half's bit, then the quarter's bit, then the
1/8 's bit, and so on. For example:
integer bits
fractional bits
1&#160;1/4
7&#160;3/8
This form of encoding cannot represent some values in binary. For example, the fraction
{\displaystyle {\tfrac {1}{5}}}
, 0.2 in decimal, the closest approximations would be as follows:
13107 / 65536
0.1999969... in decimal
13108 / 65536
0.2000122... in decimal
Even if more digits are used, an exact representation is impossible. The number
{\displaystyle {\tfrac {1}{3}}}
, written in decimal as 0...., continues indefinitely. If prematurely terminated, the value would not represent
{\displaystyle {\tfrac {1}{3}}}
precisely.
While both unsigned and signed integers are used in digital systems, even a 32-bit integer is not enough to handle all the range of numbers a calculator can handle, and that's not even including fractions. To approximate the greater range and precision of , we have to abandon signed integers and fixed-point numbers and go to a "" format.
In the decimal system, we are familiar with floating-point numbers of the form ():
1.1030402 × 105 = 1.1030402 × 100000 =
or, more compactly:
which means "1.1030402 times 1 followed by 5 zeroes". We have a certain numeric value (1.1030402) known as a "", multiplied by a power of 10 (E5, meaning 105 or 100,000), known as an "". If we have a negative exponent, that means the number is multiplied by a 1 that many places to the right of the decimal point. For example:
2.3434E-6 = 2.3434 × 10-6 = 2.3434 × 0.000001 = 0.
The advantage of this scheme is that by using the exponent we can get a much wider range of numbers, even if the number of digits in the significand, or the "numeric precision", is much smaller than the range. Similar binary floating-point formats can be defined for computers. There are a number of such schemes, the most popular has been defined by
(IEEE). The
standard specification defines a 64 bit floating-point format with:
an 11-bit binary exponent, using "excess-1023" format. Excess-1023 means the exponent appears as an unsigned binary integer from 0 to 2047; subtracting 1023 gives the actual signed value
a 52-bit significand, also an unsigned binary number, defining a fractional value with a leading implied "1"
a sign bit, giving the sign of the number.
Let's see what this format looks like by showing how such a number would be stored in 8 bytes of memory:
where "S" denotes the sign bit, "x" denotes an exponent bit, and "m" denotes a significand bit. Once the bits here have been extracted, they are converted with the computation:
&sign& × (1 + &fractional significand&) × 2&exponent& - 1023
This scheme provides numbers valid out to about 15 decimal digits, with the following range of numbers:
1.231E+308
4.465E-324
-4.465E-324
-1.231E+308
The specification also defines several special values that are not defined numbers, and are known as , for "Not A Number". These are used by programs to designate invalid operations and the like.
Some programs also use 32-bit floating-point numbers. The most common scheme uses a 23-bit significand with a sign bit, plus an 8-bit exponent in "excess-127" format, giving seven valid decimal digits.
The bits are converted to a numeric value with the computation:
&sign& × (1 + &fractional significand&) × 2&exponent& - 127
leading to the following range of numbers:
Such floating-point numbers are known as "reals" or "floats" in general, but with a number of variations:
A 32-bit float value is sometimes called a "real32" or a "single", meaning "single-precision floating-point value".
A 64-bit float is sometimes called a "real64" or a "double", meaning "double-precision floating-point value".
The relation between numbers and bit patterns is chosen for convenience in c eight bytes stored in computer memory may represent a 64-bit real, two 32-bit reals, or four signed or unsigned integers, or some other kind of data that fits into eight bytes. The only difference is how the computer interprets them. If the computer stored four unsigned integers and then read them back from memory as a 64-bit real, it almost always would be a perfectly valid real number, though it would be junk data.
Only a finite range of real numbers can be represented with a given number of bits. Arithmetic operations can overflow or underflow, producing a value too large or too small to be represented.
The representation has a limited precision. For example, only 15 decimal digits can be represented with a 64-bit real. If a very small floating-point number is added to a large one, the result is just the large one. The small number was too small to even show up in 15 or 16 digits of resolution, and the computer effectively discards it. Analyzing the effect of limited precision is a well-studied problem. Estimates of the magnitude of round-off errors and methods to limit their effect on large calculations are part of any large computation project. The precision limit is different from the range limit, as it affects the significand, not the exponent.
The significand is a binary fraction that doesn't necessarily perfectly match a decimal fraction. In many cases a sum of reciprocal powers of 2 does not matches a specific decimal fraction, and the results of computations will be slightly off. For example, the decimal fraction "0.1" is equivalent to an infinitely repeating binary fraction: 0. ...
Programming in
requires the programmer to keep track of the representation of numbers. Where the processor does not support a required mathematical operation, the programmer must work out a suitable algorithm and instruction sequence to car on some microprocessors, even integer multiplication must be done in software.
High-level
offer an abstract number that may be an expanded type such as rational, bignum, or complex. Mathematical operations are carried out by library routines provided by the implementation of the language. A given mathematical symbol in the source code, by , will invoke different object code appropriate to the representation o mathematical operations on any number—whether signed, unsigned, rational, floating-point, fixed-point, integral, or complex—are written exactly the same way.
Some languages, such as
and , provide decimal floating points operations, which provide rounding errors of a different form.
The initial version of this article was based on a
article from .
Jon Stokes (2007). . No Starch Press. p.&#160;66. &#160;.
. Network Dictionary 2012.
Goebel, Greg.
: Hidden categories:

我要回帖

更多关于 integer.parseint 的文章

 

随机推荐