• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

If statement containing casting causes error

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends,



The above peice of code gives the following error(altough if there is a single statement in an IF statement curly braces are optional).....


test1.java:29: not a statement
Dvd one = (Dvd) ne;
^
test1.java:29: ';' expected
Dvd one = (Dvd) ne;


and gets solved when we change the code to ..




Why is this happening...??

Can anyone please give their suggestions on the above problem.Thank you in advance.

Regards,
Hardik.S.Raja
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although it is works if you change it to




Thanks,
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> Can anyone please give their suggestions on the above problem.Thank you in advance.

I guess, that declaration is not a valid statement, which can be inserted immeadiatly after if. (you must use block statement for that as you wrote)
http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.5
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Please give a sample code of your problem...

Thanks
Harish
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a sample with similar problem




If you replace

if( "test" instanceof String)
String s = (String)"test";

with

String s = null;
if( "test" instanceof String)
s = (String)"test";

it will work.
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If you replace

if( "test" instanceof String)
String s = (String)"test";

with

String s = null;
if( "test" instanceof String)
s = (String)"test";

it will work





Why the behaviour of IF statement changes like this?
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pretty strange, is it a bug?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Yeming Hu:
pretty strange, is it a bug?



No. A local variable declaration statement cannot be used as the consequent statement executed by an if. See the JLS link posted above.
 
Yeming Hu
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:


No. A local variable declaration statement cannot be used as the consequent statement executed by an if. See the JLS link posted above.



A little confused. Why is there this restriction? If variable declaration statement can be used as the consequent statement executed by an if, what is the bad consequence?
In addition, the executing results of:
[1] if("text" instanceof String)
String s= (String)"text";
[2] if("text" instanceof String)
{
String s= (String)"text";
}
are the same. Why [2] compiles but [1] is not allowed?

Thanks a lot!
 
Yeming Hu
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:


No. A local variable declaration statement cannot be used as the consequent statement executed by an if. See the JLS link posted above.


I just check the BNF for if_statement of Java, It seems to me that local variable declaration statement is not forbidden as the consequent statement executed by an if, see BNF for if_statement of Java. Therefore, I am pretty confused now. Look forward to your answer and thanks in advance.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I think about it is that local variables must be declared in blocks, and a one line (ExpressionStatement) is not a block.
Please refer to http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.2
 
Yeming Hu
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Satou kurinosuke:
What I think about it is that local variables must be declared in blocks, and a one line (ExpressionStatement) is not a block.
Please refer to http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.2



It seems that is right. But why is that?
[ May 14, 2007: Message edited by: Yeming Hu ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it make sense to declare a variable as a statement, where it can never be used ?
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
its really a tough question making everyone confused(including me).
can anyone clear the doubt as to why is this happening aided with a good example?
thanks in advance
[ May 14, 2007: Message edited by: debasmita pattnayak ]
 
Yeming Hu
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Satou kurinosuke:
Does it make sense to declare a variable as a statement, where it can never be used ?



I like this explanation. Thanks!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Yeming Hu:

I just check the BNF for if_statement of Java, It seems to me that local variable declaration statement is not forbidden as the consequent statement executed by an if, see BNF for if_statement of Java. Therefore, I am pretty confused now. Look forward to your answer and thanks in advance.



In my opinion that BNF is wrong. The JLS states that a statement is:


None of those is a local variable declaration.

Anyway, as previously noted: declaring a variable as the only statement is rather meaningless because you cannot use it in the subsequent code.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In my opinion that BNF is wrong


Yes, I thought it was too, but did not pay too much attention to it. It's better to refer to the JLS instead.
 
Yeming Hu
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:


In my opinion that BNF is wrong. The JLS states that a statement is:


None of those is a local variable declaration.

Anyway, as previously noted: declaring a variable as the only statement is rather meaningless because you cannot use it in the subsequent code.



I see. Thanks!
 
debasmita pattnayak
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
i am not clear about this concept. can someone please elaborate on this?

Thanks!
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
debasmita,
What is not clear ? Look at the following code :

Where are you going to use "one" ?
 
debasmita pattnayak
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Satou,
i think i have understood now. But please let me know if I have really understod the problem or not by looking once at my explaination :

code:
--------------------------------------------------------------------------------

if(ne instanceof Dvd)
Dvd one = (Dvd) ne;

--------------------------------------------------------------------------------

If we are declaring a local variable, then the compiler expects us to use it in the block( not just declaring it). Hence, the error.
But when we are doing this:
if(ne instanceof Dvd){
Dvd one = (Dvd) ne;
}
As we are putting it in curly braces,the compiler thinks that we will be using it further(though its also a declaration and we are not using it but the compiler is satisfied with this).

Is this what you wanted to convey?please correct me if I have misunderstood something.
thanks in advance
 
debasmita pattnayak
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Satou,
and moreover is it like it happens with all the local instance variable that we try to decalre then only we get this error?

Does it happen with other flow statements like for, while etc?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is this what you wanted to convey?


Yes it is

and moreover is it like it happens with all the local instance variable that we try to decalre then only we get this error?


Yes. Local variable declarations must be in blocks.
 
debasmita pattnayak
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Satou,
thanks a lot
 
What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic