Thursday, January 31, 2013

XSD vs DTD

Both XSD and DTD are used for defining the structure of XML file.

  1. XSD are newer than DTD.
  2. XSD allow making more complicated definitions than DTD (and are recommended to use). 
  3. XSD files are XML files.
  4. In XSD you could put the restriction on the occurrence of the elements. This is done by using attributes minOccurs and maxOccurs.

Some of the features of XSD (that DTD have not) are:
  • support of namespaces;
  • existence of data types;



Monday, December 31, 2012

Inner classes and static nested classes

Terminology
Nested classes are divided into two categories:
  • static. They are simply called static nested classes.
  • non-static. They are called inner classes. 
Ideology
Reasons for using nested classes:
  • It is a way of logically grouping classes that are only used in one place.
  • It increases encapsulation.
  • Nested classes can lead to more readable and maintainable code.

Notes
  1. Access modifiers (private/public/protected) have no effect on inner classes - independant of the modifier, the classes are public.
  2. Inner classes could access members of outer class, no meter of their access modifier.
Usage out of the outer class

Sunday, December 30, 2012

Passing method arguments - by value or by reference. Theory and examples

When Java passes method arguments (parameters) by value and when by reference?
Here we put the major cases and below you will get an working example of it.

Rules:
  • primitive types are passed by value (example in swapPrimitives function);
  • arrays data is passed by reference (example in swapInArray function);
  • reassigning the argument inside the method does not affect the variable in the caller function;
  • all methods called on the arguments are called on the real object (argument variable could be changed);
We will describe the rules with swap function. Some of the methods are wrong and do not swap its arguments. After the code is the output of the program.

Code example:

Thursday, December 20, 2012

Java tests

Question 1: You are planning to do an indexed search in a list of objects. Which of the two Java collections should you use:
  1. ArrayList
  2. LinkedList
   
Question 2: Inner classes can be defined within methods.

SQL tests

Example data 1:
==============================
|FirstName      |LastName    |
==============================
|'Peter'        |'Hansen'    |
|'Peter'        |'Jackson'   |
|'John'         |'Nilsen'    |
|'David'        |'Jackson'   |
==============================


Question 1: Lets have Example data 1;
How much rows will return this query:

SELECT * FROM Persons A, Persons B WHERE
(A.FirstName = B.FirstName Or  A.LastName = B.LastName) AND NOT
(A.FirstName = B.FirstName AND A.LastName = B.LastName)

Thursday, December 13, 2012

SQL GROUP BY and HAVING in examples

Here we show you the usage HAVING and GROUP BY. As an advance, we have an example with GROUP BY multiple columns.
In first section you have got the data schema. Later you have some tasks on it. After that you have the solutions for the tasks.

Singleton design pattern in examples

The singleton is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system.

There are different ways of implementation.

Lazy Singleton aka Singleton with double-checked locking