An error in Java programmer's inertial thinking

Source: Internet
Author: User

I haven't accumulated things for a long time. I happened to encounter a problem a few days ago. Although it's not big but interesting, I will record it here and I will be able to serve as a test for other people like interview questions in the future, think about things that are far more practical than the things we criticized:

The table structure is as follows:

T_SOME_TABLE {

Crowid varchar (36 );

Zrmb float (7,3 );

Zjdw float (7,3 );

}

Which of the following two sections of code will cause an error? Why?

Code snippet 1:

// The background code is as follows:

String hqlStr = "select SUM (t. zrmb) AS SUM_1, SUM (t. zjdw) AS SUM_2 from T_SOME_TABLE t where 1 = 1 ";

List sumList = baseDao. find (hqlStr); // use hibernate to query the List of results returned by the HQL summary statement

Request. setAttribute ("sumList", sumList );

// The front-end code is as follows:

String sum1 = "";

String sum2 = "";

ArrayList sumList = request. getAttribute ("sumList") = null? Null :( ArrayList) request. getAttribute ("sumList ");

If (null! = SumList ){

For (int I = 0; I

Object [] tempObj = (Object []) sumList. get (I );

Sum1 = tempObj [0] = null? "0.0": tempObj [0]. toString ();

Sum2 = tempObj [1] = null? "0.0": tempObj [1]. toString ();

}

}

Out. prinln ("sum1:" + sum1 );

Out. prinln ("sum2:" + sum2 );

Code snippet 2:

// The background code is as follows:

String hqlStr = "select SUM (t. zrmb) AS SUM_1 from T_SOME_TABLE t where 1 = 1 ";

List sumList = baseDao. find (hqlStr); // use hibernate to query the List of results returned by the HQL summary statement

Request. setAttribute ("sumList", sumList );

// The front-end code is as follows:

String sum1 = "";

ArrayList sumList = request. getAttribute ("sumList") = null? Null :( ArrayList) request. getAttribute ("sumList ");

If (null! = SumList ){

For (int I = 0; I

Object [] tempObj = (Object []) sumList. get (I );

Sum1 = tempObj [0] = null? "0.0": tempObj [0]. toString ();

}

}

Out. prinln ("sum1:" + sum1 );

In actual operation, we will find that code segment 2 has an error and code segment 1 can run normally, in the process of function development, Segment 2 is implemented by inertial thinking on the basis of Segment 1, but the actual operation will find that the result is not what you want, this person with strong hands-on ability can debug the problem and quickly understand the problem. Here is a brief introduction:

All people who have done hibernate know that they use hibernate to call the summary statement of SQL query. The returned result is encapsulated into an Object and saved to the List. Compared with code 1 and Code 2, the difference is only about the number of fields. If more than two fields are encapsulated into an Object [] array, this is undisputed, however, if it is a field, the List stores the Object instead of the Object [] array.

In this way, we can infer that hibernate is working internally.

The Code 2 loop should be: www.2cto.com

Object tempObj = (Object) sumList. get (I );

Sum1 = tempObj = null? "0.0": tempObj. toString ();


Author: xi_hong_shi
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.