• Post Reply Bookmark Topic Watch Topic
  • New Topic

difference b/w two code snippets  RSS feed

 
roby george
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello i have two code snippet
First one is like bellow which run properly

int first_var=0;
int second_var=0;
if (first_var > second_var){
int difference = first_var - second_var;
}

second one is like bellow which gives me compile time error
int first_var=0;
int second_var=0;
if (first_var > second_var)
int difference = first_var - second_var;

The difference is only curly braces of if block

Can anybody tell me what is the exact difference and why above code gives me compile time error.

Thanks
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without a brace an if condition can have only a Statement not a declaration.
and please use code tag henceforth
 
roby george
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it a specification/rules that JAVA have ?
Can you tell me why is it not possible.
Thanks
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the error it should be. lets hear from others though.
 
roby george
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, will wait for others's reponce.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Find the grammar of the language, and you see
Statement:
Block
assert Expression [ : Expression] ;
if ParExpression Statement [else Statement]
Now looking up Statement, you find it doesn't allow declaration as a type of Statement. It does however allow Block.
 
roby george
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie . can you please explain you answer.
Thanks
 
N Sahni
Ranch Hand
Posts: 55
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Campbell.

In Java every local variable declaration is immediately contained by a block.
Please refer: http://java.sun.com/docs/books/jls/third_edition/html/statements.html#5920
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the grammar, or the definition of statement you have been given, roby george?
 
Kammaganti Kamal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this code works fine :

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N Sahni wrote:Agree with Campbell.

In Java every local variable declaration is immediately contained by a block.
Please refer: http://java.sun.com/docs/books/jls/third_edition/html/statements.html#5920


Yeah, there is really nothing more to explain. It's how it is defined by the Java language specification. To get more explanation than that, we'll need to have one of the java designers explain why they did that -- and I don't think Gosling shows up around these parts.

Henry
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
roby george wrote:Campbell Ritchie . can you please explain you answer.
Thanks


It is not that hard to explain in plain English.

An statement (such as if) can take a block, surrounded by brackets OR ONE and only ONE statement.

When you write:


it is actually TWO statements. If you "translate" the code into natural language is states "Declare a variable of type int called difference and assign it the product of the mathematical function first_var minus second_var". That is two statements.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, it's not a statement at all. It's a declaration with initialization. And because it's a declaration it's not allowed to be the body of any if statement (or else, while, for, etc).
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob is correct (he usually is). If you look in the grammar, under "Statement" it doesn't say anything about declaration. It does however say "block" and you are allowed declarations inside blocks.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:. . . To get more explanation than that, . . .
I think there is another explanation. If you have a declaration as a second part of an if statement, all on its own, without enclosing {}, it is obvious to the compiler that this variable cannot be used because it would go out of scope.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!