Tutorial by Examples: def

/** * Interface with default method */ public interface Printable { default void printString() { System.out.println( "default implementation" ); } } /** * Class which falls back to default implementation of {@link #printString()} */ public class WithDefault...
You can as well access other interface methods from within your default method. public interface Summable { int getA(); int getB(); default int calculateSum() { return getA() + getB(); } } public class Sum implements Summable { @Override public int get...
Don't just use Optional.get() since that may throw NoSuchElementException. The Optional.orElse(T) and Optional.orElseGet(Supplier<? extends T>) methods provide a way to supply a default value in case the Optional is empty. String value = "something"; return Optional.ofNullable(v...
In your build.gradle file of main module(app), define your minimum and target version number. android { //the version of sdk source used to compile your project compileSdkVersion 23 defaultConfig { //the minimum sdk version required by device to run your app minSd...
Colors are usually stored in a resource file named colors.xml in the /res/values/ folder. They are defined by <color> elements: <?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <c...
This Activity code will provide basic functionality for including a Google Map using a SupportMapFragment. The Google Maps V2 API includes an all-new way to load maps. Activities now have to implement the OnMapReadyCallBack interface, which comes with a onMapReady() method override that is execute...
Place this code in a file named HelloWorld.scala: object Hello { def main(args: Array[String]): Unit = { println("Hello World!") } } Live demo To compile it to bytecode that is executable by the JVM: $ scalac HelloWorld.scala To run it: $ scala Hello When the Scala...
Using the def statement is the most common way to define a function in python. This statement is a so called single clause compound statement with the following syntax: def function_name(parameters): statement(s) function_name is known as the identifier of the function. Since a function def...
You can't pass an empty sequence into max or min: min([]) ValueError: min() arg is an empty sequence However, with Python 3, you can pass in the keyword argument default with a value that will be returned if the sequence is empty, instead of raising an exception: max([], default=42) ...
Arguments are defined in parentheses after the function name: def divide(dividend, divisor): # The names of the function and its arguments # The arguments are available by name in the body of the function print(dividend / divisor) The function name and its list of arguments are called...
Optional arguments can be defined by assigning (using =) a default value to the argument-name: def make(action='nothing'): return action Calling this function is possible in 3 different ways: make("fun") # Out: fun make(action="sleep") # Out: sleep # The argumen...
One can give a function as many arguments as one wants, the only fixed rules are that each argument name must be unique and that optional arguments must be after the not-optional ones: def func(value1, value2, optionalvalue=10): return '{0} {1} {2}'.format(value1, value2, optionalvalue1) Wh...
Arbitrary number of positional arguments: Defining a function capable of taking an arbitrary number of arguments can be done by prefixing one of the arguments with a * def func(*args): # args will be a tuple containing all values that are passed in for i in args: print(i) fun...
There is a problem when using optional arguments with a mutable default type (described in Defining a function with optional arguments), which can potentially lead to unexpected behaviour. Explanation This problem arises because a function's default arguments are initialised once, at the point whe...
By default, attempting to lookup the value for a key which does not exist will return nil. You can optionally specify some other value to return (or an action to take) when the hash is accessed with a non-existent key. Although this is referred to as "the default value", it need not be a s...
The differences between null and undefined null and undefined share abstract equality == but not strict equality ===, null == undefined // true null === undefined // false They represent slightly different things: undefined represents the absence of a value, such as before an identifier/...
try { StyledDocument doc = new DefaultStyledDocument(); doc.insertString(0, "This is the beginning text", null); doc.insertString(doc.getLength(), "\nInserting new line at end of doc", null); MutableAttributeSet attrs = new SimpleAttributeSet(); StyleCons...
StyledDocuments generally do not implement clone, and so have to copy them in a different way if that is necessary. try { //Initialization DefaultStyledDocument sourceDoc = new DefaultStyledDocument(); DefaultStyledDocument destDoc = new DefaultStyledDocument(); ...
// zoo.php class Animal { public function eats($food) { echo "Yum, $food!"; } } $animal = new Animal(); $animal->eats('meat'); PHP knows what Animal is before executing new Animal, because PHP reads source files top-to-bottom. But what if we wanted to create ...
// autoload.php spl_autoload_register(function ($class) { require_once "$class.php"; }); // Animal.php class Animal { public function eats($food) { echo "Yum, $food!"; } } // zoo.php require 'autoload.php'; $animal = new Animal; $animal->e...

Page 2 of 27