This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Why is void used?

 
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might sound a bit weird but i really did not get the void concept in book it's given that void is a return type which returns nothing then why to use void?
 
Marshal
Posts: 65772
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The void keyword is not so much a erturn type as an absence of return type.

It is needed because every method must declare what it returns, or (void) that it returns nothing.
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:The void keyword is not so much a erturn type as an absence of return type.



Exactly what Campbell says. I just want to give you an example that gives the clear understanding of void. In swing/AWT (It could be true for any other Java frameworks. I tested it in Swing/AWT. So, please conside this). For an analogy, We have simple user login page. In this page we are first checking whether user filled username and password or not? If
No then we will simply put a check

else yes then

  • Create Database Connection
  • Validate User
  • And other related user authentication task


  • So in first check (#1) we say to compiler if something goes wrong then please do not proceed further.

    I think that will be sufficient to explain. In case of any doubt please let me know.
     
    Campbell Ritchie
    Marshal
    Posts: 65772
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I would have thought that throwing an Exception would be a better approach than a plain return; statement in that situation.
     
    Bartender
    Posts: 1842
    13
    Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I just had to do a whole project that was filled with entry validation. I didn't throw any exceptions, just returned true or false for good or bad. It seems like Ashish had a similar take on a similar problem. I guess now that you say it exceptions might be good, but I think in a beginner or low level school project, probably not necessary.
     
    Campbell Ritchie
    Marshal
    Posts: 65772
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If I were marking, I would give extra marks for your using Exceptions to enforce defensive programming.
     
    Rancher
    Posts: 600
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I agree with Campbell that exceptions are a much better choice.

    John.
     
    Fidel Edwards
    Ranch Hand
    Posts: 234
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    John de Michele wrote:I agree with Campbell that exceptions are a much better choice.

    John.



    I also agree with Campbell's comments but I just want to give a glimpse of void returning situations. It is always as good practice to handle exception rather than simply writing return.

     
    Ranch Hand
    Posts: 466
    1
    IntelliJ IDE Oracle Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Say you senerio where you want to do some action but don't want to have an acknowledgement (read return) you can use a void there.
     
    To avoid criticism do nothing, say nothing, be nothing. -Elbert Hubbard. Please critique this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!