f



compiler question #2

This code snippet will compile and run as given below:

public class TooSmartClass {
    public static void main(String[] args) {
     int weight = 10, thePrice;                         // Local
variables
       // int weight = 10, thePrice = 0;                     // Local
variables

        if (weight <  10) thePrice = 1000;
        if (weight >  50) thePrice = 5000;
        if (weight >= 10) thePrice = weight*10;            // Always
executed.
       // System.out.println("The price is: " + thePrice);   // (1)
    }
}

But will not compile as given below:

public class TooSmartClass {
    public static void main(String[] args) {
     int weight = 10, thePrice;                         // Local
variables
       // int weight = 10, thePrice = 0;                     // Local
variables

        if (weight <  10) thePrice = 1000;
        if (weight >  50) thePrice = 5000;
        if (weight >= 10) thePrice = weight*10;            // Always
executed.
          System.out.println("The price is: " + thePrice);   // (1)
    }
}

Error : C:\Java Files>javac TooSmartClass.java
TooSmartClass.java:10: variable thePrice might not have been
initialized
        System.out.println("The price is: " + thePrice);   // (1)
                                              ^
1 error

Why so ?? Because thePrice is not initialized in both the scenarios !


Thanks,
Ankur
0
6/29/2008 6:25:56 PM
comp.lang.java.programmer 52714 articles. 1 followers. Post Follow

7 Replies
357 Views

Similar Articles

[PageSpeed] 27

On Jun 29, 11:25=A0am, ankur <ankur.a.agar...@gmail.com> wrote:
> This code snippet will compile and run as given below:
>
> public class TooSmartClass {
> =A0 =A0 public static void main(String[] args) {
> =A0 =A0 =A0int weight =3D 10, thePrice; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 // Local
> variables
> =A0 =A0 =A0 =A0// int weight =3D 10, thePrice =3D 0; =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 // Local
> variables
>
> =A0 =A0 =A0 =A0 if (weight < =A010) thePrice =3D 1000;
> =A0 =A0 =A0 =A0 if (weight > =A050) thePrice =3D 5000;
> =A0 =A0 =A0 =A0 if (weight >=3D 10) thePrice =3D weight*10; =A0 =A0 =A0 =
=A0 =A0 =A0// Always
> executed.
> =A0 =A0 =A0 =A0// System.out.println("The price is: " + thePrice); =A0 //=
 (1)
> =A0 =A0 }
>
> }
>
> But will not compile as given below:
>
> public class TooSmartClass {
> =A0 =A0 public static void main(String[] args) {
> =A0 =A0 =A0int weight =3D 10, thePrice; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 // Local
> variables
> =A0 =A0 =A0 =A0// int weight =3D 10, thePrice =3D 0; =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 // Local
> variables
>
> =A0 =A0 =A0 =A0 if (weight < =A010) thePrice =3D 1000;
> =A0 =A0 =A0 =A0 if (weight > =A050) thePrice =3D 5000;
> =A0 =A0 =A0 =A0 if (weight >=3D 10) thePrice =3D weight*10; =A0 =A0 =A0 =
=A0 =A0 =A0// Always
> executed.
> =A0 =A0 =A0 =A0 =A0 System.out.println("The price is: " + thePrice); =A0 =
// (1)
> =A0 =A0 }
>
> }
>
> Error : C:\Java Files>javac TooSmartClass.java
> TooSmartClass.java:10: variable thePrice might not have been
> initialized
> =A0 =A0 =A0 =A0 System.out.println("The price is: " + thePrice); =A0 // (=
1)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 ^
> 1 error
>
> Why so ?? Because thePrice is not initialized in both the scenarios !
>
> Thanks,
> Ankur

That is because a local variable is not defaulted like the class
variables (boolean is false, int is zero...etc.). You are assigning
values to thePrice based on the weight, but since there is no default
value for that variable, java complains when you try to *access* it.
It is not just the System.out.println, even if you do something like

