Android: How to set a default value for an argument variable
Android function
PHP example:
function HaHa($a = "Test")
{
print $a;
}
The question is how to do it in android...
public void someFunction(int ttt = 5)
{
// something
}
The solution above doesn't work, how can I do it?
Thanks!
java variables arguments default
add a comment |
Android function
PHP example:
function HaHa($a = "Test")
{
print $a;
}
The question is how to do it in android...
public void someFunction(int ttt = 5)
{
// something
}
The solution above doesn't work, how can I do it?
Thanks!
java variables arguments default
I don't think we can do that in Java. and I just can make a wild guess, if I remember correctly, that this is a feature that is supposed to be in Java 7. So in this case, one can check the variablettt
for being null, and if its null, assign a value to it.
– Aman Alam
Apr 7 '11 at 12:55
1
Related: Does Java support default parameter values?
– eldarerathis
Apr 7 '11 at 12:57
Possible duplicate of Java optional parameters
– T.Todua
Jan 12 '17 at 8:29
add a comment |
Android function
PHP example:
function HaHa($a = "Test")
{
print $a;
}
The question is how to do it in android...
public void someFunction(int ttt = 5)
{
// something
}
The solution above doesn't work, how can I do it?
Thanks!
java variables arguments default
Android function
PHP example:
function HaHa($a = "Test")
{
print $a;
}
The question is how to do it in android...
public void someFunction(int ttt = 5)
{
// something
}
The solution above doesn't work, how can I do it?
Thanks!
java variables arguments default
java variables arguments default
edited Jul 22 '18 at 8:44
Yogesh Suthar
26.7k175891
26.7k175891
asked Apr 7 '11 at 12:53
M.V.
95472652
95472652
I don't think we can do that in Java. and I just can make a wild guess, if I remember correctly, that this is a feature that is supposed to be in Java 7. So in this case, one can check the variablettt
for being null, and if its null, assign a value to it.
– Aman Alam
Apr 7 '11 at 12:55
1
Related: Does Java support default parameter values?
– eldarerathis
Apr 7 '11 at 12:57
Possible duplicate of Java optional parameters
– T.Todua
Jan 12 '17 at 8:29
add a comment |
I don't think we can do that in Java. and I just can make a wild guess, if I remember correctly, that this is a feature that is supposed to be in Java 7. So in this case, one can check the variablettt
for being null, and if its null, assign a value to it.
– Aman Alam
Apr 7 '11 at 12:55
1
Related: Does Java support default parameter values?
– eldarerathis
Apr 7 '11 at 12:57
Possible duplicate of Java optional parameters
– T.Todua
Jan 12 '17 at 8:29
I don't think we can do that in Java. and I just can make a wild guess, if I remember correctly, that this is a feature that is supposed to be in Java 7. So in this case, one can check the variable
ttt
for being null, and if its null, assign a value to it.– Aman Alam
Apr 7 '11 at 12:55
I don't think we can do that in Java. and I just can make a wild guess, if I remember correctly, that this is a feature that is supposed to be in Java 7. So in this case, one can check the variable
ttt
for being null, and if its null, assign a value to it.– Aman Alam
Apr 7 '11 at 12:55
1
1
Related: Does Java support default parameter values?
– eldarerathis
Apr 7 '11 at 12:57
Related: Does Java support default parameter values?
– eldarerathis
Apr 7 '11 at 12:57
Possible duplicate of Java optional parameters
– T.Todua
Jan 12 '17 at 8:29
Possible duplicate of Java optional parameters
– T.Todua
Jan 12 '17 at 8:29
add a comment |
6 Answers
6
active
oldest
votes
No, Java does not support default values for function parameteres. There's an interesting post about borrowing language features here: http://java.dzone.com/news/default-argument-values-java
I thought so but native functions support it somehow... I can't actually remember one... Or for example JSONArray can be built from different sources: String, JSONTokener x, Collection collection or blank... I thought there could be work around
– M.V.
Apr 7 '11 at 13:02
Well work around for me is to put String inside and can add or remove arguments...
– M.V.
Apr 7 '11 at 13:03
In this article it is great workaround... Thanks!
– M.V.
Apr 7 '11 at 14:16
Glad you found it helpful :)
– Bendihossan
Apr 7 '11 at 14:36
thank you forNo
.
– Muhammad Saqib
Jan 30 '17 at 17:34
add a comment |
You can abuse overloading like this:
int someMethod() { return someMethod(42); }
int someMethod(int arg) { .... }
add a comment |
No need to overload anything, just write:
public int getScore(int score, Integer... bonus)
{
if(bonus.length > 0)
{
return score + bonus[0];
}
else
{
return score;
}
}
I think this is better. It can be simplified such asreturn bonus.length > 0 ? score + bonus[0] : score;
– Sithu
Feb 25 '16 at 7:25
I like your way.
– Malv
Nov 21 '18 at 9:00
add a comment |
you can use 3 dots
syntax:
public void doSomething(boolean... arg1) {
boolean MyArg1= (arg1.length >= 1) ? arg1 : false;
}
add a comment |
You can use overloading a function .
The overloading is also okay but in case you need default values for multiple arguments you will end up creating having many methods with all possible combination of default arguments, for the example you use imagine you want to have a default value for the 3 arguments. you will end up with this
public void methodA(A arg1) { }
public void methodA( B arg2,) { }
public void methodA(C arg3) { }
public void methodA(A arg1, B arg2) { }
public void methodA(A arg1, C arg3) { }
public void methodA( B arg2, C arg3) { }
public void methodA(A arg1, B arg2, C arg3) { }
So here's the hack i did for me , you can also use
public static void main(String args)
{
defaultParameter();
defaultParameter(true);
}
public static void defaultParameter(Boolean ...gender)
{
boolean genderBoolean = false; // It the default value you want to give
if(gender.length == 1)
{
genderBoolean = gender[0]; // Overrided Value
}
System.out.println(genderBoolean);
}
The above code will genrate result
false
true
I find out here the example java-default-parameter-values
add a comment |
Java doesn't support a syntax that will do what you want.
Perhaps at the start of someFunction(int), you could just check the incoming value, and assign a different value if you don't like what's coming in.
if (ttt == 0) ttt = 5;
Please note this question appears to have nothing to do with android, and so is improperly tagged.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5581360%2fandroid-how-to-set-a-default-value-for-an-argument-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
No, Java does not support default values for function parameteres. There's an interesting post about borrowing language features here: http://java.dzone.com/news/default-argument-values-java
I thought so but native functions support it somehow... I can't actually remember one... Or for example JSONArray can be built from different sources: String, JSONTokener x, Collection collection or blank... I thought there could be work around
– M.V.
Apr 7 '11 at 13:02
Well work around for me is to put String inside and can add or remove arguments...
– M.V.
Apr 7 '11 at 13:03
In this article it is great workaround... Thanks!
– M.V.
Apr 7 '11 at 14:16
Glad you found it helpful :)
– Bendihossan
Apr 7 '11 at 14:36
thank you forNo
.
– Muhammad Saqib
Jan 30 '17 at 17:34
add a comment |
No, Java does not support default values for function parameteres. There's an interesting post about borrowing language features here: http://java.dzone.com/news/default-argument-values-java
I thought so but native functions support it somehow... I can't actually remember one... Or for example JSONArray can be built from different sources: String, JSONTokener x, Collection collection or blank... I thought there could be work around
– M.V.
Apr 7 '11 at 13:02
Well work around for me is to put String inside and can add or remove arguments...
– M.V.
Apr 7 '11 at 13:03
In this article it is great workaround... Thanks!
– M.V.
Apr 7 '11 at 14:16
Glad you found it helpful :)
– Bendihossan
Apr 7 '11 at 14:36
thank you forNo
.
– Muhammad Saqib
Jan 30 '17 at 17:34
add a comment |
No, Java does not support default values for function parameteres. There's an interesting post about borrowing language features here: http://java.dzone.com/news/default-argument-values-java
No, Java does not support default values for function parameteres. There's an interesting post about borrowing language features here: http://java.dzone.com/news/default-argument-values-java
answered Apr 7 '11 at 12:58
Bendihossan
99931125
99931125
I thought so but native functions support it somehow... I can't actually remember one... Or for example JSONArray can be built from different sources: String, JSONTokener x, Collection collection or blank... I thought there could be work around
– M.V.
Apr 7 '11 at 13:02
Well work around for me is to put String inside and can add or remove arguments...
– M.V.
Apr 7 '11 at 13:03
In this article it is great workaround... Thanks!
– M.V.
Apr 7 '11 at 14:16
Glad you found it helpful :)
– Bendihossan
Apr 7 '11 at 14:36
thank you forNo
.
– Muhammad Saqib
Jan 30 '17 at 17:34
add a comment |
I thought so but native functions support it somehow... I can't actually remember one... Or for example JSONArray can be built from different sources: String, JSONTokener x, Collection collection or blank... I thought there could be work around
– M.V.
Apr 7 '11 at 13:02
Well work around for me is to put String inside and can add or remove arguments...
– M.V.
Apr 7 '11 at 13:03
In this article it is great workaround... Thanks!
– M.V.
Apr 7 '11 at 14:16
Glad you found it helpful :)
– Bendihossan
Apr 7 '11 at 14:36
thank you forNo
.
– Muhammad Saqib
Jan 30 '17 at 17:34
I thought so but native functions support it somehow... I can't actually remember one... Or for example JSONArray can be built from different sources: String, JSONTokener x, Collection collection or blank... I thought there could be work around
– M.V.
Apr 7 '11 at 13:02
I thought so but native functions support it somehow... I can't actually remember one... Or for example JSONArray can be built from different sources: String, JSONTokener x, Collection collection or blank... I thought there could be work around
– M.V.
Apr 7 '11 at 13:02
Well work around for me is to put String inside and can add or remove arguments...
– M.V.
Apr 7 '11 at 13:03
Well work around for me is to put String inside and can add or remove arguments...
– M.V.
Apr 7 '11 at 13:03
In this article it is great workaround... Thanks!
– M.V.
Apr 7 '11 at 14:16
In this article it is great workaround... Thanks!
– M.V.
Apr 7 '11 at 14:16
Glad you found it helpful :)
– Bendihossan
Apr 7 '11 at 14:36
Glad you found it helpful :)
– Bendihossan
Apr 7 '11 at 14:36
thank you for
No
.– Muhammad Saqib
Jan 30 '17 at 17:34
thank you for
No
.– Muhammad Saqib
Jan 30 '17 at 17:34
add a comment |
You can abuse overloading like this:
int someMethod() { return someMethod(42); }
int someMethod(int arg) { .... }
add a comment |
You can abuse overloading like this:
int someMethod() { return someMethod(42); }
int someMethod(int arg) { .... }
add a comment |
You can abuse overloading like this:
int someMethod() { return someMethod(42); }
int someMethod(int arg) { .... }
You can abuse overloading like this:
int someMethod() { return someMethod(42); }
int someMethod(int arg) { .... }
answered Apr 7 '11 at 15:20
Ingo
31.7k54489
31.7k54489
add a comment |
add a comment |
No need to overload anything, just write:
public int getScore(int score, Integer... bonus)
{
if(bonus.length > 0)
{
return score + bonus[0];
}
else
{
return score;
}
}
I think this is better. It can be simplified such asreturn bonus.length > 0 ? score + bonus[0] : score;
– Sithu
Feb 25 '16 at 7:25
I like your way.
– Malv
Nov 21 '18 at 9:00
add a comment |
No need to overload anything, just write:
public int getScore(int score, Integer... bonus)
{
if(bonus.length > 0)
{
return score + bonus[0];
}
else
{
return score;
}
}
I think this is better. It can be simplified such asreturn bonus.length > 0 ? score + bonus[0] : score;
– Sithu
Feb 25 '16 at 7:25
I like your way.
– Malv
Nov 21 '18 at 9:00
add a comment |
No need to overload anything, just write:
public int getScore(int score, Integer... bonus)
{
if(bonus.length > 0)
{
return score + bonus[0];
}
else
{
return score;
}
}
No need to overload anything, just write:
public int getScore(int score, Integer... bonus)
{
if(bonus.length > 0)
{
return score + bonus[0];
}
else
{
return score;
}
}
answered Nov 3 '13 at 8:35
Hamzeh Soboh
4,42052850
4,42052850
I think this is better. It can be simplified such asreturn bonus.length > 0 ? score + bonus[0] : score;
– Sithu
Feb 25 '16 at 7:25
I like your way.
– Malv
Nov 21 '18 at 9:00
add a comment |
I think this is better. It can be simplified such asreturn bonus.length > 0 ? score + bonus[0] : score;
– Sithu
Feb 25 '16 at 7:25
I like your way.
– Malv
Nov 21 '18 at 9:00
I think this is better. It can be simplified such as
return bonus.length > 0 ? score + bonus[0] : score;
– Sithu
Feb 25 '16 at 7:25
I think this is better. It can be simplified such as
return bonus.length > 0 ? score + bonus[0] : score;
– Sithu
Feb 25 '16 at 7:25
I like your way.
– Malv
Nov 21 '18 at 9:00
I like your way.
– Malv
Nov 21 '18 at 9:00
add a comment |
you can use 3 dots
syntax:
public void doSomething(boolean... arg1) {
boolean MyArg1= (arg1.length >= 1) ? arg1 : false;
}
add a comment |
you can use 3 dots
syntax:
public void doSomething(boolean... arg1) {
boolean MyArg1= (arg1.length >= 1) ? arg1 : false;
}
add a comment |
you can use 3 dots
syntax:
public void doSomething(boolean... arg1) {
boolean MyArg1= (arg1.length >= 1) ? arg1 : false;
}
you can use 3 dots
syntax:
public void doSomething(boolean... arg1) {
boolean MyArg1= (arg1.length >= 1) ? arg1 : false;
}
edited Dec 27 '18 at 20:12
answered Jan 12 '17 at 8:31
T.Todua
29.8k11131130
29.8k11131130
add a comment |
add a comment |
You can use overloading a function .
The overloading is also okay but in case you need default values for multiple arguments you will end up creating having many methods with all possible combination of default arguments, for the example you use imagine you want to have a default value for the 3 arguments. you will end up with this
public void methodA(A arg1) { }
public void methodA( B arg2,) { }
public void methodA(C arg3) { }
public void methodA(A arg1, B arg2) { }
public void methodA(A arg1, C arg3) { }
public void methodA( B arg2, C arg3) { }
public void methodA(A arg1, B arg2, C arg3) { }
So here's the hack i did for me , you can also use
public static void main(String args)
{
defaultParameter();
defaultParameter(true);
}
public static void defaultParameter(Boolean ...gender)
{
boolean genderBoolean = false; // It the default value you want to give
if(gender.length == 1)
{
genderBoolean = gender[0]; // Overrided Value
}
System.out.println(genderBoolean);
}
The above code will genrate result
false
true
I find out here the example java-default-parameter-values
add a comment |
You can use overloading a function .
The overloading is also okay but in case you need default values for multiple arguments you will end up creating having many methods with all possible combination of default arguments, for the example you use imagine you want to have a default value for the 3 arguments. you will end up with this
public void methodA(A arg1) { }
public void methodA( B arg2,) { }
public void methodA(C arg3) { }
public void methodA(A arg1, B arg2) { }
public void methodA(A arg1, C arg3) { }
public void methodA( B arg2, C arg3) { }
public void methodA(A arg1, B arg2, C arg3) { }
So here's the hack i did for me , you can also use
public static void main(String args)
{
defaultParameter();
defaultParameter(true);
}
public static void defaultParameter(Boolean ...gender)
{
boolean genderBoolean = false; // It the default value you want to give
if(gender.length == 1)
{
genderBoolean = gender[0]; // Overrided Value
}
System.out.println(genderBoolean);
}
The above code will genrate result
false
true
I find out here the example java-default-parameter-values
add a comment |
You can use overloading a function .
The overloading is also okay but in case you need default values for multiple arguments you will end up creating having many methods with all possible combination of default arguments, for the example you use imagine you want to have a default value for the 3 arguments. you will end up with this
public void methodA(A arg1) { }
public void methodA( B arg2,) { }
public void methodA(C arg3) { }
public void methodA(A arg1, B arg2) { }
public void methodA(A arg1, C arg3) { }
public void methodA( B arg2, C arg3) { }
public void methodA(A arg1, B arg2, C arg3) { }
So here's the hack i did for me , you can also use
public static void main(String args)
{
defaultParameter();
defaultParameter(true);
}
public static void defaultParameter(Boolean ...gender)
{
boolean genderBoolean = false; // It the default value you want to give
if(gender.length == 1)
{
genderBoolean = gender[0]; // Overrided Value
}
System.out.println(genderBoolean);
}
The above code will genrate result
false
true
I find out here the example java-default-parameter-values
You can use overloading a function .
The overloading is also okay but in case you need default values for multiple arguments you will end up creating having many methods with all possible combination of default arguments, for the example you use imagine you want to have a default value for the 3 arguments. you will end up with this
public void methodA(A arg1) { }
public void methodA( B arg2,) { }
public void methodA(C arg3) { }
public void methodA(A arg1, B arg2) { }
public void methodA(A arg1, C arg3) { }
public void methodA( B arg2, C arg3) { }
public void methodA(A arg1, B arg2, C arg3) { }
So here's the hack i did for me , you can also use
public static void main(String args)
{
defaultParameter();
defaultParameter(true);
}
public static void defaultParameter(Boolean ...gender)
{
boolean genderBoolean = false; // It the default value you want to give
if(gender.length == 1)
{
genderBoolean = gender[0]; // Overrided Value
}
System.out.println(genderBoolean);
}
The above code will genrate result
false
true
I find out here the example java-default-parameter-values
answered Aug 19 '16 at 11:14
Jigar Shah
312
312
add a comment |
add a comment |
Java doesn't support a syntax that will do what you want.
Perhaps at the start of someFunction(int), you could just check the incoming value, and assign a different value if you don't like what's coming in.
if (ttt == 0) ttt = 5;
Please note this question appears to have nothing to do with android, and so is improperly tagged.
add a comment |
Java doesn't support a syntax that will do what you want.
Perhaps at the start of someFunction(int), you could just check the incoming value, and assign a different value if you don't like what's coming in.
if (ttt == 0) ttt = 5;
Please note this question appears to have nothing to do with android, and so is improperly tagged.
add a comment |
Java doesn't support a syntax that will do what you want.
Perhaps at the start of someFunction(int), you could just check the incoming value, and assign a different value if you don't like what's coming in.
if (ttt == 0) ttt = 5;
Please note this question appears to have nothing to do with android, and so is improperly tagged.
Java doesn't support a syntax that will do what you want.
Perhaps at the start of someFunction(int), you could just check the incoming value, and assign a different value if you don't like what's coming in.
if (ttt == 0) ttt = 5;
Please note this question appears to have nothing to do with android, and so is improperly tagged.
answered Apr 7 '11 at 13:02
Thane Anthem
3,58932223
3,58932223
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5581360%2fandroid-how-to-set-a-default-value-for-an-argument-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I don't think we can do that in Java. and I just can make a wild guess, if I remember correctly, that this is a feature that is supposed to be in Java 7. So in this case, one can check the variable
ttt
for being null, and if its null, assign a value to it.– Aman Alam
Apr 7 '11 at 12:55
1
Related: Does Java support default parameter values?
– eldarerathis
Apr 7 '11 at 12:57
Possible duplicate of Java optional parameters
– T.Todua
Jan 12 '17 at 8:29