Tuesday, 5 November 2013

Oops concepts in java


Java is a pure object oriented programming language. It is based on the concept of object. Java implements all the features of object oriented programming like

  1. Abstraction
  2. Encapsulation
  3. Polymorphism
  4. Inheritance
  5. Class
  6. Objects
  7. Methods

Object oriented programming provide a great flexibility, reusability, modularity and security to an application.

Data hiding is a very powerful feature. It provides security to various sensitive methods and variables.

Abstraction provides a way to hide the less essential properties so as to reduce complexity and increase efficiency.

Encapsulation is an information hiding and securing mechanism. It helps in restricting the access of data from the outside entities it means only the methods in a class can access its member variables. Encapsulation reduces the system complexity and increases robustness.

Polymorphism is a way to have more than one form of a method. In polymorphism, we use the method overloading and method overriding techniques.

Inheritance is used to reduce the code and reuse the already available code. Here we extend a base class to a derived class. The derived class then inherits the properties of its base class.

Class is a very essential entity in java. Every program in java has a class. Class is like a blueprint of an object. A class contains member variables and methods.

Object is soul of object oriented programming. An object is an instance of its class. Object is used to access the methods and variables in a class.

Methods are the operations that an object can perform. Methods define the behavior of an object.

All of the above object oriented programming concepts are widely used when we write java codes. OOPS concepts are one of the reason for the power and success of java programming language.

Features of java


Java is now one of the most popular programming language. Its success can be traced by its programming features and designs. The “write one run anywhere” is one of the most important feature.
Some of the features of java are as follows,

  1. Object Oriented
  2. Platform Independent
  3. Distributed
  4. Robust
  5. Secure
  6. Multithreaded
  7. High Performance

Object Oriented

Java is a purely object oriented language. Every variable, constant or function is a part of an object. An object or a class is always needed to access its member variables or member functions.

Platform Independent

It is one of the main feature of java. Java is platform independent at source code level i.e. at bytecode level. Java bytecode can be moved from one platform to another platform without any difficulty. Java is a strongly typed language, we have to declare type for every variable. The data types in java are consistent across all platforms. The java interpreter read the bytecode and converts it into the native language of the machine.

Distributed

Using java we can create applications than can run on different platform, GUI and operating system. Java is designed to make distributed computing easy and efficient. Java also provides various libraries for creating network based applications.

Robust

Java is a designed to create highly reliable and robust applications. It checks errors in code during compilation as well as at the time of interpretation. This eliminates various programming errors. Java does not have pointers and pointer arithmetic. In java programmer does not need to bother about memory deallocation as this task is done by garbage collection very effectively.

Secure

Java implements a good security mechanism to protect the system from viruses in code. It assumes that “no code is safe” for execution.

Multithreaded

Multithreading is now a very essential for software applications. Applications have to carry various tasks at a given time. Java provides a very easy mechanism to develop multithreaded applications that are capable to perform various tasks simultaneously.

High performance

Performance plays a very important role in software. It is always believed that execution of bytecode is not as fast as a complied language. But as the processors are advancing this become a myth. Java bytecode executes nearly as good as C or C++

History of Java


Java is now a well know programming language in the world of computers. Its applications range from mobile devices to large complicated machines.

Java was developed by James Gosling & Sun Microsystems (now owned by Oracle Corporation). James Gosling, Mike Sheridan and Patrick Naughton started the java project in June 1991 for interactive television. It was initially called as “Oak”, named after a Oak tree that stood outside Goslings office. Java is developed from taking many of the features of C and C++ language.

Java is very famous for its quotation i.e. “Write Once Run Anywhere (WORA)”. This nature of java programs makes it very popular. With the release of Java 2, it has been divided into 3 main sub categories,

  1. J2SE (Java 2 Standard Edition)
  2. J2EE (Java 2 Enterprise Edition)
  3. J2ME (Java 2 Micro Edition)

J2SE or Java Se
It is a java platform used to deploy portable applications for general use.

J2EE or Java EE
It is a java platform that is used to deploy sever programming. It is different from Java SE as it is used to deploy large java softwares running on an application server.

J2ME or Java ME
It is a java platform that is designed for embedded systems like mobile phones.

There are 5 main features of java language,

  1. It is simple, object oriented and familiar
  2. It is robust and secure
  3. It is architecture neutral and portable
  4. It is executed with high performance
  5. It is interpreted, threaded and dynamic.

These 5 features of java make java as one of the most loved programming language by programmers.

Java scanner


Java Scanner is one of the easiest and fastest way of getting input from user through console. The Scanner class is present in the java.util package. A Scanner object can take input from console or a file. Even if the program has a good GUI to accept user data scanner always prove useful to read data files. A Scanner breaks the input in to token using the delimiter, the default delimiter is white spaces.
A scanner class has 8 different constructors. They are as follows,

  1. Scanner(File source) 
  2. Scanner(File source, String charsetName) 
  3. Scanner(InputStream source)
  4. Scanner(InputStream source, String charsetName) 
  5. Scanner(Readable source) 
  6. Scanner(ReadableByteChannel source) 
  7. Scanner(ReadableByteChannel source, String charsetName) 
  8. Scanner(String source) 

