You are on page 1of 4

Data structures and Java class library

1.Programming Style:

 Comments are introduced by special characters which tell the compiler that the following
words should not be interpreted as code.
 Bracketed comments are used to leave longer explanations directly in the source code.
 Comments are used to document and describe the meaning and purpose of lines of code
directly on the spot.
 The three comment types in Java:
1 - Single line comments.
2 - Bracketed or Multi line comments.
3 - Javadoc comments.
 The short description of a method consists of the first sentence of the Javadoc comment.
Method parameters are described by the Javadoc tag @param; the return value by
@return.

 Java annotations are metadata available to the compiler and at the runtime of the program.
They can be used to make certain statements about the code which can then be checked
automatically by the compiler.
 Annotations always start with an @ sign and the first letter is always a Capital letter.
 The two most common annotations are @Override and @Deprecated.
 Using @Override, one can mark methods that override the method of a superclass.
 With @Deprecated, one can mark classes, attributes, and methods that are obsolete and
should not be used in new code.

 Code conventions typically affect the structuring of code, the naming of variables, or the
formatting of source code. In particular, code conventions establish basic rules intended to
simplify the reading and understanding of source code.
 Code conventions are agreements on how code is structured, named, and formatted. They
were developed since many people often work together on a system and therefore need to
understand one another's code.
 The term “camel case” refers to a special way of writing compound words. Each
additional word is appended to the existing words with a capital initial letter. The capital
letters appearing several times in the word give it the shape of a camel with several
humps.
 These conventions are intended to help one read source code more efficiently. Code that
can be read quickly can also be better understood, maintained and adapted.

2.Working with Objects :


 When complex data types are compared with the == operator, the references are
compared.
 Primitive data types are stored in main memory as numbers, floating-point numbers,
letters, or truth values. Complex data types are stored as a reference that refers to an
object.
 Primitive data types are compared with each other without any problems when using the
== operator. Reference types, on the other hand, are only compared with each other using
the reference. The system therefore checks whether they are the same objects. Identical
objects cannot be compared in this way.

 In Java, the equals() method of the class Object can be overridden to compare the contents
of objects.
 The usual procedure for a comparing content with the equals() method :
1. Check that it is the same reference.
2. Check that the objects are of the same type.
3. If no, convert type and compare.
4. If yes, call equals() method of the superclass.

 The comparison with equals() is too slow for large classes with many attributes relevant
for the comparison. In order to achieve similar fast processing as when comparing
references, the comparison is performed using a hash code that depends on the content
which can be generated with the method hashCode(). Apart from this, the hashCode()
method is required by data structures that use hashing.
 Hashing is the generation of a key for a given object that is as unique as possible.
 The method must be stable and match the equals() method. In addition, the generated hash
codes must be as unique or widely spread as possible and only attributes that have to do
with the identity of the object should be included in the calculation.
 Complex data types (e.g., objects) usually have more than one attribute. This means that
the runtime environment cannot easily know which attributes should be used to compare
the objects.
 Java offer to compare complex data types with regard to their order by the
implementation of the interface Comparable.

 If primitive data types are used as parameters when a method is called in Java, a copy of
the value is passed. This is termed a call by value and means that changes to the passed
values only have local effects within the method.

 If, on the other hand, an object is expected, the reference to the object is passed and is not
a copy of the object. This is termed a call by reference and means that the calling program
and the called method work with the same object.
 Objects can be cloned in two ways: as a “copy constructor” or by overwriting the clone()
method of the Object class.
 When cloning objects, it is important to note the handling of reference data types and
whether only references are copied (shallow copy) or a copy is created of all referenced
objects (deep copy). A deep copy can be created, for example, by creating and calling a
copy constructor. Note that the implementation of clone() in the class Object only creates
a shallow copy. This means that a deep copy must be explicitly programmed in the
overwritten method if desired.
3.External Packages and Libraries :
 To use externally developed libraries in your project, The libraries must first be purchased
or downloaded. They must then be added to the Java classpath so that they can be found
by the compiler.
 Keyword for importing is import.
 Whole packages can be imported by using the “*” character

 The Java class library is one of the most important libraries in Java. It supports the
programmer with useful functions for the most different problems.
 The Java class library is already integrated in the Java runtime environment and therefore,
in contrast to externally developed libraries, does not have to be manually added to the
classpath.
 For externally developed libraries only the externally offered behavior is known (public
method signatures of the classes). The internal structure, and how the functionality is
implemented, is unknown. Since the method names are not necessarily self-explanatory,
the programmer needs information about the purpose and use of the methods. This is
ensured by API documentation and, if necessary, additional tutorials.
 API documentation provides access to the source code of the externally developed library.

4.Data Structures:
 Arrays are a rudimentary way to store groups of objects with the same type in Java. The
elements of an array are sequentially written, one after the other, to main memory.
 When declaring an array, a data type must be specified followed by an open and a closed
square bracket followed by the variable name, In the square brackets, the desired capacity
must be specified.
 Once an array is instantiated, its capacity cannot be changed.
 It is possible to nest arrays in Java. This means that the elements of an array can also be
arrays. This is called a multi-dimensional array.
 Multi-dimensional arrays are declared by using more than one pair of square brackets.
 The collections framework consists of useful data structures enriched with a collection of
search and sorting algorithms, interfaces with different implementations, and other useful
classes.

 Iterating means to run through all elements (e.g., within a program loop) and process them
as required.
 Java offers an iterator and an extended for loop for sequentially processing elements of a
collection.
 They can be processed using an iterator because the interface collection is derived from
Iterable and therefore all collections must offer an iterator.
 The condition that must be met to use the extended for-loop is The data structure must
implement the Iterable interface.

 ArrayList stores its elements internally in an array and access to the single elements is
therefore very fast.
 Because the LinkedList works without an index, when searching for a specific element,
the whole list must be traversed to get a reference to it. Such access is thus slower with
the LinkedList than with the ArrayList.
 Lists can store any amount of data whose elements have a fixed order whose elements can
be of any type and whose elements can be accessed randomly or sequentially.

 The special features of a set is that it represents a set in the mathematical sense. All
implementations of this interface ensure that no element exists twice. More precisely, they
ensure that the following condition never applies to any element pair x and y: x.equals(y)
== true.

 A stack is a data structure in which only the topmost element can be accessed at any one
time or in which new elements can be stored on top.
 Java offers with java.util.Stack and java.util.Deque two possible data structures for this
use case. Because both data structures inherit from other structures such as Collection and
List, they offer considerably more methods than necessary.
 The queue data structure corresponds to a queue. Elements should be processed in the
order in which they are received (first-in/first-out). This means that the first element
inserted in a queue is also the first element removed from the queue.

5.Strings and Calendar:


 Strings can be linked with other strings and primitive data types with the plus operator.
 There are no constructors available for generating strings from truth values or numbers.
 Strings are real objects in Java with their own methods.
 The literal notation of strings allows one to specify strings directly in the source code
without calling a constructor. String objects can be specified in Java with double
quotation marks.
 Transforming primitive values into strings can be done by using the String class method
valueOf. One can convert a string back into a primitive type by using the static method
parse of the corresponding wrapper class (e.g., Integer, Double, Float).
 If “immutable”, it means that the content of a string is not changed after its creation. All
editing methods (such as substring or trim) always return a new string.
 When chaining many strings, many temporary string objects are generated. This happens
because string objects are unmutable.
 With the split method, a string can be split along a separator or a separator string. Split
returns a string array with the resulting parts.

You might also like