|
From: ankur on 29 Jun 2008 14:25 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
From: Manish Pandit on 29 Jun 2008 15:36 On Jun 29, 11:25 am, ankur <ankur.a.agar...(a)gmail.com> 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
From: Roedy Green on 29 Jun 2008 15:45 Sun, 29 Jun 2008 11:25:56 -0700 (PDT), ankur <ankur.a.agarwal(a)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
From: Maarten Bodewes on 29 Jun 2008 19:19 Roedy Green wrote: > Sun, 29 Jun 2008 11:25:56 -0700 (PDT), ankur > <ankur.a.agarwal(a)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
From: Arne Vajhøj on 29 Jun 2008 19:32 Maarten Bodewes wrote: > Roedy Green wrote: >> Sun, 29 Jun 2008 11:25:56 -0700 (PDT), ankur >> <ankur.a.agarwal(a)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
|
Next
|
Last
Pages: 1 2 Prev: Sun's jdk, hibernate, jboss and log4j code quality Next: Translate attempt |