Posts

Showing posts from October, 2020

Slices as arguments in Golang

Image
I recently came across several examples of using slices as function arguments, which lead to unexpected results. This post focuses on these examples. First example. The slice is passed to the function as an argument where it will be modified. https://play.golang.org/p/r5mKX5ErwLC package main import "fmt" func change(abc []int) { for i := range abc { abc[i] = 4 } fmt.Println(abc) } func main() { abc := []int{1, 2, 3} change(abc) fmt.Println(abc) } Output: [4 4 4] [4 4 4] Someone might expect that when we pass a slice to a function, we will get a copy of it in the function, and the changes will only appear in the function. But this is not the case. Slices in Go have a base array and are passed by reference, not value. We just pass the slice reference to the function, then change it, and it changes the outer slice. Second example. https://play.golang.org/p/5ruLrp6ZJJc package main import "fmt" func change(abc ...

volatile keyword in Java

Image
Defining a variable with the volatile keyword means that the value of that variable can be changed by other threads. To understand what volatile does, it is helpful to understand how threads handle regular variables. For performance reasons, the Java language specification allows the JRE to store a local copy of the variable for each thread that references it. These "local" copies of variables are like a cache and help a thread avoid accessing main memory every time it needs to retrieve the value of a variable. When two threads start, one of them reads variable A as 10, and the other as 20. If the value of variable A has changed from 10 to 20, then the first thread will not know about the change and will store the wrong value A. But if variable A is marked as volatile, then whenever the thread reads the value of A, it will access the master copy of A and read its current value. A thread-local cache makes sense if the variables in your applications will not be changed extern...

Methods for reading XML in Java

Image
DOM (Document Object Model) - object - reads XML, recreating it in memory in the form of an object structure, while the XML document is represented as a set of tags - nodes. Each node can have an unlimited number of child nodes. Each child can also contain several levels of descendants or none at all. Thus, the result is a kind of tree. Pluses: Easy to program. If there are many objects in XML with cross-references to each other, it is enough to go through the document twice: first time create objects without references and fill in the name-object dictionary, the second time - restore links. If an error occurs in XML, a half-created XML structure remains in memory, which will be automatically destroyed. Suitable for both reading and writing. Minuses: Low speed. Wastes a lot of memory. SAX (Simple API for XML) - event-driven - reads an XML document, responding to emerging events (opening or closing tag, string, attribute) by calling the event handlers provided by the appl...

XSD

Image
XSD, XML Schema Definition, XML Schema - a language for describing the structure of an XML document. In particular, XML Schema describes: dictionary - names of elements and attributes; content model - the relationship between elements and attributes, as well as their structure of the document; used data types. The advantages of XSD over DTD are as follows: DTD, unlike XSD, is not XML and has its own syntax. As a result, various problems with encoding and verifying XML documents can arise. When using XSD, the XML parser can check not only the syntax of an XML document, but also its structure, content model, and data types. In XML DTD, there is only one data type - a string, and if, for example, there is text in a numeric field, then the document will still be able to pass verification, since the XML DTD will not be able to check the data type. You cannot map more than one DTD to one XML document. Therefore, a document can be verified with only one DTD description. XSD is exte...

XML, well-formed XML and valid XML

Image
XML, the eXtensible Markup Language, is a language with a simple formal syntax, well suited for creating and manipulating documents with programs, and at the same time easy to read and create documents by humans. XML is extensible, it does not fix the markup used in documents, and the developer is free to create markup in accordance with the needs of a particular area, being limited only by the syntax rules of the language. DTD A DTD, Document Type Definition, is a predefined set of rules that defines the relationships between elements and attributes. For example, the DTD for HTML says that the DIV tag must be inside the BODY tag and can appear multiple times, TITLE - in HEAD and only once, and SCRIPT - both there and there as many times as you want. A DTD is usually described directly in the document as a formulation line starting with <!DOCTYPE ... > or in a separate file. Difference between well-formed XML and valid XML Depending on the level of compliance, the document...