• 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
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Syntax error, insert "AssignmentOperator Expression" to complete Expression

 
Greenhorn
Posts: 10
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Could anyone please explain to me why Java behave differently on Line 1 and Line 2 in this code



Why Line 1 doesn't compile and Line 2 does?
 
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

G Batra wrote:Why Line 1 doesn't compile and Line 2 does?


Because a method invocation (line2) is a valid expression/statement, but just accessing a variable isn't (line1). There are a few alternatives to fix this issue and one of them is to do what the compiler suggestsorAnother alternative is to use the variable as part of another method call, e.g.

Hope it helps!
Kind regards,
Roel
 
G Batra
Greenhorn
Posts: 10
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply Roel.

Is the usability of the statement drives the validity of it? I mean, if the statement is useful in any way, then the statement becomes a valid statement. If it isn't then it is NOT a valid statement.

Just wanted to know, why Java doesn't enforce the method return values to be assigned to appropriate variable in the calling method? Does it not make the return values as "useless".?
 
Roel De Nijs
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

G Batra wrote:Is the usability of the statement drives the validity of it? I mean, if the statement is useful in any way, then the statement becomes a valid statement. If it isn't then it is NOT a valid statement.


I assume one (or more) sections in the JLS will specify all conditions a statement must fulfill to be valid. This and all subsequent sections describe all valid statements. And I think we can agree that the empty statement is pretty useless, but it's allowed (and thus will compile)

G Batra wrote:Just wanted to know, why Java doesn't enforce the method return values to be assigned to appropriate variable in the calling method? Does it not make the return values as "useless".?


What if you are not interested in the return value. For example, the add() method of ArrayList returns a boolean. I have used this method many, many, many times without using the return value, because in these use cases it didn't matter. So if the Java compiler enforces return values to be assigned, your code would be a mess because you would need to create a bunch of local variables which are not used at all. So you would need to type boolean result =, just to store the return value of the add() method and make the compiler happy. But you won't do anything with this variable, because you are not interested in this return value. And that's for every method invocation which returns a value

Hope it helps!
Kind regards,
Roel
 
Enthuware Software Support
Posts: 4907
60
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you think about it, the statement doesn't make any sense. Let's say we are in a room and I shout out your name...just the name. What are you going to do? All you can do is say, "what?". That means calling out just the name is kinda incomplete. This is exactly what you are doing when you write new Jump().x;
OTOH, if I say, "Batra, check your email!", it is a clear instruction about what you need to do. You will check the mail and may or may not give a confirmation of some kind. I may or may not ignore that confirmation. This is what happens with a method call.
 
Roel De Nijs
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Anilprem wrote:OTOH, if I say, "Batra, check your email!", it is a clear instruction about what you need to do. You will check the mail and may or may not give a confirmation of some kind. I may or may not ignore that confirmation. This is what happens with a method call.


Or if he doesn't know/understand "email", he throws a GreekToMeException
 
Paul Anilprem
Enthuware Software Support
Posts: 4907
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

Paul Anilprem wrote:OTOH, if I say, "Batra, check your email!", it is a clear instruction about what you need to do. You will check the mail and may or may not give a confirmation of some kind. I may or may not ignore that confirmation. This is what happens with a method call.


Or if he doesn't know/understand "email", he throws a GreekToMeException


 
G Batra
Greenhorn
Posts: 10
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think talking about the issues, actually put some sense into place and widens the visibility.

Thank you Roel and Paul for the explanation and Analogies..

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic