2 Replies Latest reply: Aug 7, 2013 6:41 PM by EJP RSS

    Java Short hand syntax extension.

    user13613967

      Currently Java allows for short hand use with some non-primitive objects to be resolved in expressions such as String.

       

      We are accustomed to writing

       

      String x = “abc”;

      Which is the equivalent to

                      String x = new String(“abc”);

       

      We also can use strings in expressions with the + operator

                      String x = “abc” + “xyz”;

      Is equivialnt to

                      String x = new String(“abc”).concat(new String(“xyz));

       

      A great deal of programming, especially for business is manipulation of strings and Decimals, wouldn’t it make sense to include in the short hand expression in Java syntax to include Strings, BigDecimals, and BigIntegers?

       

      For instance Given:

      BigDecimal principal = new BigDecimal(“1000.00”);

      BigDecimal interest = new BigDecimal(“.20”);

       

      So we could express

       

      BigDecimal amount = principal * interest;

       

      Instead of

      BigDecimal amount = prinicipal.mult(interest);

       

       

      Or similar to String we could simply say

       

      BigDecimal x = 10;     // would resolve to  BigDecimal x = new BigDecimal(10);  

       

      BigDecimal x = (amount + 100) * .10;  // could resolve to..

       

      BigDecimal x = amount.add(new BigDecimal(100)).mult(new BigDecimal(.10));

       

      Also.. Comparators.. 

       

      Like comparing primitives’, alow short had compare for String, BigDecimal and BigInteger

       

      String X = “123”;

      String Y = “123”;

      If (X==Y) {…}   // would resolve to if(X.equals(Y)){..}       -- I recognize that == equivocates the POINTERS, but perhaps some exception or syntax operator can be implemented.

       

      or for BigDecimal

       

      BigDecimal a = new BigDecimal(10);

       

      if (a>0) {...}  // would resolve to   if (a.compareTo(new BigDecimal(0)>0) {...}

       

       

      If such a short hand syntax was implemented on just these few objects,  business programs using Java would be much more readable.. Thus improving the standing of Java in the world of languages in my opinion.

       

      Thoughts.?

        • 1. Re: Java Short hand syntax extension.
          EJP

          > String x = “abc”;

          > Which is the equivalent to

          >                String x = new String(“abc”);

           

          No it isn't. No String is created at runtime. "abc" is already a String, created at compile time.

           

          >                String x = “abc” + “xyz”;

          > Is equivalent to

          >                String x = new String(“abc”).concat(new String(“xyz));

           

          No it isn't. It is equivalent to

           

                      String x = “abcxyz”;

           

          > Thoughts?

           

          You're asking in the wrong place. Submit a Request For Enhancement.

          • 2. Re: Java Short hand syntax extension.
            user13613967

            Thanks for the reply , but where do I submit that request?