compiler question

A

ankur

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
 
M

Manish Pandit

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
 
R

Roedy Green

Sun, 29 Jun 2008 11:25:56 -0700 (PDT), ankur
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.
 
M

Maarten Bodewes

Roedy said:
Sun, 29 Jun 2008 11:25:56 -0700 (PDT), ankur


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
 
A

Arne Vajhøj

Maarten said:
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
 
J

John B. Matthews

Maarten Bodewes said:
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!

[...]
 
M

Maarten Bodewes

Arne said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top