Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

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
0
prograMNewbie
Asked:
prograMNewbie
  • 3
  • 2
  • 2
  • +1
3 Solutions
 
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
 
prograMNewbieAuthor Commented:

So is it the same as:

public void setVal(String val)
    {
        if (val == null)
        {
           
        }
    }
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
hoomanvCommented:
YES
0
 
hoomanvCommented:
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
 
CEHJCommented:
And of course it's the same as

public void setVal(String val) {}
0
 
objectsCommented:
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now