Posts

Showing posts with the label Explanation / Reference

Wha is a Call Site in Programming?

Image
A call site is the location, the line of code where a specific function  is called by passing to it its arguments and receiving its return value.

Comparison: Rust vs Go

Image
"They are not meant to be competing languages. They fill different needs, designed with different approaches and excels at different things. If you want to write low-level stuff, something that needs every tiny bit of performance, working directly with the hardware or graphics, and you want to handle the memory. Basically something that you'd use C/C++ to write, something like kernel, driver, graphics library, a big library that exposes a C interface for other languages to call, a game that couldn't work well if there are GC pauses, or something like what Mozilla is building: Servo, a new browser engine. Then go with Rust, just choose Rust, or plain C/C++ for that. I've made the mistake of "fanboyingly" choosing Go for this kind of task and it hurts, it's really stupid and inappropriate, even though Go can definitely do some, it's awkward. On the other hand, if you want to do something like writing a tool, a server, a web application, a network ap...

Short Definition: Pessimistic Locking vs Optimistic Locking

Image
In Pessimistic Locking a lock is always applied to a modifiable resource once it is accessed in an — even potentially — modifiable transaction. A lock prevents other requests from modifying the resource as the current transaction proceeds. This is necessary to prevent other requests from modifying a resource whereas the current transaction assumes a certain state that is not given anymore as the resource has already been modified by a faster processing operation. This is the core problem that can also be addressed in a different way. In Optimistic Locking , no lock is applied . Instead once a resource is accessed, its state is temporary saved . After the transaction has modified the resource and is about to commit the modified resource to persistent storage, the resource is read again from persistent storage and compared to the temporary saved state from the beginning of the transaction. If the retrieved state differs from the previously temporary saved state of the ...

Difference Between a Parameter & Argument

Image
A parameter is part of a method's signature declaration and is used as a receiving container for an argument . An argument is an actual value literal that is passed to a method that performs operations based on and an arguments value. This graphic illustrates the difference:

GoF Architectural Design Patterns for OOD

Creational Patterns Abstract Factory Builder Factory Method Prototype Singleton Structural Patterns Adapter Bridge Composite Decorator Facade Flyweight Proxy Behavioral Patterns Chain of Responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template Method Visitor

In-Depth Understanding JUnit Tests

