2016-03-31



Image source: pixabay.com

Java is one of the most interesting programming languages I have come across since I have started to learn programming. It is fast, versatile and can be used almost for a lot of things. People tend to think that Java is difficult, especially Advanced Java in terms of App Development. But let me tell you, it is not. First of all, there are very few programming languages which are tough to learn; the ones like Malbolge. But Java and Advanced Java? Nope. Its not. Its like they said in the movie ‘Dark Knight Rises’, ‘It needs a less busy mind to do it’. Similar is the case for Java App development. One needs to be fully involved in it to make sure things work out in a proper manner when developing a Java based App. Obviously it is not as easy as Python or Scheme. But the speed and utilization of resources are far better than the likes of it. Java App development is not for beginners or even people who have learnt other programming languages. It is only for those who have a rock solid foundation of the basic Java.

The PreRequisites

So, before you proceed to use Java App development, make sure you know a few things like below:

Generic Programming

Java App Development with Network Programming

Various Java Applets and Web Servlets

Regexes or Regular Expressions

Exception Handling

If you don’t know the above terms and how to work with them, then there is no chance you could develop a stable and a sensible app. One requires intense amount of knowledge in the above terminologies in order to work with them. Atleast if you have some basics in any programming language and in its java app development, its fine. If you don’t know what they are, or how they work, don’t worry. That’s what I am here for. So, lets get started…shall we?

Generic Programming

You may have heard of the high level or the low level programming, and similar stuff. But what exactly is Generic Programming? That is not a term you hear when you work with almost any other programming language. Generic programming mostly became famous with C++ and then Java. Generic programs have lots of techniques. Generic programming is all about centralizing the software components so as to use them and reuse them in a variety of manner. Normally, when you try to generalize a program, people often do it at the cost of efficiency and the app becomes less optimized and slow. And thats not good. However, the templates used in functions and classes like in C++ are actually very effective for generic programming since here, the generalization is possible without the loss of efficiency.

Generic methods and classes in Java enable the app developers to specifically choose a single method declaration along with a query of related methods, class declarations and types as well. They also support the safety of the compilation time in order to catch weird behaviour types during the compilation.

Following is a given example that states how to print arrays of multiple types using a mono Generic method:

public class TestMethod{// The method is pArray which needs to be printedpublic static <X> void pArray( X[] insertArray ){// Displaying all the elements in the arrayfor ( X element : insertArray ){        System.out.printf( “%s “, element );

}

System.out.println();

}

public static void main( String args[] )

{

// To Create Integer, Double and Character arrays

Integer[] iArray = { 1, 2, 3, 4, 5 };

Double[] dArray = { 1.1, 2.2, 3.3, 4.4 };

Character[] chArray = { ‘C’, ‘X’, ‘M’, ‘A’, ‘S’ };

System.out.println( “Array iArray contains:” );

pArray( iArray  ); // Display an Integer array

System.out.println( “\nArray dArray contains:” );

pArray( dArray ); // Display a Double array

System.out.println( “\nArray chArray contains:” );

pArray( chArray ); // Display a Character array

}

}

Java App Development with Network Programming

If you are an experienced programmer, even if not in Java but in some other language, then the first thing that comes to mind when you hear the term Network Programming are Sockets. Sockets play an extremely important role in java app development. If you don’t know what sockets are, let me explain it to you in a simple manner.

A socket is just one side of the two-way connectivity link between two programs running altogether on the same network. Classes used in Sockets are used to represent the connectivity one between the client side and other on the server side. And our Java package provides two classes for the same that is the socket and the server sockets which execute the connectivity from the client side as well as the server side together. Though URL connections provide a decent upper level mechanism for redistributing resources on the Internet, but sometime you need applications that require lower the belt network communication which in our case is using sockets or when you need to develop a client and server App.

For example, say when developing a client and server app, the server is used to process database query and to send out the price of current stock. The client then uses this service which is given by the server to display the required database results to the requesting user and also thereby giving stock recommendations to the specific kind of investor. And when this communication is taking place, it should be without any data drop in the same manner as it was sent to the client side, so that there is no incorrect data transferred.

Thus, the TCP connectivity here helps to provides a trustworthy end to end communication channel that is used on the web by the client and the server application to establish secure communication with each other. And this is where the socket connectivity comes in between. These sockets are binded to every other program to communicate at its end channel. Then, the client as well as the server reads and writes to the required socket as and when required.

Various Java Applets and Web Servlets

Most of you, if you have used Linux then you may already know what an applet is. But still, some of you may be confused as to what Servlets are and how they place an important role in the java App development of Java. So, lets take a look at the major difference between them and what exactly they are:

