• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Ugly Style !!

 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends...
can you guess the result...?
this is simple, but i think we can learn something from this.
public class UglyStyle {
public static void main(String[] args) {
String STR;
int x = 5;
if (x < 5) STR = "01234";<br /> if (x >= 5) STR = "56789";
System.out.println(str);
}
}
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, i think you've mistyped, str instead of STR in println.
Anyway, i was looking into code assuming STR.
Noticed it will not compile, complaining STR not initialised.
but if we modify, code as,
public class UglyStyle {
public static void main(String[] args) {
String STR;
int x = 5;
if (x < 5) STR = "01234";
else STR = "56789";
System.out.println(STR);
}
}
it compiles. Hope somebody will explain the difference.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stevie,
What you say is quite true. This not good coding style as the compiler error will show.
Actually we use CAPITALS exclusively for final variables, isn't it?

------------------
Hope this helps. Correct me if I am wrong.
Cheers ,
Kapil
 
Stevie Kaligis
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are right Indika, there is a miss typed,
it supposed to be :
System.out.println(STR);
and yes if we used "else" it will compile, that's why i called it "ugly style".
and Kapil, it is not about CAPITALS letter...
thank's
stevie
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello everybody,
i think all of u are not getting the point to which steven is pointing.
Read it carefully.
what steve want's to say is that if u put the brace's around the if condition then the compiler in the above case will not give an error otherwise it will,becasue what happen's when u don't put the brace's, the statement immediately after the if() is a part of if body but the second statement after the immedate statement is not part of the if() condition, therefore when the compiler compile's it check's the if condition if it is true it initializes STR ="56789";then it executes the next statement but if the condition does not meet then the STR ="56789"; does not get execute and that line is skipped and it moves to next statement ( System.out.println(STR) the moment it come's to this statement compiler sees that STR is not initialized therefore it give's an error.I hope this is where steve is actually pointing
Thank's.
bye.
 
Indika Perera
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nitin, do you mean to say, following code will compile.
public class UglyStyle {
public static void main(String[] args) {
String STR;
int x = 5;
if (x < 5) {STR = "01234";}<br /> if (x >= 5) {STR = "56789";}
System.out.println(STR);
}
}
No it doesn't, make a difference.
 
reply
    Bookmark Topic Watch Topic
  • New Topic