Scanner can be created using any of the above constructor according to need.
Example:
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();

The Above syntax accepts the input from user through console. Here “nextInt()” method is used to accept the input of integer type. There are methods like “nextDouble()”, “nextFloat()” etc to accept data in their respective format.

Scanner object can also accept data from a file.
Example:
Scanner data1 = new Scanner(new FileStream("filename.txt"));
 while (in.hasNextInt())
{
total +=in.nextInt();
}

The above example accepts all the integer values from the file and add the in the variable “total”. Here the filename is passed to the FileStream constructor to read the file. The “hasNextInt()” method is used to read all the integer values from the file.

The “close()” method is used to close the scanner.
Exmple :
sc.close();

Hence, Java Scanner is one of the convenient way of accepting user data through console or file.

Java string


A String is a sequence of characters, numbers, alphabets and symbols. The String class in java.util package. The String class in java provides number of methods for string manipulation.

Java supports direct string concatenation with “+” symbol, without use of any other method. A String is always read-only and once it is created it cannot be modified.

Creating a string is very simple. Just enclose the string in double quotes and assign it to a literal using assignment operator. String creation through assignment operator does requires new keyword.
Example:
String str=”New String”;
String str1=”Hello Java”;

The String class have 13 constructors overloaded and having different parameters.
  1. String() 
  2. String(byte[] bytes) 
  3. String(byte[] ascii, int hibyte)
  4. String(byte[] bytes, int offset, int length) 
  5. String(byte[] ascii, int hibyte, int offset, int count) 
  6. String(byte[] bytes, int offset, int length, String charsetName) 
  7. String(byte[] bytes, String charsetName) 
  8. String(char[] value)
  9. String(char[] value, int offset, int count)
  10. String(int[] codePoints, int offset, int count) 
  11. String(String original) 
  12. String(StringBuffer buffer)
  13. String(StringBuilder builder)

You can create sting string using any of the above constructor.
Example :
 String str1 = new String();
 String str2=new String(”Hello Java”);

 String str3=new String(str2);

char ch[]={‘H’,’E’,’L’,’L’,’O’};
 String str4=new String(ch);

String is one of the most commonly used class in java.

Tuesday, 29 October 2013

Java applet


A Java Applet is a java program that can be embedded in a html. An Applet is a “.class” that is precompiled. A applet is embedded in a html file using “applet” tag. Applets are the one of the main reason behind the success of java programming language.

A JVM must be installed on the client machine to run the applet. Also a java plug-in is required to be installed in the web browser. Now a day’s most browser support applets. Applets are designed to run on a client machine remotely. A applet can also run offline once downloaded.

Applets can run on any platform, it means they are platform independent. As applets are run remotely they have lot of restrictions. A applet is run in a sandbox environment in a browser. It means the applet cannot access the clients file system. Applet can have full access to the client machine if the user allows.

Applet can be run on all versions of java plug-in. Applets are used to make websites more dynamic and attractive. Applets support most of java controls like buttons, checkbox, text fields etc. Applets are widely used for creating interactive learning environment. Lot of games are also build using applets that also supports multiplayer gaming option.

Applets also support 3D graphics. It uses the system’s 3D hardware acceleration. It enables to develop applets with heavy graphics.

The main disadvantage of a java applet is it need the java plug-in installed in the browser. The mobile browsers are not capable to run applets. A applet code can also raise main security issues if its downloaded from a un-trusted source. A similar interface can be created in HTML very easily with less security issues and more compatibility.

Overall applet is one of the useful feature provided by java programming language to create dynamic and interactive websites.

Java virtual machine


Java Virtual Machine also known as JVM is a virtual machine that is included in Java Runtime Environment to run java applications. JVM can also be called as the heart of Java platform. If you have JRE installed on your computer or any device it means you have JVM installed in it.

The JVM is acts like a virtual environment to run platform independent java byte codes. Byte codes are the files with “.class” extension that are generated after compilation of java file. Byte codes are the intermediate code between java and machine language. JVM provides a cross platform functionality to java applications. Most programming languages like C complied the programs according to the operating system and processor architecture, it means they cannot run on machines having different processor architecture.

The JVM is a machine within original machine that acts as intermediate between the application and the original machine. The JVM converts the byte code files according to the original machine requirement. Hence java applications are platform independent as the JVM take care of all the platform dependencies.

JVM also runs the “.jar” files that are Java Archive files.

Java API is also bundled with JVM in Java Runtime Environment. The JVM is responsible for “write once run anywhere” (WORA) nature of java programs.

