Casting primitive wrap type variable to primitive

We have this code:
float f = 22.2f;
System.out.println((int)f);
Which prints out "22".
Float f = 22.2f;
System.out.println((int)f);
When i changed the variabile type from float to Float, code throw up a compile a error, that Float type cannot be casted to int. Can somebody tell me what's going on? Isnt compiler supposed to do unboxing the variable to primitive float and then primitive float to be casted to an int?
System.out.println((int)f.floatValue());
Isnt that what the compiler is supposed to do? Code which works perfectly fine.
Thank for your help.

Hello vcraescu,
I think you are wondering why the compiler does not convert from Float to float (unboxing) and subsequently from float to short (your cast), so the bug report probably does not help you too much.
Casts come into play when the compiler can not assure that the statement is semantically correct. The bytecode would be the same with or without the cast (if the compiler would accept omitting casts).
Casting primitives:
int i = 0;
short s = (short)i;Purpose of the cast: The compiler has no idea whether the two higher bytes are relevant and you therefore need to specify that they are not.
Casting reference types:
With reference types there are two types of casts, the upcast and the downcast.
class Exampe {
     public static void example(Object... objects) {
          for (Object object : objects) {
               System.out.print(object);
               System.out.print(' ');
          System.out.println();
     public static void main(String[] args) {
          Object[] objects = { "String", 123, 1.23 };
          example(objects);
          example((Object) objects);
}The purpose of this (up)cast is that you want the object to be treated as beeing of a more general type.
org.w3c.dom.Node node;
switch(node.getNodeType()) {
     case org.w3c.dom.Node.ELEMENT_NODE:
          org.w3c.dom.Element element = (org.w3c.dom.Element) node;
     case org.w3c.dom.Node.TEXT_NODE:
          org.w3c.dom.Text element = (org.w3c.dom.Text) node;
     default:
}These casts tell the compiler that you are certain the object node is of a more specific type.
My point is that all these casts have a distinct purpose. Your cast on the other hand does not. Looking at your code I see a conversion from a wrapper to a primitive, not necessarily the loss of precision connected with it and that is what this cast should be about.
With kind regards
Ben Schulz

Similar Messages

  • Unable to cast object of type 'Oracle.DataAccess.Types.OracleDecimal'......

    I have some Oracle Tables with sequences for primary key and stored procs in packages to wrap up the insert commands. The sequences field are all declared as NUMBER.
    I also have Datasets based on the tables and a DataAdapter for each package. The Datasets see the primary keys as System.Decimal. The DataAdapter sees the output primary key parameter to the stored procs as OracleDecimal.
    tmp.Parameters.Add(new OracleParameter("P_ID", Oracle.DataAccess.Client.OracleDbType.Decimal, ParameterDirection.Output));
    tmp.Parameters["P_ID"].SourceColumn = "ID";
    When I call the Update on the DataAdapter the update happens on the DB and then I get the following error
    System.ArgumentException : Unable to cast object of type 'Oracle.DataAccess.Types.OracleDecimal' to type 'System.IConvertible'.Couldn't store <231> in ID Column. Expected type is Decimal.
    ----> System.InvalidCastException : Unable to cast object of type 'Oracle.DataAccess.Types.OracleDecimal' to type 'System.IConvertible'.
    If I change the Oracle parameter to Oracle.DataAccess.Client.OracleDbType.Int32 or Oracle.DataAccess.Client.OracleDbType.Int64 it works fine - any ideas why that would be ? I would expect System.Decimal to map to Oracle.DataAccess.Types.OracleDecimal.

    Hi,
    If I change the Oracle parameter to Oracle.DataAccess.Client.OracleDbType.Int32 or Oracle.DataAccess.Client.OracleDbType.Int64 it works fine - any ideas why that would be ? I would expect System.Decimal to map to Oracle.DataAccess.Types.OracleDecimal.
    I'm trying to do the same, but no matter what I do, I get the OracleDecimal error. Parameter is defined as:
    bq. this._adapter.InsertCommand = new global::Oracle.DataAccess.Client.OracleCommand(); \\ this._adapter.InsertCommand.Connection = this.Connection; \\ this._adapter.InsertCommand.CommandText = "INSERT INTO PERSON\r\n                      (ID, SURNAME, NAME, BIRTHCITY, EMSO)\r\nV" + \\ +"ALUES (:ID, :SURNAME, :NAME, :BIRTHCITY, :EMSO) RETURNING ID INTO :ID";+ \\ this._adapter.InsertCommand.CommandType = global::System.Data.CommandType.Text; \\ param = new global::Oracle.DataAccess.Client.OracleParameter(); \\ param.ParameterName = "ID"; \\ param.DbType = global::System.Data.DbType.Int32; \\ param.OracleDbType = global::Oracle.DataAccess.Client.OracleDbType.Int32; \\ param.Direction = global::System.Data.ParameterDirection.Output; \\ param.IsNullable = true; \\ param.SourceColumn = "ID"; \\ this._adapter.InsertCommand.Parameters.Add(param);
    But no luck...

  • Unable to cast object of type OracleXmlType to type XmlDocument

    Hello All:
    I have an Oracle Procedure that is taking an XML Document as an output parameter.
    oCommand.Parameters.Add("errorrecord", OracleDbType.XmlType).Value = System.DBNull.Value;
    oCommand.Parameters["errorrecord"].Direction = System.Data.ParameterDirection.Output;
    When I try to cast this as an XmlDocument so I can set it to my ErrorRecord variable (defined as XmlDocument) and pass it back out of the Web-Service
    ErrorRecord = (XmlDocument)oCommand.Parameters["p_errorrecord"].Value;
    I get the following error: "Unable to cast object of type 'Oracle.DataAccess.Types.OracleXmlType' to type 'System.Xml.XmlDocument'"
    How do I cast / convert the Oracle XMLType back to a .Net XMLDocument to pass out of the function?
    Thanks

    No, I have not tried that yet, but I admit I don't fully understand the syntax in the document posted.
    oCommand.Parameters.Add("p_errorrecord", OracleDbType.XmlType).Value = System.DBNull.Value;
    ErrorRecord = GoCommand.Parameters["errorrecord"].Value; (this is returned as XmlType)
    I don't quite understand the syntax in the posted URL:
    Declaration
    // C#
    public XmlDocument GetXmlDocument();
    How am I to use this to get the XMLDocument?

  • Casting of Data Types

    Hi All ,
    I want to know if casting of data types is possible in ABAP. I mean if I have declared a datatype as
    data : x type char5.
    and Now I want to make x as char10.
    Is there any method to achieve this?
    Thanks and regards
    Sujeet

    Hi Sujeet
    If your requirement suits, you can use dynamic programming which is a nice feature of ABAP.
    You can use field-symbols (think them of as aliases for fields) or generic variables.
    For more information I recommend the e-class <a href="https://www.sdn.sap.com/sdn/elearning.sdn?class=/public/eclasses/teched04/ABAP351.htm">"Advanced and Generic Programming in ABAP"</a>.
    As a last thing, let me introduce you the SDN forums pointing system: You can assign points to posts which help you solving your problem. You can reward points by clicking the yellow star icon at header of each post. You can assign:
    - one 10 points (solved)
    - two 6 points (very helpful answer)
    - many 2 points (helpful answer)
    Kind regards...
    *--Serdar

  • RMS error - Unable to cast object of Type 'System.Byte[]' to type 'System.String'

    Hi,
    I'm experiencing an issue when trying to connect to RMS cluster through server manager. Error is:
    Active Directory Rights Management Services could not complete the task because an error occurred. This error may be caused by variable conditions.
    Details:
    Exception message:
    Unable to cast object of type 'System.Byte[]' to type 'System.String'.
    Exception type:
    System.Web.Services.Protocols.SoapExtension (Inner Exception: System.InvalidCastException)
    I've re-installed server, I've reinstalled RMS role, I've tried with and w/o SSL connection to RMS but the error persists. OS is 2008R2 (updated), database is internal (test environment)... 
    Any suggestions?

    Actually there was not problem witg the laptop ! Please ignore this topic. Thanks.

  • Assigning value to array type variable in a loop

    Hi
    I have a scenario in which i am assigning value from a array type variable(x) to the invoke variable of a database adapter. The variable x is exact replica of the invoke variable.
    My copy operation in assign activity looks like this-
    <copy>
    <from variable="Var" part="InputParameters"
    query="/ns7:InputParameters"/>
    <to variable="Invoke_call_XXDPI_EDI_852_PKG_InputVariable"
    part="InputParameters"
    query="/ns7:InputParameters *(* bpws:getVariableData('iterator') *)* "/>
    </copy>
    It is inside a while loop activity.
    PS *()* are square brackets
    But it is erroring out at run time.
    Does anybody has an alternate idea to assign value to an array type?.
    i have seen that while extracting value from an array type variable it works fine
    A similar kind of operation is shown below.
    <copy>
    <from variable="Var" part="InputParameters"
    query="/ns7:InputParameters *(* bpws:getVariableData('iterator') *)* "/>
    <to variable="Invoke_call_XXDPI_EDI_852_PKG_InputVariable"
    part="InputParameters"
    query="/ns7:InputParameters"/>
    </copy>
    Thanks
    Ayush
    Edited by: Ayush fujitsu on Aug 14, 2009 4:36 AM

    Hi Ayush
    I suppose you are getting some error like "source node returns multiple elements".
    In second case there is no problem because you are assigning *InputParameters[bpws:getVariableData('iterator')]* (+suppose InputParameters[1]+) to target. Here it works fine because you are fetching oonly 1 value from source and assigning it to the target.
    Now in first case you are saying copy InputParameters to target[1] suppose. You know that source is an array which contains multiple index so which index field from the source will be assigned to the target.
    Try your process with only 1 source value it will work but when multiple values will be there it will fail. You have to merge both the cases and it will look like
    *<copy>*
    *<from variable="Var" part="InputParameters"*
    query="/ns7:InputParameters ( bpws:getVariableData('iterator') ) "/>
    *<to variable="Invoke_call_XXDPI_EDI_852_PKG_InputVariable"*
    part="InputParameters"
    query="/ns7:InputParameters ( bpws:getVariableData('iterator') ) "/>
    *</copy>*
    And the easiest way to do this is by the transform activity as said above.
    Regards
    Suryaveer
    Edited by: Suryaveer on Aug 15, 2009 11:19 PM

  • Text wrap type syntax is not supported in Indesign CS4

    Dear all,
    Text wrap type syntax is not supported in Indesign CS4. How to set the text wrap type in indesign CS4

    Hey!
    app.selection[0].textWrapPreferences.textWrapMode = TextWrapModes.CONTOUR;
    TextWrapModes.BOUNDING_BOX_TEXT_WRAP
    Wraps text around the object's bounding box.
    1651729523 = 'bsds'
    TextWrapModes.CONTOUR
    Wraps text around the object following the specified contour options.
    1835233134 = 'mcon'
    TextWrapModes.JUMP_OBJECT_TEXT_WRAP
    Forces text to jump above or below the object, so that no text appears on  the object's right or left.
    1650552420 = 'band'
    TextWrapModes.NEXT_COLUMN_TEXT_WRAP
    Forces text to jump to the next available column.
    1853384306 = 'nxfr'
    TextWrapModes.NONE
    No text wrap.
    1852796517 = 'none'
    tomaxxi

  • How to convert element to mesasge type variable in BPEL

    Hi,
    I have xml message in element type variable. I want to create one variable of message type and have same elements values what element type variable has.
    I modified my xsd and created of same type like element namespace. Now when i use assign activity to copy from top root another top root. I get error Variable not initialized.
    How to correct?
    My XML message is like
    <CMISAPPL>
    <APPLICANT_FULL_NAME>Abhi</APPLICANT_FULL_NAME>
    <MANAGER_FULL_NAME>Jack</MANAGER_FULL_NAME>
    <INTERVIEWER_FULL_NAME>jack</INTERVIEWER_FULL_NAME>
    <JOB_TITLE>Test</JOB_TITLE>
    <INTERVIEW_DATE>2-Oct-2007</INTERVIEW_DATE>
    <PASSED_DAYS>5</PASSED_DAYS>
    <INT_SCH_PROFILE_VALUE>1,2,4,7</INT_SCH_PROFILE_VALUE>
    <FILE_PATH_PROFILE_VALUE/>
    <PAPERWORK_REQ_SEGMENTS_VALUE/>
    <PAPERWORK_REQ_SEGMENTS_NAME>App test</PAPERWORK_REQ_SEGMENTS_NAME>
    <VACANCY_NAME>test</VACANCY_NAME>
    <INTERVIEWER_EMAIL>t</INTERVIEWER_EMAIL>
    <INTERVIEWER_MANAGER_EMAIL>t</INTERVIEWER_MANAGER_EMAIL>
    </CMISAPPL>

    Looks like a namespace issue. There is no namespace defined in your payload.
    Marc
    http://orasoa.blogspot.com

  • Assign value to Object type variable

    CREATE OR REPLACE TYPE emp AS OBJECT (
    empid NUMBER,
    age NUMBER,
    dob date);
    CREATE OR REPLACE TYPE emp _tab AS TABLE OF emp;
    Pls hlep me assign value to the object type variable in loop..(assume there is 5 10 records in emp table)
    declare
    v_emp_tt emp _tab ;
    begin
    for i in (select empid ,age ,dob from emp ) Loop
    v_emp_tt := i.empid; --this is wrong pls help me correct it.
    end loop;
    end;
    thanks,

    I would keep the type/object naming convention distinct from the table name.
    In terms of assignment:
    CREATE OR REPLACE TYPE to_emp AS OBJECT
    (empid NUMBER,
    age NUMBER,
    dob date);
    CREATE OR REPLACE TYPE tt_emp AS TABLE OF emp;
    declare
    v_emp tt_emp;
    begin
    select to_emp_obj(emp_id, age, dob)
    bulk collect into v_emp
    from emp;
    end;

  • How to Populate a table type variable from a cursor

    Hi
    I have a stored procedure (P1) that returns a ref cursor as the output.
    Another procedure (P2) receives this ref cursor (C).
    In this procedure (P2), I want to do a Bulk Collect from this ref cursor (C) in
    a table type variable that has been declared locally in the procedure P2. I have created appropriate Object Type and Table Types at the database level.
    Please advise how to do it. I tried to do it in different ways, but was not able to do it - each time I faced incompatible data-type related issues.
    Regards
    Madhup

    What I wrote was unclear. Syntactically it is valid and does something. But consider the advantage of a decent design.
    SQL> create or replace procedure p1 (o out sys_refcursor) as
      2  begin
      3   open o for select * from emp;
      4  end p1;
      5  /
    Procedure created.
    SQL> create or replace procedure p2(i sys_refcursor) as
      2   type emp_tab is table of emp%rowtype;
      3   l_emp_tab emp_tab;
      4  begin
      5   fetch i bulk collect into l_emp_tab;
      6   close i;
      7  
      8   for i in 1..l_emp_tab.count loop
      9     NULL;
    10   end loop;
    11  end p2;
    12  /
    Procedure created.
    SQL> CREATE OR REPLACE PROCEDURE p3 IS
      2 
      3  TYPE myarray IS TABLE OF emp%ROWTYPE;
      4  l_data myarray;
      5 
      6  CURSOR r IS
      7  SELECT * FROM emp;
      8 
      9  BEGIN
    10    OPEN r;
    11    LOOP
    12      FETCH r BULK COLLECT INTO l_data;
    13 
    14      FOR j IN 1 .. l_data.COUNT
    15      LOOP
    16        NULL;
    17      END LOOP;
    18 
    19      EXIT WHEN r%NOTFOUND;
    20    END LOOP;
    21    CLOSE r;
    22  END p3;
    23  /
    Procedure created.
    SQL> set serverout on
    SQL> set timing on
    SQL> declare
      2   r sys_refcursor;
      3  begin
      4    FOR i IN 1 .. 10000 LOOP
      5      p1(r);
      6      p2(r);
      7    END LOOP;
      8  end;
      9  /
    PL/SQL procedure successfully completed.
    Elapsed: 00:00:01.71
    SQL> begin
      2    FOR i IN 1 .. 10000 LOOP
      3      p3;
      4    END LOOP;
      5  end;
      6  /
    PL/SQL procedure successfully completed.
    Elapsed: 00:00:01.21
    SQL> Again sorry for being less than clear.

  • @ Decimal Places in Packed Type Variable

    How to check if a packed type variable has decimal placed or not?

    Hi,
    Try to use the statement DESCRIBE. See the help to understand the usage. The syntax is:
    DESCRIBE FIELD <field> DECIMALS <dec>.
    This will show the number of decimals place.
    Then, you can use a condition to test.
    Regards
    RP

  • How to pass table type variable into function from SQL*PLUS ?

    How to pass a table type variable from sql*plus prompt into a function ?
    Thanx in advance.

    Krishna,
    Do you mean like this?SQL> DECLARE
      2      TYPE t_tbl IS TABLE OF VARCHAR2(20);
      3      l_sample_tbl           t_tbl;
      4
      5      FUNCTION print_contents ( p_tbl IN t_tbl )
      6      RETURN VARCHAR2
      7      IS
      8          l_string            VARCHAR2(1000);
      9      BEGIN
    10          FOR i IN 1..p_tbl.COUNT LOOP
    11              IF (i = 1) THEN
    12                  l_string := p_tbl(i);
    13              ELSE
    14                  l_string := l_string || ', ' || p_tbl(i);
    15              END IF;
    16          END LOOP;
    17          RETURN (l_string);
    18      END print_contents;
    19
    20  BEGIN
    21      l_sample_tbl := t_tbl();
    22      l_sample_tbl.EXTEND;
    23      l_sample_tbl(1) := 'one';
    24      l_sample_tbl.EXTEND;
    25      l_sample_tbl(2) := 'two';
    26      l_sample_tbl.EXTEND;
    27      l_sample_tbl(3) := 'three';
    28      l_sample_tbl.EXTEND;
    29      l_sample_tbl(4) := 'four';
    30      l_sample_tbl.EXTEND;
    31      l_sample_tbl(5) := 'five';
    32      DBMS_OUTPUT.PUT_LINE(print_contents(l_sample_tbl));
    33  END;
    34  /
    one, two, three, four, five
    PL/SQL procedure successfully completed.
    SQL> HTH,
    T.

  • How can I have both date and time for a Date type variable?

    If I have a Date type variable: Date today=new Date(), Is it possible to also give the current time to "today" but still keep the type as Date?
    I mean , for exmaple I want to store this "today" to database as Date type, and the value in database is "2007-05-05 13:30:13". I do not want to use SimpleDateFormat to do that, because I want to use Date type not String. Is that possible? how to implement this?

    I'd say he means you should use Timestamp to store
    the time value, because that's what it's there for.
    Because java.sql.Date is defined not to store a
    time value.Correct. The OP should read the Javadoc for java.sql.Timestamp.

  • Cannot SELECT into a user-defined type variable

    Hi All,
    Oracle 11.2 on Linux.
    See the steps below. I am not able to insert/select into a TYPE variable. I do not want to do a TABLE declaration in my PL/SQL block, but want to use a user defined type. Is it possible ?
    SQL> create or replace type sample_obj_rec as object
      2  (
      3     object_id    number,
      4     object_name  varchar2(32),
      5     object_type  varchar2(32)
      6  );
      7  /
    Type created.
    SQL> create or replace type sample_obj_tab as table of sample_obj_rec ;
      2  /
    Type created.
    -- ------------   CASE 1 ---------------------
    SQL> declare
      2      v_tab   sample_obj_tab := sample_obj_tab() ;
      3  begin
      4      select object_id, object_name, object_type
      5      bulk   collect into v_tab
      6      from   dba_objects
      7      where  rownum < 11 ;
      8  end ;
      9  /
        from   dba_objects
    ERROR at line 6:
    ORA-06550: line 6, column 5:
    PL/SQL: ORA-00947: not enough values
    ORA-06550: line 4, column 5:
    PL/SQL: SQL Statement ignored
    -- ------------   CASE 2 ---------------------
    SQL> declare
      2      v_rec   sample_obj_rec;
      3  begin
      4      select object_id, object_name, object_type
      5      into   v_rec
      6      from   dba_objects
      7      where  rownum = 1;
      8  end ;
      9  /
        from   dba_objects
    ERROR at line 6:
    ORA-06550: line 6, column 5:
    PL/SQL: ORA-00947: not enough values
    ORA-06550: line 4, column 5:
    PL/SQL: SQL Statement ignoredWhat is the issue with both the above cases? what am I missing here?
    Thanks in advance.

    One small detail in the SELECT.
    SQL> create or replace type sample_obj_rec as object
      2  (object_id    number,
      3   object_name  varchar2(32),
      4   object_type  varchar2(32));
      5  /
    Type created.
    SQL>
    SQL> create or replace type sample_obj_tab as table of sample_obj_rec ;
      2  /
    Type created.
    SQL>
    SQL> declare
      2     v_tab   sample_obj_tab := sample_obj_tab() ;
      3  begin
      4     select sample_obj_rec(object_id, object_name, object_type)
      5     bulk   collect into v_tab
      6     from   dba_objects
      7     where  rownum < 11 ;
      8  end ;
      9  /
    PL/SQL procedure successfully completed.
    SQL>

  • How can I obtain an object-type variable in Forms 6i?

    i create an object-type in oracle 8i database like this:
    TYPE OBJ_TYPE_NUMBER AS OBJECT
    FIELD1 NUMBER,
    MEMBER PROCEDURE INIT, ...
    i create a variable of this object-type in a stored procedure in Oracle 8i:
    v_Number OBJ_TYPE_NUMBER(10);
    and then call it's method:
    v_Number.INIT;
    it work's!
    But when I try to compile a previous variable declaration
    (v_Number OBJ_TYPE_NUMBER;) in Oracle Forms 6i I see only an error message.
    So my question is How can I declare and use an object-type variable in Forms 6i?

    Hi,
    the release after Forms 6i is Forms9i. Forms9i does have the PLSQL engine of Oracle 9.0.0.2 database which means that it should knwo how to handle object types in PLSQL.
    Frank

Maybe you are looking for

  • BW Statistics: my record time vs time in BW Statistics

    Hi all, Anyone knows or have documentation of how it works BW Statistics? I mean, from the moment it start recording the time in initializing the OLAP processor, the time when the user can see the page, to the time to end the load of the page or quer

  • What plug ins, extensions, settings, etc. mess with Disqus?

    I have Firefox 22 on Widows 8 with a bunch of plug ins, add ons. But even when i disable the those i still can't log in to Disqus to comment on sites that use it for their comment section ... i have contacted them and gotten no resolution ....

  • The "about:tabs" message always appears. The close button appears inactive so checking the "don't show this again" option doesn't work.

    I am running Firefox 3.6.13 under windows/xp. All of a sudden, when opening a new tab the "about:tabs" message comes up all the time. I checked the box labeled "do not show this message again" but the "close" box seems to be inactive. Pressing it has

  • Copy, cut and paste in sample editor

    If I want to cut (or copy) and paste a chunk of audio file in the sample editor window, where can I put the contents of the 'clipboard'? Should I be able to paste it in another sample editor window. For example, the PC CD burning prog 'Nero" allows y

  • Apps cant launch

    When i open the games or apps that download from app store when i click it , it turn back to the menu but i hold the main button it show the apps is still runing but i cant play it or use it...what can i do?i try force restart ady also same thing