File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

initialization problems

 
Anuji Philip
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i)is declarartion not a kind of statement?
ii)Jon,Please explain in detail
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic