Solved

Dont understand a method

Posted on 2006-10-26
8
184 Views
Last Modified: 2010-03-31

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
Comment
Question by:prograMNewbie
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17809968
You can always have return. This method effectively does nothing though
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17809978
(as you wouldn't know from calling the method whether the String *was* null)
0
 

Author Comment

by:prograMNewbie
ID: 17809985

So is it the same as:

public void setVal(String val)
    {
        if (val == null)
        {
           
        }
    }
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 14

Expert Comment

by:hoomanv
ID: 17810049
YES
0
 
LVL 14

Assisted Solution

by:hoomanv
hoomanv earned 30 total points
ID: 17810057
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 65 total points
ID: 17810074
And of course it's the same as

public void setVal(String val) {}
0
 
LVL 92

Assisted Solution

by:objects
objects earned 30 total points
ID: 17810135
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
 

Author Comment

by:prograMNewbie
ID: 17810422

I see, thanks everyone ;)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now