Source code on GitHub public class ExampleTest {     private static final Logger LOG = LoggerFactory.getLogger(ExampleTest.class);     /**      * This method won't be executed if the unit test will be (externally) terminated (e.g. from within the IDE).      * You have to act **really** fast in this case.      */     @BeforeClass     public static void beforeClass() throws Exception {         LOG.info("@BeforeClass"); // will be always executed, even if a test fails throwing any Exception     }     /**      * This method won't be executed if the unit test will be (externally) terminated (e.g. from within the IDE).      */     @AfterClass     public static void afterClass() throws Exception {         LOG.info("@AfterClass"); // will be always executed, even if a test fails throwin...

Profilers for Java & Java8 w/ Very Low Overhead

Both are part of the official JDK8 package and don't need any further plugins to get started. Java Mission Control (jmc) is a production system ready profiler from Oracle that has an overhead of <1% and is suitable for profiling production systems Location: $JAVA_HOME/bin/jmc JVisualVM (jvisualvm) is a profiler for Oracle that is good enough for resolution of most issues that required a profiler, a nice profiler for getting started  Location: $JAVA_HOME/bin/jvisualvm

Builder Pattern in Java

The builder pattern can be used to provide a kind of virtual dynamically extendable constructor to build e.g. a model class for RESTful requests where not all fields are required but many fields are optional. public class ClientRequestModel {     private String url;     private String headers;     private String parameters;     private String body;     public ClientRequestModel(Builder builder) {         url = builder.url;     }     public String getUrl() {         return url;     }     public String getHeaders() {         return headers;     }     public String getParameters() {         return parameters;     }     public String getBody() {         return body;     }     @Override     public String toS...

How to Assure Code Quality & Standards

Image
Code quality is an essential part of reliable high quality software where malfunctions — once they occur — can be detected and resolved quickly. To achieve a high level of software quality, certain conditions must be established to make quality not only a result of high effort and engineering passion but the result of an explicit systemic rule set.  There are several ways for  automated  code quality assessment and validation. For JVM (Java Virtual Machine) languages there are two ways to facilitate automated code analysis: Using  static byte code based analyzers like  FindBugs , that look for (anti-) patterns in the compiled JVM language code. The advantage of this tool is that it is suitable for polyglot environments and is JVM language agnostic. On the one hand it can detect issues other kind of tools cannot (e.g.  UCF_USELESS_CONTROL_FLOW ), on the other hand it  depends on a specific class file format  that is different in every major J...

Meaning of UML diagram symbols

Image

For-each loop vs Iterator vs C-style for loop

In Java a for loop that uses an iterator List<String> strings = new LinkedList<>(Arrays.asList("first", "second")); for (Iterator iterator = strings.iterator(); iterator.hasNext();) {     System.out.println(iterator.next()); } is essentially the same as List<String> strings = new LinkedList<>(Arrays.asList("first", "second")); for (String string : strings) {     System.out.println(string); } because the generated byte code for the second code snippet executes the same operations underneath as for the first code snippet. So the for-each loop , introduced with Java 5 is merely syntactic sugar for the for loop that explicitly deals with an iterator. However this C-style for loop  works with an index List<String> strings = new LinkedList<>(Arrays.asList("first", "second")); for(int index = 0; index < strings.size(); index++) {      System.out.println(strings.get(index)); ...

Manipulate / change git' commit timestamp

After doing a regular commit, issue this command: git commit --amend --date="Mon Apr 7 07:33 2014 +0200"

@Inject vs. @Resource vs. @Autowired — Java vs. Spring

@Autowired (Spring) & @Inject (Java) Matches by type Restricts by qualifiers Matches by name @Resource   (Java) Matches by name Matches by type Restricted by qualifiers, ignored if match is found by name

NPE killer in Java8 to prevent NullPointerExceptions

In JDK8 the Optional<T> type monad has been introduced. The purpose of this class is essentially to facilitate the active thinking about the case when null  might be assigned to an object causing a NullPointerException when this object will be dereferenced. Consider this: public static Optional<Drink> find(String name, List<Drink> drinks) {     for(Drink drink : drinks) {     if(drink.getName().equals(name)) {     return Optional.of(drink);     }   }   return Optional.empty(); } List<Drink> drinks = Arrays.asList(new Drink("Beer"), new Drink("Wine"), new Drink("Cocktail")); Optional<Drink> found = find("Beer", drinks); if(found.isPresent()) {   Drink drink = found.get();   String name = drink.getName(); }

Generics: difference between a wildcard vs Object

The short version is: Collection<Object> != (Collection<?, ?> == Collection) or Map<Object, Object> != (Map<?, ?> == Map) Why? Using just  Collection  is — since Java 5 & the introduction of Generics — a short form for Collection<?> collectionOfUnknown   which is not the same as Collection<Object> collectionOfObjects  because you can assign any collection to  collectionOfUnknown   like Collection<Foo> collectionOfFoos , Collection<Bar> collectionOfBars or Collection<Object> collectionOfObjects  but you cannot assign Collection<Foo> collectionOfFoos to a Collection<Object>  collectionOfObjects .

Java's Types: Primitives vs Reference Types

In Java there are essentially two different kinds of types. The primitive type denotes non-nullable types like boolean, byte, sbyte, short, integer, float, and double . The reference type denotes all nullable types, typically known as "objects" which always have " Object " as their explicit or implicit super class, e.g. Integer, Double, YourVeryOwnBusinessObject, BusinessDao, PersonDto, RuntimeException, or Exception. Actually null  is a literal of the "null" type which has no name but can be assigned or cast to any reference type . However it cannot be assigned to primitive types. By default all reference types are initialized with null .

All Java8 keywords vs literals

These are the 50 Java keywords that are not allowed to be used as identifiers: abstract, continue, for, new, switch, assert, default, if, package, synchronized, boolean, do, goto , private, this, break, double, implements, protected, throw, byte, else, import, public, throws, case, enum, instanceof, return, transient, catch, extends, int, short, try, char, final, interface, static, void, class, finally, long, strictfp, volatile, const , float, native, super, while The const and goto  keywords are reserved and not currently used. You might wonder why  null, true, and false  have not been mentioned in the list. Those are literals and no keywords. Literals are also in-source values like "this is a string" (String literal) or numbers like " 1.234_456d " (double literal).