8 Replies Latest reply: Dec 14, 2009 6:23 AM by dcminter RSS

    Casting or Generics, which is better?

    843790
      Which is better for serialization <--> de-serialization?
      public class MyClass {
       private Object st;
       public MyClass (Object what) { st = what; }
       public Object get() {return st;}
      }
      
      out.writeObject(new MyClass("This is a test"));
      
      Object ob =  in.readObject();
      if (ob instanceof String) {
        Sting result = (String) ob;
      }
      Or using Generics?
      public static final byte STRING_TYPE = 0x01;
      ....
      
      public class MyClass<T> {
       private T st;
       private byte type;
       public MyClass ( byte type, T what ) { st = what; this.type = type; }
       public T get() {return st;}
       public byte getType() {return type;}
      }
      
      out.writeObject(new MyClass<String>(STRING_TYPE,"This is a test"));
      
      MyClass<?> value =  (MyClass<?>) in.readObject()
      if (value.getType() == STRING_VALUE) {
        MyClass<String> str = (MyClass<String>) value;
        Sting result = str.get();
      }
      Both have unchecked casts (at least according to Eclipse), it is better to cast the generic or just use casts and not use generics? The latter is more complicated but allows more flexibility, but which method is correct?
        • 1. Re: Casting or Generics, which is better?
          EJP
          which method is correct?
          Both are correct. The second way is 17 lines, the first way is 12 lines. That's about all there is to choose between them.
          • 2. Re: Casting or Generics, which is better?
            843790
            Personally I strongly prefer the first one.

            The second one gives the impression that it's somehow more type-safe, while in fact it is not.

            If your code doesn't know the type, then it shouldn't pretend to know it.
            • 3. Re: Casting or Generics, which is better?
              843790
              JoachimSauer wrote:
              Personally I strongly prefer the first one.
              Assuming it was fixed to not be completely wrong...
              • 4. Re: Casting or Generics, which is better?
                843790
                endasil wrote:
                JoachimSauer wrote:
                Personally I strongly prefer the first one.
                Assuming it was fixed to not be completely wrong...
                Uhm ... of course.

                I was just skimming the question and interpreted it as "If I have a untyped Object from any source such as serialization, should I attempt to hide that fact with generics or not".

                Note to self: actually read the question before answering.
                • 5. Re: Casting or Generics, which is better?
                  843790
                  JoachimSauer wrote:
                  endasil wrote:
                  JoachimSauer wrote:
                  Personally I strongly prefer the first one.
                  Assuming it was fixed to not be completely wrong...
                  I was just skimming the question and interpreted it as "If I have a untyped Object from any source such as serialization, should I attempt to hide that fact with generics or not".
                  Oh I know what you meant there. Though I was replying to your post, that comment was meant as a hint to the OP. Should've made that clear.
                  • 6. Re: Casting or Generics, which is better?
                    843790
                    CREATE TABLE `login` (
                    `username` varchar(40) DEFAULT NULL,
                    `password` varchar(40) DEFAULT NULL
                    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

                    CREATE TABLE `amount` (
                    `amountid` int(11) NOT NULL,
                    `receiptid` int(11) DEFAULT NULL,
                    `loanid` int(11) DEFAULT NULL,
                    `amount` bigint(11) DEFAULT NULL,
                    `latefee` int(11) DEFAULT NULL,
                    `paymentid` int(11) DEFAULT NULL,
                    `pid` int(11) DEFAULT NULL,
                    PRIMARY KEY (`amountid`)
                    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

                    CREATE TABLE `applicationfee` (
                    `applicationfeeid` int(11) DEFAULT NULL,
                    `applicationamount` int(11) DEFAULT NULL,
                    `applicationfee` int(11) DEFAULT NULL
                    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

                    CREATE TABLE `category` (
                    `categoryid` int(11) DEFAULT NULL,
                    `categoryname` varchar(40) DEFAULT NULL,
                    `categorydescription` varchar(500) DEFAULT NULL,
                    `cattype` int(11) DEFAULT NULL
                    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

                    CREATE TABLE `commission` (
                    `commissionid` int(11) DEFAULT NULL,
                    `bussiness` int(11) DEFAULT NULL,
                    `commission` int(11) DEFAULT NULL,
                    `pid` int(11) DEFAULT NULL
                    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

                    CREATE TABLE `customer` (
                    `cacno` int(11) NOT NULL DEFAULT '0',
                    `name` varchar(40) DEFAULT NULL,
                    `age` int(11) DEFAULT NULL,
                    `cphone` varchar(40) DEFAULT NULL,
                    `cmobile` varchar(40) DEFAULT NULL,
                    `caddress` varchar(500) DEFAULT NULL,
                    `cstatus` varchar(20) DEFAULT NULL,
                    `cphoto` longblob,
                    `pid` int(11) DEFAULT NULL,
                    PRIMARY KEY (`cacno`)
                    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

                    CREATE TABLE `daybook` (
                    `closingbal` varchar(40) DEFAULT NULL,
                    `date` date DEFAULT NULL
                    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

                    CREATE TABLE `extraincome` (
                    `categoryid` int(11) NOT NULL,
                    `receiptid` int(11) DEFAULT NULL,
                    `date` date DEFAULT NULL,
                    `amountid` int(11) DEFAULT NULL
                    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

                    CREATE TABLE `employee` (
                    `empno` int(11) DEFAULT NULL,
                    `empname` varchar(40) DEFAULT NULL,
                    `age` int(11) DEFAULT NULL,
                    `sal` int(11) DEFAULT NULL
                    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

                    CREATE TABLE `image` (
                    `id` int(11) DEFAULT NULL,
                    `image` blob
                    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

                    CREATE TABLE `loan` (
                    `loanid` int(11) NOT NULL DEFAULT '0',
                    `loanamt` varchar(40) DEFAULT NULL,
                    `payableamount` double DEFAULT NULL,
                    `installment` int(11) DEFAULT NULL,
                    `payableinstallments` int(11) DEFAULT NULL,
                    `monthlyinstallment` varchar(20) DEFAULT NULL,
                    `surityname` varchar(20) DEFAULT NULL,
                    `applicationfeeid` int(11) DEFAULT NULL,
                    `interestrate` float DEFAULT NULL,
                    `issuedate` date DEFAULT NULL,
                    `duedate` date DEFAULT NULL,
                    `nextduedate` date DEFAULT NULL,
                    `cacno` int(11) DEFAULT NULL,
                    `cname` varchar(20) DEFAULT NULL,
                    `pid` int(11) DEFAULT NULL,
                    `interestamt` double DEFAULT NULL,
                    `pendingamt` float DEFAULT NULL,
                    PRIMARY KEY (`loanid`)
                    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

                    CREATE TABLE `md` (
                    `mdid` int(11) NOT NULL DEFAULT '0',
                    `mdname` varchar(40) DEFAULT NULL,
                    `mdphoto` varchar(100) DEFAULT NULL,
                    `mdphone` varchar(40) DEFAULT NULL,
                    `mdmobile` varchar(40) DEFAULT NULL,
                    `mdaddress` varchar(500) DEFAULT NULL,
                    PRIMARY KEY (`mdid`)
                    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

                    CREATE TABLE `partner` (
                    `pid` int(11) NOT NULL DEFAULT '0',
                    `pname` varchar(40) DEFAULT NULL,
                    `paddress` varchar(500) DEFAULT NULL,
                    `pphoto` varchar(100) DEFAULT NULL,
                    `pphone` varchar(40) DEFAULT NULL,
                    `pmobile` varchar(40) DEFAULT NULL,
                    `pstatus` varchar(20) DEFAULT NULL,
                    `mdid` int(11) DEFAULT NULL,
                    `mdname` varchar(40) DEFAULT NULL,
                    `date` date DEFAULT NULL,
                    `nextpaydate` date DEFAULT NULL,
                    PRIMARY KEY (`pid`)
                    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

                    CREATE TABLE `partnerinvested` (
                    `pid` int(11) DEFAULT NULL,
                    `pname` varchar(20) DEFAULT NULL,
                    `receiptid` int(11) DEFAULT NULL,
                    `date` date DEFAULT NULL,
                    `amountinvested` int(11) DEFAULT NULL,
                    `latefee` int(11) DEFAULT NULL,
                    `amountid` int(11) DEFAULT NULL
                    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

                    CREATE TABLE `payments` (
                    `paymentid` int(11) NOT NULL,
                    `categoryid` int(11) DEFAULT NULL,
                    `particulars` varchar(100) DEFAULT NULL,
                    `amountid` int(11) DEFAULT NULL,
                    `paymentdate` date DEFAULT NULL,
                    PRIMARY KEY (`paymentid`)
                    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

                    CREATE TABLE `receipts` (
                    `receiptid` int(11) DEFAULT NULL,
                    `paiddate` date DEFAULT NULL,
                    `amountid` int(11) DEFAULT NULL,
                    `loanid` int(11) DEFAULT NULL,
                    `latefee` int(11) DEFAULT NULL,
                    `installment` int(11) DEFAULT NULL,
                    `cacno` int(11) DEFAULT NULL,
                    `cname` varchar(40) DEFAULT NULL,
                    `pid` int(11) DEFAULT NULL
                    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
                    • 7. Re: Casting or Generics, which is better?
                      dcminter
                      Irrelevant post blocked.
                      • 8. Re: Casting or Generics, which is better?
                        843790
                        Thanks for the replies. From what I can see, there is no real way to type-safe from serialization, My application uses the same classes for both the server and the client so there is no confusion to the cast, if there is, then it would be a fatal issue with the stream. The example I wrote was purely made from the top of my head so I was not meaning it to be functional, just as an example. Generics will work out better for what I am using it for, I was just seeing what the opinion would be because I did both and both worked fine. Thanks for your time!!