• Post Reply Bookmark Topic Watch Topic
  • New Topic

assignment error  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in this code

public class Knowing{
static final long tooth = 343L;
static long doIt(long tooth){
System.out.println(++tooth + "");
return ++tooth;

}
public static void main(String[]args){
System.out.println(tooth + "");
final long tooth = 340L;
2 new Knowing().doIt(tooth);
System.out.println(tooth);
}
}

the line marked as 2 should give compilation error because the value returned by the doIt() method is not assigned to any variable.But this is not giving any error, the code is running successfully.Why?
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags in any future posts. Your code is practically unreadable without them.

In Java (and C, C++ etc.) it is legal to ignore the return value of a method. Personally, I wish it wasn't, but it's a small point.
 
Ajay Kumar Rana
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
Please use code tags in any future posts. Your code is practically unreadable without them.

In Java (and C, C++ etc.) it is legal to ignore the return value of a method. Personally, I wish it wasn't, but it's a small point.


can you please let me know how to make my code more readable. I didn't get the point of using code tags.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Click on CODE beneath the left of the message window, then you get CODE and /CODE appearing. Copy and paste the code in the gap between CODE and /CODE.
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

2 new Knowing().doIt(tooth);

This is not an error. the object or value is return but not hold by any variable. So the object is free for garbage collector and nothing in case of primitive.

Yes this is not good way to depend on garbage collector. make object null by yourself if you don't need it
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To reinforce the order of operations to the reader I suggest using parentheses in this case.


Also, you can click on the edit button above your post (pencil and paper), select the source code in your post, then click the CODE button below it. This will allow you to modify a post you've already made.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!