Dont understand a method


What is going on in this method? Even though its a void return type is has a return?  

  public void setVal(String val)
    {
        if (valr == null)
        {
            return;
        }
    }

Many thanks
prograMNewbieAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
CEHJConnect With a Mentor Commented:
And of course it's the same as

public void setVal(String val) {}
0
 
CEHJCommented:
You can always have return. This method effectively does nothing though
0
 
CEHJCommented:
(as you wouldn't know from calling the method whether the String *was* null)
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
prograMNewbieAuthor Commented:

So is it the same as:

public void setVal(String val)
    {
        if (val == null)
        {
           
        }
    }
0
 
hoomanvCommented:
YES
0
 
hoomanvConnect With a Mentor Commented:
We expect setVal to set some value for us
So that method is expected to do something like this

public void setVal(String val)
    {
        if (val == null)
        {
           throw new NullPointerException("non-null expected");
           // or throw new IllegalArgumentException("non-null expected");
        }
        // set the value here
    }
0
 
objectsConnect With a Mentor Commented:
So is it the same as:

public void setVal(String val)
    {
        if (val == null)
        {
           
        }
    }

It is but you wouldn't write it like that in case someone (and by the looks they will) decides to add code after the if
0
 
prograMNewbieAuthor Commented:

I see, thanks everyone ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.