Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6224229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:38:07+00:00 2026-05-24T08:38:07+00:00

I hope this is a very simple question, what is the Android equivalent to

  • 0

I hope this is a very simple question, what is the Android equivalent to doing this in PHP:

PHP:

function BlahBlah ($first_var,$second_var,$optional_var_1=null,$optional_var_2=null) {

}

In Android it appears everything I feed into a function must have a non-null value for all variables. While I can add logic such as if statements to check for null values, why can’t I simply pass through null values and have them be ignored?

My problem revolves around a function which enters a book into my sql database for my app. If I enter all of the book’s information such as author, title, copyright, ISBN, etc. there is no problem as I am not supplying any null values. If however, I were to leave the copyright or ISBN as Null and try to pass these through to the function I get a nullpointerexception. Sometimes I don’t need to record the ISBN or the copyright (this is an example and not the real app) so instead of making multiple functions such as addBook(…) addBookWithNoISBN(…) addBookWithNoCopyright(…) … what is the better way to do this?

I believe I should be sending all of my variables to the function as a string array and perhaps do some processing in the function itself? Is there a better way?

I hope people get what I’m trying to say, thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:38:09+00:00Added an answer on May 24, 2026 at 8:38 am

    Java supports something called method overloading, which lets you define multiple methods having the same exact name, but with different numbers/types of parameters: http://download.oracle.com/javase/tutorial/java/javaOO/methods.html

    So you would end up coding a separate method for each possible combination. They would then pass in null values where needed, and call the main method that does the core logic:

    boolean test(int param1, String param2) {
        return test(param1, param2, null, null);
    }
    boolean test(int param1, String param2, String param3) {
        return test(param1, param2, param3, null);
    }
    boolean test(int param1, String param2) {
        // Do your logic here
        if(param1 == 'foo' && param3 == null)
            return false;
        if(param2 == param3)
            return true;
    }
    

    With this, it doesn’t matter if you pass in 2, 3, or 4 variables as the same function is ultimately called with ‘null’ values for things you didn’t pass.

    Here’s another example, where you have a single method name that can accept all sorts of input types:

    boolean AddBook(int isbn)
    {
        Book result = FindBookByISBN(isbn);
        return AddBook(result);
    }
    boolean AddBook(String asin)
    {
        Book result = FindBookByAmazonId(asin);
        return AddBook(result);
    }
    boolean AddBook(String title, String author)
    {
        Book result = FindBookByTitleAndAuthor(title, author);
        return AddBook(result);
    }
    boolean AddBook(Book theBook)
    {
        if(theBook == null)
            return false;
    
        return MyBookCollection.Add(theBook);
    }
    

    Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Very simple question I hope. I have the usual <select> box like this <select
This is a very hard to explain question and I hope my code extract
This is a very simple, probably too simple question but I'm afraid my limited
Question very simple - say, i got function, which receives array as its arguments
I hope this question takes a simple fix, and I am just missing something
This seems to me like a very simple question, but I can't seem to
I hope this is quite a simple question.. I've got a compiled .exe program
I have a very simple (rather stupid) question, I hope someone can clear my
This is actually my very first question on Stackoverflow. I hope its not the
I have a -as I hope- very simple question: how to disable an UISearchBar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.