Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2005

Tip: Looking for answers? Try searching our database.

Simple Code? What's Wrong?

Thread view: 
Peter van der Goes - 14 Jul 2005 00:33 GMT
It's probably something obvious, but this has me stumped:

Very simple code extracted from a proper main()

int x = 65;
int y = 55;

if (x > y)
   int ans = x + y;

Errors:
E:\CIT1613\HelloWorld.java:17: '.class' expected
  int ans = x + y;
                           ^
E:\CIT1613\HelloWorld.java:17: not a statement
  int ans = x + y;

Line 17 is int ans = x + y;

?!?
Mike Schilling - 14 Jul 2005 01:19 GMT
> It's probably something obvious, but this has me stumped:
>
[quoted text clipped - 14 lines]
>
> Line 17 is int ans = x + y;

You can't declare a variable here.  In JLS-speak:

   Statement : if ParExpression Statement [else Statement]

and while a LocalVariableDeclarationStatement can appear in a Block, it
isn't itself a Statement.  (Which argues it should be called something else
, but ...)

Likewise

if (x > y)
   label: x = y;

and

if (x > y)
   class Inner { .....}

are also forbidden.

int ans;
if (x > y)
   ans = x + y;

is of course OK.
Peter van der Goes - 14 Jul 2005 02:56 GMT
> You can't declare a variable here.  In JLS-speak:
>
[quoted text clipped - 21 lines]
>
> is of course OK.

Thanks very much for the explanation. I don't think this is well known, at
least not by textbook authors (one of whom used this code in a test question
bank, indicating the correct answer is: ans = 120).
A local variable declaration with initialization not a statement.
Interesting. Same code works fine in C++, but that's not pertinent.
As your explanation suggests, adding {} will allow the code to compile.
I learned something today. I appreciate you taking the time...
Mike Schilling - 14 Jul 2005 20:15 GMT
> Thanks very much for the explanation. I don't think this is well known, at
> least not by textbook authors (one of whom used this code in a test
> question bank, indicating the correct answer is: ans = 120).
> A local variable declaration with initialization not a statement.

Certainly it's a statement in the normal use of that term, but it's not a
Statement in the JLS's Backus-Naur grammar.

> Interesting. Same code works fine in C++, but that's not pertinent.

That's a really bad idea, I would think.

 i = 0;
 if (i > 0)
     int ans = 12;

 if (ans > 0)    // is ans declared here?  If so, what is its value?

But C++, unlike Java, has no rules to force variables to be initialized
before they're used.
Roedy Green - 14 Jul 2005 02:53 GMT
>if (x > y)
>    int ans = x + y;
spelling that out longhand

if ( x > y )
{

int ans = x + y;

}

your error should be more obvious. You create ans then instantly throw
it away.  

you mean to write something like this:

int ans = ( x > y) ? x + y : x - y;

or

int ans = y;

if ( x > y )
  {
  ans = 42;
  }

Signature

Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.