9 Replies Latest reply: Jun 4, 2013 5:17 AM by 948748 RSS

    Local Variable

    profdeepaks
      In the following code why does java give the error highlighting last line of the code (Bold) instead of fourth line(Italic) ?

      public static void main(String args[] )
      {
      int i;
      float j;
      float w = 3.14f;
      i = w;+
      j = i + w;
      System.out.println(i);
      System.out.println(j);
      i = i + w;
      }

      The error message in BlueJ:
      Possible loss of precision
      Required: int; found: float
        • 1. Re: Local Variable
          rp0428
          >
          In the following code why does java give the error highlighting last line of the code (Bold) instead of fourth line(Italic) ?
          >
          The fourth line is invalid. You have
          +i = w; 
          The plus indicates addition. You aren't adding 'i' to anything.
          • 2. Re: Local Variable
            profdeepaks
            Sorry...
            I don't know how i+=w changed to +i=w;

            Kindly check the following code and reply.

            In the following code why does java give the error highlighting last line of the code instead of fourth line ?

            public static void main(String args[] )
            {
            int i;
            float j;
            float w = 3.14f;
            i+= w;
            j = i + w;
            System.out.println(i);
            System.out.println(j);
            i = i + w;
            }

            The error message in BlueJ:
            Possible loss of precision
            Required: int; found: float
            • 3. Re: Local Variable
              Kayaman
              1002880 wrote:
              Why does java give the error highlighting last line of the code instead of fourth line ?
              Because it's defined that way in the Java Language Specification.
              • 4. Re: Local Variable
                gimbal2
                >
                Possible loss of precision
                Required: int; found: float
                In the following code why does java give the error highlighting last line of the code instead of fourth line ?
                Do you know what an int is? Do you know what a float is? If not you would be wise at the point in time to go find that out. Then it will be easy for you to answer why you would get this "loss of precision" error. I will translate that error to something simpler: "You will lose stuff".
                • 5. Re: Local Variable
                  profdeepaks
                  Alright...
                  Is there any logic behind this?
                  What if such kind of questions come in OCA 803.
                  How to answer this question?
                  • 6. Re: Local Variable
                    EJP
                    Is there any logic behind this?
                    The logic of the Java Language Specification, as cited by Kayaman.
                    What if such kind of questions come in OCA 803
                    What if it does? That doesn't change the answer.
                    How to answer this question?
                    Kayaman's answer continues to apply.
                    • 7. Re: Local Variable
                      rp0428
                      >
                      Because it's defined that way in the Java Language Specification.
                      >
                      Hmm - the reason is defined in the spec alright but you aren't providing a reference to any section that says why.

                      And there is nothing in the spec about highlighting one line instead of another. ;)

                      I think you and everyone else may have misread OPs question - OP did NOT ask
                      >
                      Why does java give the error highlighting last line of the code ?
                      >
                      OP ask
                      >
                      Why does java give the error highlighting last line of the code instead of fourth line ?
                      • 8. Re: Local Variable
                        rp0428
                        >
                        I don't know how i+=w changed to +i=w;

                        Kindly check the following code and reply.

                        In the following code why does java give the error highlighting last line of the code instead of fourth line ?
                        . . .
                        The error message in BlueJ:
                        Possible loss of precision
                        Required: int; found: float
                        >
                        Well that fourth line is STILL invalid. Either you did not notice it or your tool, BlueJ, did not highlight. Because a lower-case 'i' is so tiny you may not have noticed the highlighting since Netbeans highlights it with this error
                        >
                        variable i might not have been initialized
                        >
                        So let's initialize it and reduce your code to the bare minimum
                        int i = 0;
                        float w = 3.14f;
                         
                        i += w;   // line 4
                        i = i + w; // line 5
                        i = (int)(i + w); // line 6
                        Now your question is valid and is a good one. Now line 4 is NOT highlighted but, like your code, line 5 is highlighted with a possible loss of precision.

                        But why isn't line 4 highlighted? Note that line 6 is ALSO not highlighted.

                        This might be what the others were trying to tell you because the language spec DOES show you that line 6 is the equivalent of line 4.

                        And that explicit cast to an int in line 6 ('(int)') is implicitly done in line 4. Because the result is being cast to an 'int' it basically tells the compiler that any loss of precision is ok.

                        Line 5 has no explicit or implicit cast of the result type so you are warned about the possible loss of precision. If you are ok with the loss of precision then you just add the cast and end up with line 6 which has no warning.

                        See the Java Language Spec at section 15.26.2
                        http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.26.2
                        >
                        15.26.2. Compound Assignment Operators
                        A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

                        For example, the following code is correct:

                        short x = 3;
                        x += 4.6;
                        and results in x having the value 7 because it is equivalent to:

                        short x = 3;
                        x = (short)(x + 4.6);
                        >
                        The lesson here?

                        Many people think that if i is an int and w is a float then these two lines of code are equivalent:
                        i += w;   // line 4
                        . . .
                        i = i + w;
                        As you now know they are NOT. But these two lines are:
                        i += w;   // line 4
                        . . .
                        i = (int)(i + w); // line 6
                        So for your question (using an initialized i to make that line valid)
                        >
                        why does java give the error highlighting last line of the code (Bold) instead of fourth line(Italic)
                        >
                        The answer is that line 4 has an implicit cast to int so the compiler does not produce the warning about loss of precision. Line 6 has no such cast.
                        • 9. Re: Local Variable
                          948748
                          p0428 is right.
                          +=, *=, %=, -=, /= are called short-cut assignment operators (http://www.freejavaguide.com/assignment_operators.htm)
                          The reasons behind are:
                          1. they make arithmetic expressions simpler
                          2. they will do implicit casting for "narrow conversions" - (http://www.java2s.com/Tutorial/SCJP/0080__Type-Casting/WideningandNarrowconversions.htm)