Overloading & Cast &Type Save?

Here I use final bounds, therefore ignore cast warnings
and test overloading and inference(<- do not work)
My question: Is this considered type-save? public class DoW {
  public static final String[] DAY_NAME=
       {"Su","Mo","Tu","We","Th","Fr","Sa"};
  private int wd;
  public DoW(int day,int month, int year) {
    // just for fun
    if (month<3) { month= month+12; year--; }
    wd= (day+ 2*month+ 3*(month+1)/5+ year+
         year/4-year/100+year/400+1)%7;
  public <T extends String> T getDoW()  { return (T)DAY_NAME[wd]; }
  public <T extends Integer> T getDoW() { return (T)new Integer(wd); }
  public static void main(String[] args) {
    DoW dow= new DoW(20,2,2005);
    System.out.println(dow.<String>getDoW()+" #"+dow.<Integer>getDoW());
}

I do not argue about sense, let me tinker please!
1. As String/Integer are final they can be used to fool
the compiler and get overloading on return types as a free lunch.
2. See reply Gafter to "Usafe casting with generics"
The compiler warns precisely when it cannot generate code to fully check the cast at runtime.Really? I doubt that, as Sun even ignores it's own compiler warning sometime.
If these two warnings are contrary to the fact that this is type save,
no one will care too much about warnings, as he might think
"my code is also Sun-type save".
This would be a (tiny?) disaster for Generics, as it depends on the fact
that only "warning-less" code can be used as generic building blocks.
3. If finals make a sense in bounds the compiler should not ignore that.

Similar Messages

  • Invalid cast type error

    Hello,
    I create a new class object at runtime using:
    Class dmClass = Class.forName(dmClassName);
    I want to be able to use the Class object, dmClass, to cast new objects but I can't seem to get this to work. I've tried:
    (dmClass) .......
    (dmClass.getName()).....
    But I get an Invalid Cast type expression error.
    How can I use dmClass as the cast type in a statement?
    Thanks
    Glenn

    Ok,
    I have:
    class DataManager
    class SasDMImp which implements DataManager
    I do the following:
    String dmClassName = "SasDMImp";
    Class dmClass = Class.forName(dmClassName);
    I now want to use the SasDMImp class to cast a new object and this is the part I'm having trouble with.
    If I hard code the class name I use:
    SasDMImp dm = (SasDMImp) Naming.lookup("rmi://" host registry);
    I want to replace the hard coded SasDMImp with dmClass but I get the Invalid Cast type when I do this.
    Hope this helps.
    Glenn

  • Problem doing ASSING.....CASTING TYPE (dbtype)

    Hi everybody, i have a problem with a dinamic movement. I explain it below.
    I want to copy HR Data with a Function Module with Destination parameter.
    I build a table type PRELP with all the personal data with all the infotypes (PA)
    In my Target System i do this:
    **DECLARATIONS
    field-symbols: <fs>    type any.
    LOOP        at PRELP_T into PRELP_WA.
    ASSING PRELP_WA to <fs> CASTING TYPE (PRELP_WA-infty)
    "PRELP_WA-infty is like 'PA0001' For example.
    MODIFY (PRELP_WA-infty) from <fs>
    ENDLOOP.
    If the <fs> contains fields like type DEC, when i move data with ASSING, this fields are fill like:
    <0.0, or <0.3, or <3........rare characters.
    In MODIFY sentences, programa return me a DUMP, I supose is becose this characters.
    PS: if i modify in debug mode this DEC fields, and change <0.0 with 2 (For Example) it runs correctly.
    I hope yo could help me
    cheers
    Edited by: Miguel Ortega on Jun 16, 2009 12:00 PM

    Hi,
    Did you create your destinations in your application server? It seems that the destinations such as topics and queues are not created. First you should create them and then start using them.
    For example, in here, it says that jndi/Topic is not valid. To check if the destinations are valid or not; you can do it either through the graphical user interface or the command line console.

  • Wht does it mean - "ASSIGN it_DB_TBL_name TO fs1 CASTING TYPE x"?

    Hi Experts,
    Just curious,
    Pls. let me know that, Wht is the functionality of the following statement:
    ASSIGN it_DB_TBL_name TO <fs1> CASTING TYPE x
    thanq

    ASSIGN feld TO <fs> CASTING TYPE type.
    ASSIGN feld TO <fs> CASTING TYPE (typename).
    ASSIGN feld TO <fs> CASTING LIKE fld.
    ASSIGN feld TO <fs> CASTING DECIMALS dec.
    You can use ASSIGN ... CASTING to treat the contents of a field as a value of another type using a field symbol. One application for this statement would be to provide different views on a structure with casts on different types.
    One wide-spread ABAP technique is to use C fields or structures as containers for storing structures of different types that are frequently only known at runtime. The components of the structure are selected with offset/length accesses to the container. Since this technique no longer works with Unicode, you can also look upon an existing memory area as a container with the suitable type definition using a field symbol with the ASSIGN ... CASTING statement. In the next example, a certain field of database table X031L is read, whereby the field and table names are only defined at runtime.
    Example:
    * Read a field of table X031L
    PARAMETERS:
      TAB_NAME    LIKE SY-TNAME,             "Table name
      TAB_COMP    LIKE X031L-FIELDNAME,      "Field name
      ANZAHL      TYPE I DEFAULT 10.         "Number of lines
    DATA:
      BEGIN OF BUFFER,
        ALIGNMENT TYPE F,                    "Alignment
        C(8000)   TYPE C,                    "Table contents
      END OF BUFFER.
    FIELD-SYMBOLS:
      <WA>   TYPE ANY,
       TYPE ANY.
    * Set field symbol with suitable to buffer area
    ASSIGN BUFFER TO <WA> CASTING TYPE (TAB_NAME).
    SELECT * FROM (TAB_NAME) INTO <WA>.
      CHECK SY-DBCNT < ANZAHL.
      ASSIGN COMPONENT TAB_COMP OF STRUCTURE <WA> TO .
      WRITE: / TAB_COMP, .
    ENDSELECT.
    Until now, in the ASSIGN feld TO <f> CASTING ... statement, the system checked to ensure that the field was at least as long as the type that was assigned to the field symbol, <f>. (Field symbols can either be typed at declaration or the type specified in an ASSIGN statement using CASTING TYPE). The syntax check is now more thorough. Now, you can only assign the field field (in either a Unicode or non-Unicode program)
    provided it is at least as long as the type assigned to the field symbol <f>. Otherwise, the system returns a syntax error. At runtime, the system only checks to see whether or not the lengths are compatible in the current system (as before).
    If the field type or field symbol type is a deep structure, the system also checks that the offset and type of all the reference components match in the area of field that is covered by <f>. The syntax check is now more thorough. Now, the system checks that these components must be compatible with all systems, whether they have a one-byte, double-byte, or four-byte character length. At runtime, the system only checks to see whether or not the reference components are compatible in the current system
    In Unicode systems, in the ASSIGN str TO <f> TYPE C/N and ASSIGN str TO <f> CASTING TYPE C/N statements, the length of str may not always be a multiple of the character length, in which case the program terminates at runtime.

  • Class Cast Type Exception

    I encountered class cast type exception becos I set the attribute by passing in a class instance but when I get the attribute, I used a string to store it. So any idea how I can solve this error? Thanks.
    request.setAttribute("message", cr); where cr is a class
    but here i declare a string to store, so how do i do a conversion?
    String messageString = request.getAttribute("message");
    if (messageString == null){
         messageString = "";

    All parameters of request objects are stored as type Object. So when you extract the objects from the request, you will need to explicitly cast to the type you are expecting e.g.
    String myString = (String)request.getParameter("Hello");

  • Ora-1460 during select using table(cast(type))

    Hi all!
    I've got a problem with a query that fails with a ora-1460 at random time intervals. This is a 10.2.0.3 version database running on a sun os.
    We've managed to reproduce this error controlled when running an analyze on the table in question at the same time this query runs.
    The error looks like this:
    Unexpected system error, see server log for details. Root message is: org.apache.ojb.broker.PersistenceBrokerSQLException: * SQLException during execution of sql-statement: * sql statement was 'SELECT A0.ID,A0.LOCK_VERSION,A0.CLASS_NAME,A0.DESCRIPTION,A0.NAME,A0.EXTERNAL_ID,A0.ORDER_NUMBER,A0.LEVEL_ID,A0.ROOT_AH_ID,A0.DIFF_END_DATE,A0.DIFF_START_DATE,A0.SUPPORTED_BY_ASS_CAL,A0.CATEGORY_ROLE_ID FROM CATEGORY A0 WHERE A0.ID IN (select /*+ cardinality(1) */ * from table(cast( ems_string_to_table(?) as ems_table_of_number_type ))union select /*+ cardinality(1) */ * from table(cast( ems_string_to_table('') as ems_table_of_number_type)))' * Exception message is [ORA-01460: unimplemented or unreasonable conversion requested ] * Vendor error code [1460] * SQL state code [72000]
    ems_string_to_table is a function thats populates a type with a unknown number of values. Though, the length of the string never exceeds 4k.
    ems_table_of_number_type is a type define as "table of numbers".
    Has anybody seen this error before, or have any idea why this should be?
    Best regards,
    Heyers

    There is an IN clause constaraint i.e max number of characters you can pass from Oracle .Please check your select inner query which might be resulting to cross more than the boundary IN clause ,

  • What cast type are used for?

    In the example attached to this question is a simple VI application that cast, using type cast of LabVIEW, one value to different other type. My understanding of type cast, in C, C++ and to my best knowledge, was an implicit adaptation of a numerical value in a specific format to an other format. The value should remain the same. Unless the type in which the value is assigned is not enough large or adapted to contain such a number ex:
    I8 = 500;
    U32 = -1;
    But in my example all the assigned indicator are large enough.
    I have the same problem in LabVIEW 5.1.1 and 6.1
    If someone could only tell me what it is used for?
    Best regards,
    Nitrof
    Attachments:
    Type_cast.vi ‏26 KB

    The type cast function is really helpful for typecasting "unusual" data to a more convienient data format. It can also be used to coerce data, like in your example, but coercion is usually not always a good thing because it can cause bad values.
    There are lots of good examples of typecasting.
    For example, I use the typecast function to change refnums to unsigned 32 integers (U32), just so I can pass the refnums between VI's easier.
    Another VERY common typecast is if you want to display the ASCII value of a character. Run the string character "A" into a typecast, and set the output type as a Unsigned 8 bit number (U8), and you will see 0x41. Take a look at my example VI's to see what I mean.
    David
    Attachments:
    Type_cast_Example5.vi ‏16 KB
    Type_cast_Example6.vi ‏18 KB

  • PDF file does not contain some of the symbols from Math Type save in a Word Document?

    I write my math documents in Word using Math Type.
    The PDF drops parentheses and some math symbols such as x=bar and p-hat.
    anyone else have this problem?

    Contact the support of Microsoft or use a forum for MS Word.

  • Casting types

    Hi!
    Situation
    class Point
    class ColorPoint extends Point
    Point pont = new Point();
    ColorPoint = new ColorPoint();
    colorPoint = (ColorPoint) point;
    I get ClassCastException in run-time mode. Why?
    And what I can do?
    Thank you.

    Point pont = new Point();
    ColorPoint = new ColorPoint();you missed the variable here. Did you mean ColorPoint colorPoint = new ColorPoint();
    colorPoint = (ColorPoint) point;This won't work (as you've seen ;-) ). colorPoint is of type ColorPoint but point is of type Point.
    possible castings:
    static:
    Point point;
    ColorPoint colorPoint = new ColorPoint();
    point = colorPoint;
    dynamic:
    Point point = new ColorPoint(); // dynamic type is now ColorPoint but static Point
    ColorPoint colorPoint = (ColorPoint) point; // works because the dynamic type of point is ColorPoint.
    Greets
    Puce

  • CPU overloading when screen saver is on

    Recently I've been noticing that whenever the screen saver starts the fan quickly starts working full power, and when I come out of sleep I can see in the activity monitor the cpu usage dropping from almost %100. (There's no cpu intensive process running normally on my Mac).
    Not only does the fan noise disturb and presumably shortens the lifespan of my MacBook, but I am worried that some spyware is running when it thinks no one would notice...
    So,
    1. How can I identify the culprit? (e.g. record which process was leeching the cpu during screensaver)
    2. Is there any way I can enforce a general limit on cpu usage when the Mac is idle?
    Thanks!!

    Use Activity Monitor (Utilities folder.) You might consider not using the particular screen saver. If it's a third-party screen saver it may simply be defective.

  • Type Casting? When to use this concept?

    There is one question bothering me very very much.
    When should I use type casting?
    For example
    ClassNameQ c = (ClassNameQ) ......displayable
    How do I know which 'cast type' to use? Is there a suitable pattern
    we need to follow so that we can know 'Ah this is the cast
    we need to use'
    Please can any one point out which 'Cast type' points needs to be
    followed?
    Regards

    You can Cast an object from any subclass to its superclass,
    or from a superclass to the sublass, if the object is already
    an object of the subclass.
    I have a class named Name:
    public class Name {
    I have a subclass named LastName:
    public class LastName extends Name { [/b]
    And another named FirstName:
    [b]public class FirstName extends Name { [/b]
    I create an object like this:
    [b]LastName ln = new LastName("Johnson");
    Then pass it to a method that can work on all names:
    capitalFirstLetter(ln);
    capitalFirstLetter(Name n) {
    Now n in CapitalFirstLetter can be used only as a Name
    object. Anything specific to the LastName object can't be used.
    So if I need to use things specific to LastName I have to cast
    the object to LastName. There is a problem, however, since this
    method can work on all Names, so I don't know if the Name is
    a LastName or not. The object itself DOES know, however, and
    we can test, so we would cast to a subclass as follows:
    if (n instanceof LastName) {
    LastName ln = (LastName)n;
    ln.addToGeneology();
    } else of (n instanceof FirstName) {
    FirstName fn = (FirstName)n;
    fn.addOccurance();
    Steve

  • Field symbol type casting

    Hi
    how can i change the data type of a field symbol at run time..(say Packed  to  char)
    may be it can have a field data  or structure data..
    please help me ..........
    Regards
    albert

    hi
    thank you for your reply
    i am trying to do as follows but i am not getting the expected output
    where do i go wrong.............
    *"*"Local Interface:
    *"  TABLES
    *"      IT_OUT STRUCTURE  THENV OPTIONAL
    DATA: v_date type sy-datum,
               mytype type C.
    FIELD-SYMBOLS: <fs>,
                   <fs1>.
    v_date = sy-datum.
    ASSIGN v_date TO <fs>.
      append <fs> to it_out.
    WRITE / <fs>.      " ---------------->20.09.2011
    ASSIGN v_date TO <fs1> casting TYPE C.
      append <fs1> to it_out.
    WRITE / <fs1>.       " --------------->20110920
    LT_OUT  is having the data as 20110920
                                                                     20110920
    but i expect in date format     20.09.2011
                           20.09.2011

  • No "Save As" in recent Pages Update

    I just updated to Mountain Lion and the most current version of iPages.  I no longer have a "Save As" function/option in my "File" menu.  I go to help and type "Save As" and I get the handy little animated help showing it, but that menu in NO WAY resembles the new menu I now have. So Help does not work because itscalling up the older menu.
    What gives?

    Durandel,
    Save As is no longer a part of the OSX preferred scheme, but they brought it back because some users didn't want to take the new path. There are new ways to do whatever you need to do, but as Peggy notes, you don't have to change your ways if you don't want to. Some of the app-specific help files haven't caught up with the new OSX methods. We're caught in a situation where because of hardware and other considerations, there are users in both worlds.
    Jerry

  • VECTOR: Can't Locate Where to Specify Content Type

    Hi all. I'm confused with warning from compiler. I'm specifying methods for swing TableModel using vector data and it always shows me unchecked call to set as a member of raw type (method setValueAt). I just have no idea how to cast type there. The code is below:
    import java.util.Vector;
    public class SSCCE {
    public static void main(String[] args){
        new dk_MF();
    class dk_MF{
    dk_MF(){ constructTable();  }
    public void constructTable(){
      Vector<Vector> bigvec = getTabData();
      Vector smallvec = (Vector) bigvec.elementAt(0).clone();
      bigvec.remove(0);
      Model MM = new Model(bigvec,smallvec);
      System.out.println(MM.getValueAt(0,0)); }
    public Vector<Vector> getTabData(){
      Vector<Vector> bv = new Vector<Vector>();
      Vector<String> sv = new Vector<String>();
      sv.add("one"); sv.add("two");
      bv.add((Vector)sv.clone());
      bv.add((Vector)sv.clone());
      sv.clear();
      return bv; }
    class Model {
      private Vector columnNames = null;
      private Vector data = null;
      public Object getValueAt(int row, int col){
       return ((Vector) data.elementAt(row)).elementAt(col); }
      public void setValueAt(Object value, int row, int col) {
       ((Vector) data.elementAt(row)).set(col, value);  }
      public void createNewTable(Vector b, Vector s) {
       data = (Vector) b.clone();
       columnNames = (Vector) s.clone();  }
      Model(Vector big, Vector small) {
       createNewTable(big,small);
    }

    Thanks for the tip on casting problem! But now I'm back to the starting point. OK, I have a Vector of Vectors of Arbitrary Data: Vector<Vector<?>>. I've done all this description in the code (below). But this cursed "unchecked call to set" remains.
    What, actually, this "unchecked call" means? Why casting ((Vector) data.elementAt(row)) at getValueAt does not get a warning?
    P.S. I've tryed to put Vector<?> (caused error) and Vector<Object> (warning) in setValueAt, but this did not help!
    import java.util.Vector;
    public class SSCCE {
    public static void main(String[] args){
        new dk_MF();
    class dk_MF{
    dk_MF(){ constructTable();  }
    public void constructTable(){
      Vector<Vector<?>> bigvec = getTabData();
      Vector smallvec = (Vector) bigvec.elementAt(0).clone();
      bigvec.remove(0);
      Model MM = new Model(bigvec,smallvec);
      System.out.println(MM.getValueAt(0,0)); }
    public Vector<Vector<?>> getTabData(){
      Vector<Vector<?>> bv = new Vector<Vector<?>>();
      Vector<String> sv = new Vector<String>();
      sv.add("one"); sv.add("two");
      bv.add(new Vector<String>(sv));
      bv.add(new Vector<String>(sv));
      sv.clear();
      return bv; }
    class Model {
      private Vector columnNames = null;
      private Vector data = null;
      public Object getValueAt(int row, int col){
       return ((Vector) data.elementAt(row)).elementAt(col); }
      public void setValueAt(Object value, int row, int col) {
       ((Vector) data.elementAt(row)).set(col, value);  }
      public void createNewTable(Vector b, Vector s) {
       data = (Vector) b.clone();
       columnNames = (Vector) s.clone();  }
      Model(Vector big, Vector small) {
       createNewTable(big,small);
    }

  • The difference between FIELD-SYMBOL and normal DATA TYPE

    Dear experts,
    Please see the example below, both are output the same result.
    DATA: EXTERNAL_RECORD(4000),
          POSITION TYPE I,
          LENGTH TYPE N,
          ENTRY TYPE STRING.
    EXTERNAL_RECORD = '0005Smith0007Edwards0005Young'.
    DO.
      LENGTH = EXTERNAL_RECORD+POSITION(4).
      IF LENGTH = 0.
        EXIT.
      ENDIF.
      ADD 4 TO POSITION.
      MOVE EXTERNAL_RECORD+POSITION(LENGTH) TO ENTRY.
      WRITE ENTRY.
      ADD LENGTH TO POSITION.
      IF POSITION >= 4000.
        EXIT.
      ENDIF.
    ENDDO.
    --OR It can be written as--
    DATA: EXTERNAL_RECORD(4000),
          POSITION TYPE I,
          LENGTH TYPE N.
    FIELD-SYMBOLS <ENTRY>.
    EXTERNAL_RECORD = '0005Smith0007Edwards0005Young'.
    DO.
      LENGTH = EXTERNAL_RECORD+POSITION(4).
      IF LENGTH = 0.
        EXIT.
      ENDIF.
      ADD 4 TO POSITION.
      ASSIGN EXTERNAL_RECORD+POSITION(LENGTH) TO <ENTRY>.
      WRITE <ENTRY>.
      ADD LENGTH TO POSITION.
      IF POSITION >= 4000.
        EXIT.
      ENDIF.
    ENDDO.
    Is there any special circumstances we need to use FIELD-SYMBOL?
    Why is FIELD-SYMBOL is introduce in the first place?
    Kindly advice with example.
    Thanks in advance for those who can help me on this.

    HI,
    You can use field symbols to make the program more dynamic. In this example the name of a table control is substituted by a field symbol. Thus you cal call the form with any internal table, using the name of the table control as a parameter.
    Example
    form insert_row
    using p_tc_name.
    field-symbols <tc> type cxtab_control. "Table control
    assign (p_tc_name) to <tc>.
    insert 100 lines in table control
    <tc>-lines = 100.
    Field symbols allow you to:
    **     Assign an alias to a data object(for example, a shortened
            name for data objects structured through several hierarchies
            - <fs>-f instead of rec1-rec2-rec3-f)
    **     Set the offset and length for a string variably at runtime
    **     Set a pointer to a data object that you determine at runtime (dynamic ASSIGN)
    **     Adopt or change the type of a field dynamically at runtime
    **     Access components of a structure
    **     (from Release 4.5A) Point to lines of an internal table
            (process internal tables without a separate work area)
    Field symbols in ABAP are similar to pointers in other programming
    languages. However, pointers (as used in PASCAL or C) differ from ABAP
    field symbols in their reference syntax.
    The statement ASSIGN f to <fs> assigns the field f to field
    symbol <fs>. The field symbol <fs> then "points" to the
    contents of field f at runtime. This means that all changes to the
    contents of f are visible in <fs> and vice versa. You declare
    the field symbol <fs> using the statement FIELD-SYMBOLS: <fs>.
    Reference syntax
    Programming languages such as PASCAL and C use a dereferencing symbol
    to indicate the difference between a reference and the object to which
    it refers; so PASCAL would use p^ for a pointer instead of p, C would
    use *p instead of p. ABAP does not have any such dereferencing symbol.
    **     In PASCAL or C, if you assign a pointer p1 to a pointer p2,
    you force p1 to point to the object to which p2 refers (reference semantics).
    **     In ABAP, if you assign a field symbol <fs1> to a field
    symbol <fs2>, <fs1> takes the value of the data object to
    which <fs2> refers (value semantics).
    **     Field symbols in ABAP are always dereferenced, that is,
    they always access the referenced data object. If you want to
    change the reference yourself in ABAP, you can use the ASSIGN statement
    to assign field symbol <fs1> to field symbol <fs2>.
    Using field symbols
    You declare field symbols using the FIELD-SYMBOLS statement.
    They may be declared either with or without a specific type.
    At runtime you assign a field to the field symbol using the ASSIGN
    statement. All of the operations on the field symbol act on the field
    assigned to it.
    When you assign a field to an untyped field symbol, the field symbol
    adopts the type of the field. If, on the other hand, you want to assign
    a field to a typed field symbol, the type of the field and that of the
    field symbol must be compatible.
    A field symbol can point to any data object and from Release 4.5A,
    they can also point to lines of internal tables.
    The brackets (<>) are part of the syntax.
    Use the expression <fs> IS ASSIGNED to find out whether the field
    symbol <fs> is assigned to a field.
    The statement UNASSIGN <fs> sets the field symbol <fs> so
    that it points to nothing. The logical expression <fs>
    IS ASSIGNED is then false. The corresponding negative expression
    is IF NOT <fs> IS ASSIGNED.
    An unassigned field symbol <fs> behaves as a constant with
    type C(1) and initial value SPACE.
    MOVE <fs>
    TO dest     Transfers the initial value SPACE to the variable dest
    MOVE 'A' to <fs>     
    Not possible, since <fs> is a constant
    (runtime error).
    To lift a type restriction, use the CASTING addition in the
    ASSIGN statement. The data object is then interpreted as though
    it had the data type of the field symbol. You can also do this
    with untyped field symbols using the CASTING TYPE <type> addition.
    The danger with pointers is that they may point to invalid areas.
    This danger is not so acute in ABAP, because the language does not
    use address arithmetic (for example, in other languages, pointer p
    might point to address 1024. After the statement p = p + 10, it would
    point to the address 1034). However, the danger does still exist, and
    memory protection violations lead to runtime errors.
    A pointer in ABAP may not point beyond a segment boundary. ABAP does
    not have one large address space, but rather a set of segments.
    Each of the following has its own segment:
    *     All global data
    *     All local data
    *     Each table work area (TABLES)
    *     Each COMMON PART
    You should only let field symbols move within an elementary field or
    structure where ABAP allows you to assign both within the global data
    and beyond a field boundary.
    Rgds
    Umakanth

Maybe you are looking for