Tutorial by Examples: basic

The nameof operator allows you to get the name of a variable, type or member in string form without hard-coding it as a literal. The operation is evaluated at compile-time, which means that you can rename, using an IDE's rename feature, a referenced identifier and the name string will update with it...
This is an example of a default build.gradle file in a module. apply plugin: 'com.android.application' android { compileSdkVersion 25 buildToolsVersion '25.0.3' signingConfigs { applicationName { keyAlias 'applicationName' keyPassword 'password...
Gradle (Module:app) Configuration android { .... dataBinding { enabled = true } } Data model public class Item { public String name; public String description; public Item(String name, String description) { this.name = name; this.descr...
/** * 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...
In Android Activities and Services, most callbacks are run on the main thread. This makes it simple to update the UI, but running processor- or I/O-heavy tasks on the main thread can cause your UI to pause and become unresponsive (official documentation on what then happens). You can remedy this by...
If you have many tasks to execute, and all these tasks are not dependent of the result of the precedent ones, you can use Multithreading for your computer to do all this tasks at the same time using more processors if your computer can. This can make your program execution faster if you have some bi...
For basic prototypes or basic command-line behavior, the following loop comes in handy. public class ExampleCli { private static final String CLI_LINE = "example-cli>"; //console like string private static final String CMD_QUIT = "quit"; //string for exit...
Server: Start, and wait for incoming connections //Open a listening "ServerSocket" on port 1234. ServerSocket serverSocket = new ServerSocket(1234); while (true) { // Wait for a client connection. // Once a client connected, we get a "Socket" object // that c...
Enum can be considered to be syntax sugar for a sealed class that is instantiated only a number of times known at compile-time to define a set of constants. A simple enum to list the different seasons would be declared as follows: public enum Season { WINTER, SPRING, SUMMER, FA...
PrinterJob pJ = PrinterJob.createPrinterJob(); if (pJ != null) { boolean success = pJ.printPage(some-node); if (success) { pJ.endJob(); } } This prints to the default printer without showing any dialog to the user. To use a printer other than the default you can use th...
The map function is the simplest one among Python built-ins used for functional programming. map() applies a specified function to each element in an iterable: names = ['Fred', 'Wilma', 'Barney'] Python 3.x3.0 map(len, names) # map in Python 3.x is a class; its instances are iterable # Out: &...
To filter discards elements of a sequence based on some criteria: names = ['Fred', 'Wilma', 'Barney'] def long_name(name): return len(name) > 5 Python 2.x2.0 filter(long_name, names) # Out: ['Barney'] [name for name in names if len(name) > 5] # equivalent list comprehension #...
let number = 3 switch number { case 1: print("One!") case 2: print("Two!") case 3: print("Three!") default: print("Not One, Two or Three") } switch statements also work with data types other than integers. They work with any data t...
An enum provides a set of related values: enum Direction { case up case down case left case right } enum Direction { case up, down, left, right } Enum values can be used by their fully-qualified name, but you can omit the type name when it can be inferred: let dir = Dire...
About Protocols A Protocol specifies initialisers, properties, functions, subscripts and associated types required of a Swift object type (class, struct or enum) conforming to the protocol. In some languages similar ideas for requirement specifications of subsequent objects are known as ‘interfaces...
struct Repository { let identifier: Int let name: String var description: String? } This defines a Repository struct with three stored properties, an integer identifier, a string name, and an optional string description. The identifier and name are constants, as they've been decla...
A basic join (also called "inner join") queries data from two tables, with their relationship defined in a join clause. The following example will select employees' first names (FName) from the Employees table and the name of the department they work for (Name) from the Departments table:...
Closures (also known as blocks or lambdas) are pieces of code which can be stored and passed around within your program. let sayHi = { print("Hello") } // The type of sayHi is "() -> ()", aka "() -> Void" sayHi() // prints "Hello" Like other fun...
This a Basic example for using the MVVM model in a windows desktop application, using WPF and C#. The example code implements a simple "user info" dialog. The View The XAML <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> ...
Functions in Swift may return values, throw errors, or both: func reticulateSplines() // no return value and no error func reticulateSplines() -> Int // always returns a value func reticulateSplines() throws // no return value, but may throw an error func reticulat...

Page 2 of 43