Java Applets

A Java applet is a predefined piece of code which is written in Java and provided to users. When the user executes the given applet on a Website, it is actually executed within a JV Machine in a totally separate process from the browser itself. Thus whenever you try to run a Java based applet, if you check your task manager, you will see a new process being executed. This is actually good but at the price of malicious applets configured to give a background Telnet access which can be executed without even user knowing that someone else is taking control of his whole computer.  Applets are designed to provide an interactive session to Web based Apps, that which cannot be provided by the basic HTML alone. They can even take control of the whole keyboard and mouse and execute specific interactions by itself. In addition to this, these applets can help in providing a graphic visual UI. This makes the applet well suitable for providing some kind of training online, playing videos and similar stuff.

Thus, applets are actually just apps designed to be send data over the Web and to get executed by the user on java supported browser. Most browsers today like Chrome, Safari, Mozilla and Opera support these applets by default without any need to have extra configuration. These applets can use the UI classes and life cycle methods like AWT, initialization, stopping itself and other similar stuff.

Java Servlets

Servlets play is partially different role in java app development, but they are an essential part here. A servlet is something which is partially similar to Applets since it has derived its name from there. However, to be more specific, it is actually Java based language class used to increase the capacity of any server. Since servlets activate on any kind of requests, they are widely used to increase the capacity of applications to be executed which is hosted on the web servers. Servlet is just a part of Java which helps to get going with other languages such as PHP or DotNet. In simple words, it is just an object that acquires a request and responds to that.

Servlets are executed with the help of something known as ‘the servlet container’ associated with the basic HTTP server. It is a server side component and it does not support a user interface. Servlet Methods are Get and Post.

Regexes or Regular Expressions

Regular expressions or what they are commonly known as Regexes is a method typically used to describe a set of strings which depends on similar characteristics mutually shared by every other string in the set. These can be used to scan, modify or even manipulate data. In order to learn regexes, you must first learn to create specific syntaxes to create other Regexes; and by other regexes I mean the ones that go beyond the normal daily usage syntax of the Java. Basic Regexes differ from each other and are complex in nature, but once you get the basics right and how they are developed or constructed, you will be able to understand and modify any other regexes.

In Java, the regex package is known as java.util.regex. The java.util.regex package includes three different classes binded together to each other:

Pattern

A Pattern object consists of a view of compiled regular expression. It doesn’t have any public constructors. However invoking a public static compile technique will give a Pattern object. Here, the first argument is treated as a regular expression.

Matcher

This object detects how the above pattern behaves and executes a matching search operation against the given string. Similar to Pattern class, there are no public constructors here.

Pattern Syntax Exception

This Exception object is not checked by default and it displays syntax errors within regex patterns.

Exception Handling

Exception is an event that happens when a program is executed, but something disturbs the normal flow of the coded instructions. When this error occurs inside of a method, it creates an object and is transferred to the runtime system. This is inferred as the exception object and it holds detailed info about the type of error, when it happened and whether the program was stopped or continued. Creation of this specific exception object and transferring it to runtime is known as throwing an exception.

After the exception is thrown, the system tries to find a way to a handle it and a few things are run which in an ordered manner to get its details, and this is known as Call Stack. Following is the data structure as to how this is actually handled.



The Call Stack



Searching call stack for Exception

There are in total three types of exception in Java App development. Following are the detailed explanation for the same:

Checked Exception:

The first kind is known as the checked exception. These types of exceptions are predicted exceptions and are known for. Thus a solution to handle these kind of exceptions are already saved in the instructions of the program. The easiest possible example could be something like when you enter an online form, you are required to fill your name, but in case you enter a numeric digit by mistake, then it should raise an exception and give back a prompt stating invalid entry rather then just quitting the whole application. Thus these are known as checked exception since these are expected from the end user and are already known and tested to bypass it.

The Error

These kind of errors are those which occur due to external environment. This is not a fault of the application, but it is some external event that causes the program to quit unexpectedly. A good example for this would be something like your program is running already, and due to some power failure, the system shuts down. Then it should raise an exception showing the user that it is something else apart from the app that has caused the application to end.

Runtime Exception

These are exceptions which are not covered in the above exceptions. For example something like bugs. These exceptions are not anticipated and are raised due to fault of the programmer. It actually makes more sense to totally eliminate the bug rather than to raise this exception. But hey, at least something is better than nothing right?

I could actually go on and on about Java App development, but these are few things that matter to the core. If you know what these are, then you atleast have the basics and are ready for the actual java app development to begin with.

The post Guide to Java App Development appeared first on eduCBA.

Show more