Stream Stream is a sequence of bytes that travel from the source to destination over a communication path. For example, source might be network, destination might be a file on the file system. We may want to read bytes over the network and then write them into a file. Communication path can also vary from one operation to another. As shown in the figure, using a input stream a Java program is trying to read certain binary data from the source or alternatively a Java program using a output stream may want to write something to the destination. A program can read from a stream or write to a stream. Streams hide the details of communication from the user. Streams are linked to physical devices by Java IO system. Streams are implemented using classes in java.io package. Two types of streams are provided by Java Byte and Character Byte streams perform input /output one byte at a time. Character streams perform input /output one character (2 bytes) at a time. Byte streams are used for input /output of binary data. Character stream are used for input /output of text data. java.io package has many classes to deal with both these types of streams. File utility classes are also present in the java.io package. Predefined Streams java.lang package defines a class called System It contains three predefined public static variables - in, out, err where err stands for error. They are accessible from any part of the program. System.out refers to standard output stream (console). System.in refers to standard input stream (keyboard). System.err refers to standard error stream (console). These streams are already open & ready to supply/accept input/output data. Five different groups of classes are defined for streams in java.io package : For building different types of byte & character streams. For filtering and piping. For reading/writing primitive values and strings. For Interacting with files. For object serialization. Reader provides a character stream analogous to byte streams InputStream provide. Writer provides a character stream analogous to byte streams OutputStream provide.
Methods of Reader are similar to methods of InputStream. Methods of Writer are similar to methods of OutputStream. InputStreamReader & OutputStreamWriter are converter classes. They translate between character and byte streams. They use either a specified character set encoding or the default encoding for the local system. Streams that have input/output pairs and byte stream & character stream variants those are : Filter streams Buffered streams In-memory streams are ByteArray streams CharArray streams String streams Serialization Disk is a persistent storage medium because whatever we store on the disk today is available to us to the next day. Serialization means storing objects persistently. Reconstructing the objects back is a deserialization process. We need to store the objects in such a form that restoration of objects back into memory should be feasible. Serialization and deserialization is used in inter-process communication. When we want to store objects itself into a database then serialization is used.
Collection There are different types of collections that can exist : ordered collection, indexed collection and mapping collection. The ordered collection is one in which input / output is order based. Means we cannot insert an element in the middle of a stack or a queue. Stack is a Last-In-First- Out collection whereas Queue is a First-In-First-Out collection. The collection can be indexed based. Example of this is an ArrayList and a Vector collection. Means based on the index value, we decide to insert an element. If we want to retrieve a value from an ArrayList at position 5 or we may want to set a value at position 2, then this type of operation is indexed based rather than order based. Third type of collection is a mapped collection where input / output is based on a key. Example of this is a HashTable and TreeMap. This always contains a key / value pairs. For this particular key, this is a value. We insert using a key and retrieve also using a key. ArrayList Java ArrayList class can contain duplicate elements. Java ArrayList class maintains insertion order. Java ArrayList class is non synchronized. Java ArrayList allows random access because array works at the index basis. In Java ArrayList class, manipulation is slow because a lot of shifting needs to be occurred if any element is removed from the array list. TreeSet Contains unique elements only like HashSet. Access and retrieval times are quiet fast.
Maintains ascending order. Innner Classes Java inner class or nested class is a class which is declared inside the class or interface. We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable. 1) Nested classes represent a special type of relationship that is it can access all the members (data members and methods) of outer class including private. 2) Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only. 3) Code Optimization: It requires less code to write Generics The Java Generics programming is introduced in J2SE 5 to deal with type-safe objects. Before generics, we can store any type of objects in collection i.e. non-generic. Now generics, forces the java programmer to store specific type of objects. Advantages 1) Type-safety : We can hold only a single type of objects in generics. It doesn t allow to store other objects. 2) Type casting is not required: There is no need to typecast the object. Before Generics, we need to type cast.
After Generics, we don't need to typecast the object. 3) Compile-Time Checking: It is checked at compile time so problem will not occur at runtime. The good programming strategy says it is far better to handle the problem at compile time than runtime. Disadvantages Only object types can be added to the collection. No primitive types can be added to the collection What an App needs to achieve network communication? To communicate over a network, we need to know three things : 1) name of the machine 2) port number : through which we want to carry out the communication 3) protocol : underlying set of rules, agreed upon Machine name internally gets mapped into Internet Protocol Addresses i.e. IPAddress. IPAddresses are 32-bit addresses. In each 32-bit address, the 4-bytes are separated using.( dot ) and the value present in each byte is taken as a number. 198.168.100.2 is an IPAddress. Port number is able to distinguish between different applications running on a machine. To distinguish one from the other, we need port number. Once we start using port number, one application will use one port number; another application will use another port number on the same machine.port numbers do not have relations with serial and parallel ports. These are only logical numbers and not physical numbers. Protocol would indicate how to send the request, what are the rules governing that and how to receive the response.there are protocols in a network communication about how to send a request from our application to the application running on other side of the network. When that application sends a response, how to accept that response.these set of rules governing the network communication are known as protocol. Examples : Hyper Text Transfer Protocol, Simple Mail Transfer Protocol, File Transfer Protocol.
Network Programming Model The Network programming model always involves the two machines : 1) client 2) server Communication will always happen between client and server. Client will send the request and server will send the response to that request. For example, when we look at email clients like Outlook,they always send mails and receive mails. While doing so they are communicating with server software known as Microsoft Exchange server. They would be using Post Office Protocol for downloading mails and Simple Mail Transfer Protocol for sending mails. When we try to browse certain sites, we might use a browser like Firefox,which is a client side software. On the server side there would be a Web Server which might be,internet Information Server from Microsoft or Apache Web server. A client-server model inolves, sending a HTTP request and receiving a HTTP response.networking programming model is basically a client-server model. What request is send,accordingly we get a response from server side. MD5 MD5 stands for Message-Digest algorithm 5. It was designed by Ronald Rivest in 1991. It converts variable length content to a 128-bit fixed length value. MD5 algorithm function is a one way hash function. It is used in a wide variety of security applications: Check integrity of files Store passwords MD5 hash is expressed as a 32-character hex number. A small change in the content gives a different 32-char hex number. Marshalling Marshalling is the process of gathering data and transforming it into a standard format before it is transmitted over a network so that, data can travel through the network to reach the predefined destination. In order for an object to be moved around a network, it must be converted into a data stream,that corresponds with the packet structure of the network transfer protocol. This conversion is known as data marshalling. When the data is transmitted, the receiving computer converts the marshaled data back into an object. The process is known as unmarshalling. JAXB stands for Java Architecture for XML Binding. JAXB provides API (Application Programming Interface) for converting Object to XML and XML to object quickly.
The XML and Java language are mostly used technology partners to exchange information over the internet. As we know now that, JAXB is used to convert XML to object and vice versa, below are the terms described for the above phenomena: 1. Converting XML to Java Object: JAXB Unmarshalling. 2. Converting a Java object XML: JAXB Marshalling. Thread and MultiThreading A thread, in the context of Java, is a unit of execution. All Java programs have at least one thread, known as the main thread, which is created by the JVM at the program s start. In Java, creating a thread is accomplished by implementing an interface or extending a class. Every Java thread is created and controlled by the java.lang.thread class. When a thread is created, it is assigned a priority. The thread with higher priority is executed first, followed by lower-priority threads. The JVM stops executing threads under either of the following conditions: If the exit method has been invoked. All the daemon threads of the program, are terminated. A multi-threaded application, allows multiple thread execution at a particular time. In a single-threaded application, only one thread is executed at a time because the application or program can handle only one task at a time. For example, a single-threaded application may allow for the typing of words. However, this single thread requires an additional single thread allowing for the recording of keystrokes in order to type the words. Thus, a single-threaded application records the keystrokes, allowing the next single-threaded application (the typing of words) to follow. However, a multi-threaded application allows for the handling of both tasks (recording and typing the keystrokes) within one application.
Commonly used methods of thread class: public void run(): is used to perform action for a thread. public void start(): starts the execution of the thread. JVM calls the run() method on the thread. Synchronization Synchronization in java is the capability to control the access of multiple threads to any shared resource. Java Synchronization is better option where we want to allow only one thread to access the shared resource. The synchronization is mainly used to 1. To prevent thread interference. 2. To prevent consistency problem. There are two types of synchronization 1. Process Synchronization 2. Thread Synchronization There are two types of thread synchronization mutual exclusive and inter-thread communication. 1. Mutual Exclusive 2. Inter-thread communication in java Mutual Exclusive helps keep threads from interfering with one another while sharing data. Inter-thread communication or Co-operation is all about allowing synchronized threads to communicate with each other. Cooperation (Inter-thread communication) is a mechanism in which a thread is paused running in its critical section and another thread is allowed to enter (or lock) in the same critical section to be executed. Zip and UnZip ZIP format is a data compression and archival format. A ZIP file contains one or more compressed files. The format was originally designed by Phil Katz for PKZIP compression and decompression utility. ZIP files generally use the file extensions ".zip" or ".ZIP". ZIP format compresses every file separately. This allows individual files to be retrieved. ZIP is an archive file format that supports lossless data compression.
A.ZIP file may contain one or more files or directories that may have been compressed. The.ZIP file format permits a number of compression algorithms, though DEFLATE is the most common. Microsoft has included built-in.zip support Apple has included built-in.zip support WSDL WSDL stands for Web Services Description Language. It is the standard format for describing a web service. WSDL was developed jointly by Microsoft and IBM. WSDL is XML-based. Protocol for information exchange in distributed enviornment WSDL definitions describe how to access a web service WSDL provides information about the operations the web service will perform Universal Description, Discovery, and Integration,(UDDI), an XML-based worldwide business registry makes use of WSDL Web Services Web Services are services that are offered on the Internet which anybody can consume. It does not matter whether web service was written in Java, C# or VB.Net. If it is written in Java then we can consume it as Java program. Once we create a web service, we can consume it from multiple clients. Web service is available all the time. Web service can be used for HTTP / HTTPS for data transfer. XML format has a universal appeal for representing any idea. So WSDL is used. Through XML, we cannot make calls to functions like getrate( ), getratebycountry( ), etc. For that we need SOAP envelope. SOAP is, Simple Object Access Protocol. Using this protocol, we can make calls to functions. When this protocol indicates that such method is called, again XML is used. We have two sides to a web service; one is service requestor or a consumer and another is a service provider. From the consumer a SOAP envelope has to be send to the Service Provider. Through that SOAP envelope the call would be made to the service, whatever is the returned value,that returned value once again would be put into the SOAP envelope and it will be returned to the client who is trying to consume that web service. All of these are text based. SOAP is a message envelope and its basic job is to map Java types to XML before sending it over the network and vice versa. There are two ways; one is JAX-WS (Java API for XML Web Services) either that can be used or JAX-RPC (Java API for XML - based RPC, Remote Procedure Calls) can use any one of them. XML
XML stands for extensible Markup Language It is a markup language much like HTML XML was designed to store and transport data XML was designed to be self-descriptive XML is a W3C Recommendation Defines a set of rules for encoding documents in a format that is both humanreadable and machine-readable. The design goals of XML emphasize simplicity and usability across the Internet. It is a textual data format with strong support via Unicode for different human languages. The language is widely used for the representation of data structures such as those, used in web services. DTD A DTD is a Document Type Definition. A DTD defines the structure of a Document DTD defines the legal elements and attributes of an document, like XML With a DTD, independent groups of people can agree on a standard DTD for interchanging data. An application can use a DTD to verify that XML data is valid. If the DTD is declared inside the XML file, it must be wrapped inside the <!DOCTYPE> definition The DTD states what tags and attributes are used in order to describe the content of anxml or HTML document, where which tag is allowed, and which tags can appear within other tags is mentioned. For example, in a DTD one could say that LIST tags can contain ITEM tags, but ITEM tags cannot contain LIST tags. This ensures that all the documentation is formatted the same way. Applications will use a document's DTD to properly read and display a document's contents. Changes in the format of the document can be easily made by modifying the DTD. Annotations Annotations are more mature comments. They provide data about a program that is not part of the program itself.
Annotations can be applied to classes, fields, methods and packages. Three uses of annotations: We can think of using annotations during compilation. Usage of annotations during compilation is to detect errors. It will also allow to suppress the warnings. We can use annotations during deployment process. Annotations can be used during deployment to generate code and to create XML files. We can use annotations during execution process. We can use annotations during execution to test the program. Java Annotation is a tag that represents the metadata attached with class, interface, methods or fields to indicate some additional information, which can be used by java compiler and JVM. Annotations in java are used to provide additional information, so it is an alternative option for XML and java marker interfaces. Built-in Java Annotations : @Override @SuppressWarnings @Deprecated @Override annotation : assures that the subclass method is overriding the parent class method. @SuppressWarnings annotation: is used to suppress warnings issued by the compiler. @Deprecated annoation : marks that this method is deprecated so compiler prints warning. It informs user that it may be removed in the future versions. So, it is better not to use such methods. Reflection Reflection is a mechanism which allows to get the information or use the information about an object, about a class or about an interface. We can come to know that This particular object has been created from which particular class. If we know the class, we can find the modifiers used by the class. Fields that this class have, Different constructors defined within this class, Different methods declared within the class, The class from which this class has been derived, Parameters we need to pass the methods that we intend to call, etc. If we know the interface, we can figure out different constants used in it, methods present inside the interface. We are able to get all these information using Reflection. And this can be done at runtime in a programmatic fashion.
Java Reflection is a process of examining or modifying the run time behavior of a class at run time. The java.lang.reflect packages provide classes for java reflection. The java.lang.class class provides many methods that can be used to get metadata, examine and change the run time behavior of a class. The Reflection API is mainly used in: IDE (Integrated Development Environment) e.g. Eclipse, MyEclipse, NetBeans etc. Debugger Test Tools etc.