Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > compiler question

Reply
Thread Tools

compiler question

 
 
ankur
Guest
Posts: n/a
 
      06-29-2008
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
 
Reply With Quote
 
 
 
 
Manish Pandit
Guest
Posts: n/a
 
      06-29-2008
On Jun 29, 11:25*am, ankur <(E-Mail Removed)> wrote:
> 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


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 = thePrice+20;

it will throw the same compile error.

-cheers,
Manish
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      06-29-2008
Sun, 29 Jun 2008 11:25:56 -0700 (PDT), ankur
<(E-Mail Removed)> 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
 
Reply With Quote
 
Maarten Bodewes
Guest
Posts: n/a
 
      06-29-2008
Roedy Green wrote:
> Sun, 29 Jun 2008 11:25:56 -0700 (PDT), ankur
> <(E-Mail Removed)> 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
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      06-29-2008
Maarten Bodewes wrote:
> Roedy Green wrote:
>> Sun, 29 Jun 2008 11:25:56 -0700 (PDT), ankur
>> <(E-Mail Removed)> 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
 
Reply With Quote
 
John B. Matthews
Guest
Posts: n/a
 
      06-30-2008
In article <48681888$0$14348$(E-Mail Removed)4all.nl>,
Maarten Bodewes <(E-Mail Removed)> 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
 
Reply With Quote
 
Maarten Bodewes
Guest
Posts: n/a
 
      06-30-2008
Arne Vajh°j wrote:
> Maarten Bodewes wrote:
>> Roedy Green wrote:
>>> Sun, 29 Jun 2008 11:25:56 -0700 (PDT), ankur
>>> <(E-Mail Removed)> 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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-30-2008
On Sun, 29 Jun 2008 23:00:31 -0400, "John B. Matthews"
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiler Error Message: The compiler failed with error code -1073741819 Ram ASP .Net 0 09-13-2005 09:52 AM
Why is a JIT compiler faster than a byte-compiler RickMuller Python 4 03-26-2005 04:30 PM
Compiler compiler with C++ as output Andrey Batyuck C++ 3 05-17-2004 08:17 PM
Can we use <compiler> tag to avoid RunTime Compiler error? Jack Wright ASP .Net 5 01-19-2004 04:36 PM
Compiler Error Message: The compiler failed with error code 128. Yan ASP .Net 0 07-21-2003 10:49 PM



Advertisments