int xyz =3D thePrice+20;

it will throw the same compile error.

-cheers,
Manish
0
6/29/2008 7:36:04 PM
 Sun, 29 Jun 2008 11:25:56 -0700 (PDT), ankur
<ankur.a.agarwal@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>    if (weight <  10) thePrice = 1000;
>        if (weight >  50) thePrice = 5000;
>        if (weight >= 10) thePrice = weight*10;            // Always
>executed.
>          System.out.println("The price is: " + thePrice);   // (1)

You are presuming greater intelligence of the compiler than it has.

Is sees this blurrily something ilke

  if ( somethingoroother ) assign price
if ( somethingelse ) assign price
if (yetanothingh thing) assign price

Had you written this as:


  if (weight <  10)
   {
   thePrice = 1000;
   }
 else   if (weight >  50)  
   {  
   thePrice = 5000;
   }
 else
  {
   thePrice = weight*10; 
   }

Then it could be absolutely sure thePrice will be assigned. It does
not have to analyse the conditional expressions to know this.

-- 

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
0
see_website (5876)
6/29/2008 7:45:07 PM
Roedy Green wrote:
>  Sun, 29 Jun 2008 11:25:56 -0700 (PDT), ankur
> <ankur.a.agarwal@gmail.com> wrote, quoted or indirectly quoted someone
> who said :
> 
>>    if (weight <  10) thePrice = 1000;
>>        if (weight >  50) thePrice = 5000;
>>        if (weight >= 10) thePrice = weight*10;            // Always
>> executed.
>>          System.out.println("The price is: " + thePrice);   // (1)
> 
> You are presuming greater intelligence of the compiler than it has.
> 
> Is sees this blurrily something ilke
> 
>   if ( somethingoroother ) assign price
> if ( somethingelse ) assign price
> if (yetanothingh thing) assign price
> 
> Had you written this as:
> 
> 
>   if (weight <  10)
>    {
>    thePrice = 1000;
>    }
>  else   if (weight >  50)  
>    {  
>    thePrice = 5000;
>    }
>  else
>   {
>    thePrice = weight*10; 
>    }
> 
> Then it could be absolutely sure thePrice will be assigned. It does
> not have to analyse the conditional expressions to know this.
> 

Yup, and to be sure that your variable will only be assigned precisely 
once, create it using the final keyword.

final int thePrice;
if(..) thePrice = 1;
else thePrice = 2;

This will throw a compiler error whenever thePrice is not assigned, or 
when thePrice is assigned a second time. Much more secure.

Note that I would not use two assignments in one line, it's less 
readable and harder to work with in the debugger.

Regards,
Maarten
0
6/29/2008 11:19:36 PM
Maarten Bodewes wrote:
> Roedy Green wrote:
>>  Sun, 29 Jun 2008 11:25:56 -0700 (PDT), ankur
>> <ankur.a.agarwal@gmail.com> wrote, quoted or indirectly quoted someone
>> who said :
>>
>>>    if (weight <  10) thePrice = 1000;
>>>        if (weight >  50) thePrice = 5000;
>>>        if (weight >= 10) thePrice = weight*10;            // Always
>>> executed.
>>>          System.out.println("The price is: " + thePrice);   // (1)
>>
>> You are presuming greater intelligence of the compiler than it has.
>>
>> Is sees this blurrily something ilke
>>
>>   if ( somethingoroother ) assign price
>> if ( somethingelse ) assign price
>> if (yetanothingh thing) assign price
>>
>> Had you written this as:
>>
>>
>>   if (weight <  10)
>>    {
>>    thePrice = 1000;
>>    }
>>  else   if (weight >  50)     {     thePrice = 5000;
>>    }
>>  else
>>   {
>>    thePrice = weight*10;    }
>>
>> Then it could be absolutely sure thePrice will be assigned. It does
>> not have to analyse the conditional expressions to know this.
>>
> 
> Yup, and to be sure that your variable will only be assigned precisely 
> once, create it using the final keyword.
> 
> final int thePrice;
> if(..) thePrice = 1;
> else thePrice = 2;
> 
> This will throw a compiler error whenever thePrice is not assigned, or 
> when thePrice is assigned a second time. Much more secure.
> 
> Note that I would not use two assignments in one line, it's less 
> readable and harder to work with in the debugger.