JVM also have lots of other feature like automated exception handling, memory management, garbage collection.

Java runtime environment


Java Runtime Environment (JRE) is included in JDK. It is one of the main part of a JDK to run java applications. It is a minimum requirement to run any java application.

Java Runtime Environment consists of Java Virtual Machine (JVM), various core classes, libraries and various other components to run java programs and applets. JRE plug-in is installed in every browser to run java applets and to deploy Java Web Start.

Java Web Start is used to deploy individual application over the network.
Java Runtime Environment is a sub-set of JDK and it is can also be downloaded separately. JRE is required on all operating systems to run java applications. It is also present on mobile devices which are capable of running java applications.

Java Runtime Environment is the very basic requirement to run java programs. It loads and runs java applications with the use of JVM.

When a java file is complied it generates a byte code file i.e. “.class” file. The byte code file is platform independent but the Java Runtime Environment is platform dependent. It means if you need to run java programs on Linux, you need a JRE for Linux. JRE is present for most of the operating system.

Hence java programs are platform independent but Java Runtime Environment is platform dependent means different platform have different JRE.

3 main types of java applications

There are 3 main types of java applications,
  1. Desktop application
  2. Web based application
  3. Applets

Desktop applications

Desktop applications are standalone programs that are installed on local computer. These programs mostly use local available resources on a computer. Some of the desktop application programs also make use internet for networking.

Web based application
Web based applications are programs that are accessed over a network i.e. internet or intranet. These applications are present on a centralized web server. Web based applications are accessed through web browser. These applications are based on client-server architecture. Web based applications are easy to maintain as they are not installed on different computers so updating is easy.
Applets
Applets are programs that can be embedded in a web page just like a image. They are delivered to a user as java bytecode and are executed by the browser’s JVM. Applets were introduced in the first version of Java. Applets is one of the most popular technology in Java. Applets are embedded in web page using embed tag.
Difference between desktop application, web based application and applet,
Desktop Application
Web Based Application
Applet
Installed on local computer

Stored on web server
Stored on web server and expected on clients web browser’s JVM
Need your laptop or desktop to install
Can be accessed from anywhere on any desktop
Can be accessed from anywhere on any desktop
User have to updated the application
Updated by website owner
Updated by website owner
Platform dependent
Platform independent
Platform independent just require JVM
High Piracy risk
No piracy risk
Very low piracy risk

What is Java?


During the early nineties, Java was developed by a group guided by James Gosling meant for Sun Microsystems. It had been earlier created to work on digital cellular devices, for example mobile phones. But, as soon as Java 1. 0 was released for the open public in 1996, its major aim had moved to implement on the Internet. It delivered much more interactivity with users by providing programmers a method to create animated webpages. Over time it has developed in the form of powerful language designed to work with both on and off the Internet. A decade soon after, it’s even now a remarkably popular language along with more than 6. 5million programmers all over the world.

Java is one of the most powerful programming language used these days. It is largely used for creating enterprise applications. A java program is written one and then it can be executed on any platform and any device having java run-time environment installed on it. A java program is mainly run in JRE.

When Java was first released, it mainly was comprised of 2 components: the actual programming language standards, along with the Java runtime environment specification which usually described the actual things about the JVM. As the Java language improved gradually, Sun Microsystems step by step included new features and also technologies which made Java a lot more than just a programming language. To illustrate, Servlets along with JavaServer Pages were created to offer a good mechanism designed for using Java to develop powerful Webpages. JavaBeans offer Java application component architecture. Enterprise JavaBeans gives a mechanism for the purpose of creating distributed applications. All of these technological innovations offer unique standards. Shortly after the introduction of Java 2, to develop standard runtime environments for Java programmers to focus on, Sun grouped their key Java programming technologies into 3 editions:

  • J2ME: Java 2 Platform, Micro Edition
  • J2SE: Java 2 Platform, Standard Edition
  • J2EE: Java 2 Platform, Enterprise Edition

J2SE

J2SE is the core programming language. It provides environment for developing java applications and also supports JDBC, applets, JavaBeans, RMI (Remote Method Invocation), networking, XML, GUI programming and lots of other technologies.

J2EE

J2EE is the enterprise version of java. It is a collection of java technologies for creating platform for distributed applications. J2EE provides support for creating large business applications with platform independency. It comprise of various enterprise technologies like Enterprise JavaBeans (EJB), Java Servlets, Java Server Pages, Java Mail, Java Messaging Services (JMS), Java Database Connectivity (JDBC), Java Naming and Directory Interface (JNDI), Extensible Markup Language (XML), Java Web Services, Java Transaction API (JTA), Java Transaction Service (JTS), etc.

J2ME

J2ME is Micro edition of java. It is not a scaled down version of java. It is mainly used for creating applications for portable devices like mobile phones.

This is just a short introduction of java. If you have any questions please do comment.