aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes initialization problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "initialization problems" Watch "initialization problems" New topic
Author

initialization problems

Anuji Philip
Ranch Hand

Joined: Feb 25, 2002
Posts: 46
1)for(int i=0;i,2;i++)String s ;-not compiling while
for(int i=0;i,2;i++){String s;}is compiling without error.why?
2)null when associated with a string becomes of
null string.With object it is nothing.Could you
please explain each case?
Joshua Kueck
Ranch Hand

Joined: Mar 14, 2002
Posts: 71
1. The compiler checks to see if the one without brackets is a statement like i++ or s="groovy", but its not.. so it doesn't like it. Even if you do if(i==0) String s;. It's trying to help you out. With the brackets, it doesn't worry about it because you may be using the string inside the block. My guess is the compilers says "ok. we have a for.. if the next code is a statement or a block, its all good ".
Charlie Sturman
Ranch Hand

Joined: Apr 04, 2002
Posts: 112
for requires a statement,
String s;
is NOT a statement it is a declaration.
;
is a Statement,therefore:
for( ; false ; ) ;
is valid
for( ; false ; ) {}
is also valid because
{}
is a statement!!
for(initialization; Boolean-expression; step)
statement
[ April 12, 2002: Message edited by: Charlie Sturman ]
Anuji Philip
Ranch Hand

Joined: Feb 25, 2002
Posts: 46
i)is declarartion not a kind of statement?
ii)Jon,Please explain in detail
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055

1)for(int i=0;i,2;i++)String s ;-not compiling while for(int i=0;i,2;i++){String s;}is compiling without error.why?

(To be picky about this, you should replace your ',' with '<' in your boolean expression.)
You are making a local variable declaration, which requires a scope. Adding { } to the loop body makes the scope explicit.

2)null when associated with a string becomes of null string.With object it is nothing.Could you please explain each case?

An object reference and the object it is referring to are two very different things. When a String reference is null, it only means that that particular String reference is not pointing to any String object. The same argument applies to any other object reference. There is really no such thing as nullString.
-anthony
[ April 13, 2002: Message edited by: Anthony Villanueva ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: initialization problems