aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Ugly Style !! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Ugly Style !!" Watch "Ugly Style !!" New topic
Author

Ugly Style !!

Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
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);
}
}
Indika Perera
Greenhorn

Joined: Feb 28, 2001
Posts: 8
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.
kapil apshankar
Ranch Hand

Joined: Dec 17, 2000
Posts: 66
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


Hope this helps. Correct me if I am wrong.<p>Cheers <img src="smile.gif" border="0"> ,<br />Kapil
Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
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
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
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

Joined: Feb 28, 2001
Posts: 8
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ugly Style !!