4

i was wondering if java have a "default value assignation" (i really don't know how to call it) like what you can do in javascript or python.

For example in js you can do this:

var myVar = variable || "default value";

and if variable contains a value, myVar will have the value of variable otherwise myVar will be the string "default value".

Also in python you can do this:

a = "hello" 
b = a or "good bye" 
print b #this will print "hello" 
a = None
b = a or "good bye" 
print b #this will print "good bye"

Can you do this something similar in java? Usually when i have to do this in Java, I do something like this:

String a = b != null ? b : "default value";

But I think what you can do in js or python it's much more readable. Also can you tell me what's the "name" of this ? I know the || symbol it's an or operator, but how it's called when you do this "defaultvalue" thing. Thanks

agmezr
  • 350
  • 2
  • 15
  • 3
    The example code you posted in your question is the way to do this in Java. For clarity, I'd recommend adding parenthesis so it reads `String a = ((b != null) ? b : "default value")` – Krease Oct 15 '14 at 17:09
  • Java will not examine the truthiness of a variable like Javascript. – Elliott Frisch Oct 15 '14 at 17:10
  • Related: In C#, you can do this with the [null-coalescing operator](http://msdn.microsoft.com/en-us/library/ms173224.aspx) `??`. See: [Is there a java equivalent to null-coalescing operator in C#?](http://stackoverflow.com/questions/5223044/is-there-a-java-equivalent-to-null-coalescing-operator-in-c) – Luke Willis Oct 15 '14 at 17:14
  • 2
    @Tichodroma tried what? Tried using the syntax for default values from every other programming language that has them? "Have you tried it" is a good response to people who ask "what happens when I do X?", but it doesn't work so well for "does Java have a feature like feature X from programming language Y." – Solomon Slow Oct 15 '14 at 17:14
  • Your python example actually results in an exception, since, unlike in javascript, referencing the nonexistent `c` variable won't result in `null`/`None`. – a p Oct 15 '14 at 17:18
  • @ap thanks, i changed the code – agmezr Oct 15 '14 at 17:20
  • @Chris yes, the parentheses are useful, but unfortunately it's not as readable as python – agmezr Oct 15 '14 at 17:30

5 Answers5

8

Since Java is more statically typed than JavaScript or Python, I'm afraid not. Considering this example:

myVar = variable || "default value";

In order for this to be executed at all in Java, variable and "default value" would have to be Booleans, since they're being used in a boolean logic conditional operator. And the result (myVar) would also be a Boolean.

Since the types have to be more statically explicit in Java, the full comparison expression needs to be defined:

myVar = (variable != null) ? variable : "default value";

In this case the boolean expression is the condition being examined, and variable and "default value" need to be of the same type. Which will also be the resulting type of myVar.

In JavaScript (as well as just about any dynamic type system I've used), pretty much anything can be examined as a boolean value. (Anything can be "truthy".) In statically typed languages like Java or C#, however, that's generally not the case.

David
  • 208,112
  • 36
  • 198
  • 279
4

No, there is no default value for method return types in Java.

In plain old Java, you would write something like:

if (value != null) {
  return value;
else {
  return "default";
}

or you could simplify that to

return value != null ? value : "default";

In Java8, there is the possibility to use Optional<T>. That means, you can hold a value, or not. This would look like this:

Optional<String> optionalVal = Optional.ofNullable(null); // or Optional.of("Foo");
return optionalVal.getOrElse("default");
Martin Seeler
  • 6,874
  • 3
  • 33
  • 45
1

There is no similar expression in Java (other than the ternary operator, which you have already described).

In certain scripting languages like JS, Python, and Lua, and and or operations will return the last value considered (the first true value or last false value for or, and the first false value or last true value for and) rather than a boolean, which is why the var myvar = param || "default" tricks works.

In Java however, the && and || operators only work with boolean values, so the above behavior doesn't apply.

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
1

The reason this trick works in Javascript and Python is that || can be applied to non-booleans and returns one of its arguments depending on their truthiness.

In Java, || can only be applied to booleans, meaning the trick doesn't work.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0
String a = b != null ? b : "default value";

This is a shorthand for:

if(b != null) a = b;
else a = "default value";
TwilightTitus
  • 190
  • 1
  • 9