• 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

difference b/w two code snippets

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, will wait for others's reponce.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell Ritchie . can you please explain you answer.
Thanks
 
Ranch Hand
Posts: 55
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you read the grammar, or the definition of statement you have been given, roby george?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this code works fine :

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic