473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

So everybody thinks this is normal?

11,448 Expert 8TB
I bumped my reply from another thread just because I don't believe that everybody
thinks this is normal. Have a close read:

I don't like autoboxing very much; below you'll see an example derived from an
original example by someone else posted in Sun's Java forum quite a while ago:

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. public class Autoboxing {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Object foo = new Long(0xcafebabedeadbeefL);
  8.         List<Object> bars = new ArrayList<Object>();
  9.  
  10.         bars.add(bool() ? (Long)foo : (Number)foo);
  11.         bars.add(bool() ? (Long)foo : (Long)foo);
  12.         bars.add(bool() ? (Long)foo : (Double)foo);
  13.         bars.add(bool() ? (Long)foo : ((Long)foo).longValue());
  14.  
  15.         System.out.print("==    :");
  16.         for (Object bar : bars)
  17.             System.out.print(" "+(foo == bar));
  18.         System.out.println();
  19.  
  20.         System.out.print("equals:");
  21.         for (Object bar : bars)
  22.             System.out.print(" "+foo.equals(bar));
  23.         System.out.println();
  24.     }
  25.  
  26.     private static boolean bool() {
  27.         return true;
  28.     }
  29. }

Output:

Expand|Select|Wrap|Line Numbers
  1. ==    : true true false false
  2. equals: true true false true
  3.  
That's why I don't like autoboxing very much ...

kind regards,

Jos
Sep 26 '07 #1
6 1570
r035198x
13,262 8TB
Is that what they were talking about in

Ideally, boxing a given primitive value p, would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques. The rules above are a pragmatic compromise. The final clause above requires that certain common values always be boxed into indistinguishable objects. The implementation may cache these, lazily or eagerly.

For other values, this formulation disallows any assumptions about the identity of the boxed values on the programmer's part. This would allow (but not require) sharing of some or all of these references.
Sep 26 '07 #2
JosAH
11,448 Expert 8TB
Is that what they were talking about in
Not really; it's the ternary operator that spoils all that nice hulla baloo:

The type of a conditional expression is determined as follows:


If the second and third operands have the same type (which may be the null type), then that is the type of the conditional expression.
If one of the second and third operands is of type boolean and the type of the other is of type Boolean, then the type of the conditional expression is boolean.
If one of the second and third operands is of the null type and the type of the other is a reference type, then the type of the conditional expression is that reference type.
Otherwise, if the second and third operands have types that are convertible (§5.1.8) to numeric types, then there are several cases:
If one of the operands is of type byte or Byte and the other is of type short or Short, then the type of the conditional expression is short.
If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression of type int whose value is representable in type T, then the type of the conditional expression is T.
If one of the operands is of type Byte and the other operand is a constant expression of type int whose value is representable in type byte, then the type of the conditional expression is byte.
If one of the operands is of type Short and the other operand is a constant expression of type int whose value is representable in type short, then the type of the conditional expression is short.
If one of the operands is of type; Character and the other operand is a constant expression of type int whose value is representable in type char, then the type of the conditional expression is char.
Otherwise, binary numeric promotion (§5.6.2) is applied to the operand types, and the type of the conditional expression is the promoted type of the second and third operands. Note that binary numeric promotion performs unboxing conversion (§5.1.8) and value set conversion (§5.1.13).
Otherwise, the second and third operands are of types S1 and S2 respectively. Let T1 be the type that results from applying boxing conversion to S1, and let T2 be the type that results from applying boxing conversion to S2. The type of the conditional expression is the result of applying capture conversion (§5.1.10) to lub(T1, T2) (§15.12.2.7).
At run time, the first operand expression of the conditional expression is evaluated first; if necessary, unboxing conversion is performed on the result; the resulting boolean value is then used to choose either the second or the third operand expression:

If the value of the first operand is true, then the second operand expression is chosen.
If the value of the first operand is false, then the third operand expression is chosen.
The chosen operand expression is then evaluated and the resulting value is converted to the type of the conditional expression as determined by the rules stated above. This conversion may include boxing (§5.1.7) or unboxing conversion. The operand expression not chosen is not evaluated for that particular evaluation of the conditional expression
A Long isn't a Long when one of the other operands happens to be a Double etc.

funny eh?

Jos
Sep 26 '07 #3
r035198x
13,262 8TB
Not really; it's the ternary operator that spoils all that nice hulla baloo:



A Long isn't a Long when one of the other operands happens to be a Double etc.

funny eh?

Jos
At least they wrote it down.
Determining the type of an expression was always going to involve some voodoo somewhere.
Sep 26 '07 #4
JosAH
11,448 Expert 8TB
At least they wrote it down.
Determining the type of an expression was always going to involve some voodoo somewhere.
Not in C or C++ and not in Java before that darn autoboxing that is. But I guess
most of the people take it all for granted until something goes wrong ;-)

kind regards,

Jos
Sep 26 '07 #5
r035198x
13,262 8TB
Not in C or C++ and not in Java before that darn autoboxing that is. But I guess
most of the people take it all for granted until something goes wrong ;-)

kind regards,

Jos
What? You expect them all to read (whatever that word means) that JLS?
Sep 26 '07 #6
JosAH
11,448 Expert 8TB
What? You expect them all to read (whatever that word means) that JLS?
Mwah, not me; you're the optimist of the group here ;-)

kind regards,

Jos
Sep 26 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Irmen de Jong | last post by:
Okay I tried some profiling, but am uncertain about the results I'm getting. They confuse the hell out of me. I have a test program (see below) that essentially has two loops that get called...
8
by: DraguVaso | last post by:
Hi, I'm new to WebServices, and I'm doing some tests (with a small VB.NET-application) to know the performance-difference between a WebService and the 'normal'-way of getting data (just...
1
by: SyracuseCheer | last post by:
I have a VS.NET 2003 project that's giving me issues. Any assistance would be greatly appreciated! I have a fairly straight-forward system of server controls and web services, where the server...
2
by: alex | last post by:
Hello Friends, Please go through the text. Bill Gates thinks Google should be worried! ------------------------------------------- You must have heard by now about Agloco and how many people...
4
by: alex | last post by:
Hello Friends, Please go through the text. Bill Gates thinks Google should be worried! ------------------------------------------- You must have heard by now about Agloco and how many people...
14
by: MLH | last post by:
After entering a date (earlier than today's date) into a textbox control though, I'm stumped as to why VBA thinks date I enter into the control is greater than Now - when they are CLEARLY less than...
3
by: cheesecaker | last post by:
For some reason, when PHP starts, it doesn't want to load some extensions that I KNOW are there. Here's the startup log: Parent: Received restart signal -- Restarting the server. Child 5176:...
1
by: Ben | last post by:
Hi, The gridview contains a radiobuttonlist with boolean values (true/false) coming from a database. In normal mode, the gridview displays True or False for that field. What i want is to...
0
by: awyeah | last post by:
Hello everyone, I have some very difficult task to accomplish using ADODB, or DAO in visual basic 6 whichever. This is my masters project and I am stuck!! Say I have a recordset with 4 customers...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.