When it comes to style I would recommend Java Coding Convention
from SUN.

The above is not compliant.

Arne
0
arne6 (9808)
6/29/2008 11:32:01 PM
In article <48681888$0$14348$e4fe514c@news.xs4all.nl>,
 Maarten Bodewes <maarten.bodewes@xs4all.nl> wrote:
[...]
> Yup, and to be sure that your variable will only be assigned precisely 
> once, create it using the final keyword.
> 
> final int thePrice;
> if(..) thePrice = 1;
> else thePrice = 2;
> 
> This will throw a compiler error whenever thePrice is not assigned, or 
> when thePrice is assigned a second time. Much more secure.

Aha, I had seen this in Roedy's and others' code without fully 
understanding its import. I'm a fan of letting the compiler do the 
work:-) Thanks!

[...]
-- 
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews
0
nospam21 (19047)
6/30/2008 3:00:31 AM
Arne Vajh�j wrote:
> Maarten Bodewes wrote:
>> Roedy Green wrote:
>>>  Sun, 29 Jun 2008 11:25:56 -0700 (PDT), ankur
>>> <ankur.a.agarwal@gmail.com> wrote, quoted or indirectly quoted someone
>>> who said :
>>>
>>>>    if (weight <  10) thePrice = 1000;
>>>>        if (weight >  50) thePrice = 5000;
>>>>        if (weight >= 10) thePrice = weight*10;            // Always
>>>> executed.
>>>>          System.out.println("The price is: " + thePrice);   // (1)
>>>
>>> You are presuming greater intelligence of the compiler than it has.
>>>
>>> Is sees this blurrily something ilke
>>>
>>>   if ( somethingoroother ) assign price
>>> if ( somethingelse ) assign price
>>> if (yetanothingh thing) assign price
>>>
>>> Had you written this as:
>>>
>>>
>>>   if (weight <  10)
>>>    {
>>>    thePrice = 1000;
>>>    }
>>>  else   if (weight >  50)     {     thePrice = 5000;
>>>    }
>>>  else
>>>   {
>>>    thePrice = weight*10;    }
>>>
>>> Then it could be absolutely sure thePrice will be assigned. It does
>>> not have to analyse the conditional expressions to know this.
>>>
>>
>> Yup, and to be sure that your variable will only be assigned precisely 
>> once, create it using the final keyword.
>>
>> final int thePrice;
>> if(..) thePrice = 1;
>> else thePrice = 2;
>>
>> This will throw a compiler error whenever thePrice is not assigned, or 
>> when thePrice is assigned a second time. Much more secure.
>>
>> Note that I would not use two assignments in one line, it's less 
>> readable and harder to work with in the debugger.
> 
> When it comes to style I would recommend Java Coding Convention
> from SUN.
> 
> The above is not compliant.

Absolutely! My only possible way of explaining myself is not having the 
Eclipse checkstyle plugin working on my Thunderbird client :)

Maarten
0
6/30/2008 7:05:43 PM
On Sun, 29 Jun 2008 23:00:31 -0400, "John B. Matthews"
<nospam@nospam.com> wrote, quoted or indirectly quoted someone who
said :

>
>Aha, I had seen this in Roedy's and others' code without fully 
>understanding its import. I'm a fan of letting the compiler do the 
>work:-) Thanks!

I am a great fan of final. See http://mindprod.com/jgloss/final.html
for why.
-- 

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
0
see_website (5876)
6/30/2008 10:31:04 PM
Reply: