Tutorial by Examples: all

Java 7 introduced the very useful Files class Java SE 7 import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.Path; Path path = Paths.get("path/to/file"); try { byte[] data = Files.readAllBytes(path); } catch(IOException e) { e.printStackTrace();...
If your computation produces some return value which later is required, a simple Runnable task isn't sufficient. For such cases you can use ExecutorService.submit(Callable<T>) which returns a value after execution completes. The Service will return a Future which you can use to retrieve the r...
import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class User { private long userID; private String name; // getters and setters } By using the annotation XMLRootElement, we can mark a class as a root element of an XML file. import java.io.File; ...
To read an XML file named UserDetails.xml with the below content <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <user> <name>Jon Skeet</name> <userID>8884321</userID> </user> We need a POJO class named U...
Static and member methods in Java can be marked as native to indicate that their implementation is to be found in a shared library file. Upon execution of a native method, the JVM looks for a corresponding function in loaded libraries (see Loading native libraries), using a simple name mangling sche...
Calling a Java method from native code is a two-step process : obtain a method pointer with the GetMethodID JNI function, using the method name and descriptor ; call one of the Call*Method functions listed here. Java code /*** com.example.jni.JNIJavaCallback.java ***/ package com.example....
Overview In this example 2 clients send information to each other through a server. One client sends the server a number which is relayed to the second client. The second client halves the number and sends it back to the first client through the server. The first client does the same. The server st...
Variables can be accessed via dynamic variable names. The name of a variable can be stored in another variable, allowing it to be accessed dynamically. Such variables are known as variable variables. To turn a variable into a variable variable, you put an extra $ put in front of your variable. $va...
Given a list comprehension you can append one or more if conditions to filter values. [<expression> for <element> in <iterable> if <condition>] For each <element> in <iterable>; if <condition> evaluates to True, add <expression> (usually a function...
Consider a database with the following two tables. Employees table: IdFNameLNameDeptId1JamesSmith32JohnJohnson4 Departments table: IdName1Sales2Marketing3Finance4IT Simple select statement * is the wildcard character used to select all available columns in a table. When used as a substitute f...
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...
The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds. It is also a trivial way to achieve an asynchronous operation. In this example calling the wait function resolves the promise after the time specified as first argument: function wait(ms) ...
git add -A 2.0 git add . In version 2.x, git add . will stage all changes to files in the current directory and all its subdirectories. However, in 1.x it will only stage new and modified files, not deleted files. Use git add -A, or its equivalent command git add --all, to stage all ch...
from module_name import * for example: from math import * sqrt(2) # instead of math.sqrt(2) ceil(2.7) # instead of math.ceil(2.7) This will import all names defined in the math module into the global namespace, other than names that begin with an underscore (which indicates that the wri...
Instead of this lambda-function that calls the method explicitly: alist = ['wolf', 'sheep', 'duck'] list(filter(lambda x: x.startswith('d'), alist)) # Keep only elements that start with 'd' # Output: ['duck'] one could use a operator-function that does the same: from operator import metho...
Python's str type also has a method for replacing occurences of one sub-string with another sub-string in a given string. For more demanding cases, one can use re.sub. str.replace(old, new[, count]): str.replace takes two arguments old and new containing the old sub-string which is to be replace...
A quick way to make a copy of an array (as opposed to assigning a variable with another reference to the original array) is: arr[:] Let's examine the syntax. [:] means that start, end, and slice are all omitted. They default to 0, len(arr), and 1, respectively, meaning that subarray that we are ...
class adder(object): def __init__(self, first): self.first = first # a(...) def __call__(self, second): return self.first + second add2 = adder(2) add2(1) # 3 add2(2) # 4
This example uses the Cars Table from the Example Databases. UPDATE Cars SET Status = 'READY' This statement will set the 'status' column of all rows of the 'Cars' table to "READY" because it does not have a WHERE clause to filter the set of rows.
reduce will not terminate the iteration before the iterable has been completly iterated over so it can be used to create a non short-circuit any() or all() function: import operator # non short-circuit "all" reduce(operator.and_, [False, True, True, True]) # = False # non short-circu...

Page 2 of 113