• Post Reply Bookmark Topic Watch Topic
  • New Topic

difference between unused and unread variables  RSS feed

 
Abhishek Kanade
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
can anyone please explain the exact difference between unused and unread variables in java?

thanks,
Abhishek
 
Omkar Shetkar
Ranch Hand
Posts: 100
2
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think,

Unread Variable - Variable which is not read but should have been initialized.
Unused Variables - Variable which is not read and might have NOT been initialized.




Here 'i' is unread variable and 'j' is unused variable. So unread variable is also an unused variable. I don't think reverse is true.
You can read something when it has some value.
You can use something only when it exists through variable declaration.

 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I would suggest you forget about the distinction between those two terms. If you have completed writing your class and you still have unused or unread variables, delete them.
 
Abhishek Kanade
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hahaha!
But I need to create such malicious code for checking the respective rules in SonarQube.
 
Abhishek Kanade
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Omkar Shetkar
Will the same apply if no constructor is written? Say a global variable is left uninitialized and just never used. So still does it abide by the term 'unread' ?
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope you never have a global variable. That would be dreadful design. It is probably bad design to write a class without a constructor. As you know, in that instane a default constructor is added. What happens to fields is described in the Java Language Specification (=JLS). Look at the second line in the paragraph beginning If there is not sufficient space available and look at the link they give to section 4.12.5. Unlike some parts of the JLS, that is reasonably easy to understand.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhishek Kanade wrote: . . . So still does it abide by the term 'unread' ?
Who cares? Delete it.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhishek Kanade wrote:can anyone please explain the exact difference between unused and unread variables in java?

TBH, I've never heard of that distinction - and I've certainly never worried about it.

If I had to guess, I suppose setting a value to a variable counts as "usage", so on that basis it would certainly be possible to have a variable that is "used", but never read.
But it seems to me a semantic distinction only: A variable that is never read is definitely redundant, so I'd avoid them - but whether you regard that as "useless" or not is kind of up to you.

And I heartily agree with Campbell: Don't define global variables. EVER.

Winston
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reminded of when I did the Programming Paradigms module, along with a load of second‑years who had done little coding. People would ask for help and I looked at their code and deleted about half of each function as unneeded.
 
Abhishek Kanade
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all!

After doing some backtracking research I've got that, according to the description of SonarQube, an "unread" variable is the one which is initialized and not used in the program. Whereas "unused" variable is the one which is not initialized and not used in the program. Just a tiny difference it was, which confused me.

@Winston Gutkowski and @Campbell Ritchie, advices of "Constructor" and "Global variables" duely taken. Thanks!
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhishek Kanade wrote:Thank you all!

Ditto to what Campbell said. And thanks for telling us the "difference".

Good luck with your studies.

Winston
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!