1

As per This Post..it is said that Java does not have a nullable feature as C# has. but i have seen many methods in Android Sdk that accepts 'null' as parameters for e.g see this method

SmsManager manager = SmsManager.getDefault();
manager.sendTextMessage("address@example.com", null, "Message body here", null, null);

In the above sentTextMessage method, it is accepting null as parameters, i have a my method that accepts String as parameters i also want to pass null in some condition how can i do that ? in C# i can do public void methodName(int? param1) but in java how to do that..here is my method that should accept null also

public boolean update(long rowId,String status_receive,String status_send){/*xyz*/}

update(2,null,"yes") // error Invalid Argument,

My Question is how to declare a method that can accepts null values also ?

Community
  • 1
  • 1
FosterZ
  • 3,863
  • 6
  • 38
  • 62
  • 2
    The invalid argument error is not coming from your passing the value `null` to a `String` argument. If you are getting a stack trace, look at the line number on which the invalid argument exception was thrown. – Ray Toal Jul 13 '11 at 06:30

2 Answers2

3

Typically you would use the wrapper classes in the java.lang package - e.g. Integer for int, Long for long etc. Auto-boxing will convert primitive values into objects where necessary - but be aware that this is different from .NET nullable types in that it does use full objects, rather than value types with an extra flag.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

Anything which is not a primitive type (int, byte, char etc.) can be null in Java. There is no need or possibility to declare anything on that.

sstn
  • 3,050
  • 19
  • 32