Archive for the ‘Java’ Category

JAVA Five: Architectural Features of Java.

Posted: August 1, 2012 by neel131193 in Java, Uncategorized

We have seen that Java programming language finds its various applications in different working fields in world of computers and technology. In this post I’ll tell you about the basic architectural features of Java in brief.

The Java Virtual Machine (JVM):

JVM is defined as: “An imaginary machine that is implemented by emulating it in software on a real machine.” The code for the JVM is stored in the .class files, each of which contains the code on the public class.

The compiler takes the Java application source code and generates bytecodes. Bytecodes are machine code instructions for the JVM. Every Java interpreter, regardless whether it is JAVA technology development tool or a web browser that can run applets, has an implementation of the JVM. The JVM design allows the creation of implementation for multiple operating environments.

Garbage collection:

Many programming languages allow the dynamic memory allocation. The process varies according to the syntax of the programming language, but always involves the returning of a pointer to the starting address of the memory block. After the allocated memory is no longer required the program or the run-time environment should de-allocate the memory.

In C, C++ and other languages, you are responsible for de-allocating the memory. This can be difficult exercise sometimes because you do not know which memory to de-allocated in advance. Programs that donot de-allocate the memory eventually crash.

However Java programming language removes you from the responsibility of de-allocating the memory. It provides a system level thread that tracks ach memory allocation. During idle cycles in the JVM the garbage collection thread checks for and frees the memory that can be freed.

The Java Runtime Environment:

Java software source files are compiled in the sense that they are converted to a set of byte codes. These byte codes are then saved in ‘.class’ files. At the run time the bytecodes that make up a Java program are loaded, checked and run in an interpreter. The interpreted has two functions: it executes bytecode and makes the necessary calls.

JVM tasks:

The following are the tasks performed by the JVM:

  1. Loading of class: This is done by the class loader.
  2. Verifying the code: It is performed by the bytecode verifier.
  3. Execution of the code: It is performed by the Runtime interpreter.

1. The Class Loader: The class loader loads all the classes that are required for the execution of the program.

2.The Bytecode Verifier:  The JVM puts the code through the bytecode verifier that tests the format of the code fragments and checks the code fragments for illegal code, which is that forges illegal pointers , violates access rights on object or attempts to change object types.

3. The Verifying process: The bytecode verifier passes the code through four processes which verify whether the code adheres to the specifications mentioned by the JVM.

In this post we will see meaning of the three main features of Java as a Object oriented programming language.

  1. Encapsulation: Encapsulation is the mechanism that binds together code and the data it manipulates and keeps both safe from outside interference and misuse. One way to think about encapsulation is as a protective wrapper that prevents the code and the data from being arbitrarily accessed by other code defined outside the wrapper. Access to the code inside the wrapper is tightly controlled by a well defined interface. In Java the interface is called a ‘class’. A class is a logical construct.
  2. Inheritance: Inheritance is the process by which one object acquires the properties of another object. This is important because it supports the concept of hierarchical classification. Without the hierarchies each object would have to define its own properties explicitly. For example, consider an apple and a mango. A mango and an apple are both fruits. We all know that fruits have general properties like   the outer covering, the flesh inside, the seed enclosed within et al. So ‘fruit’ is a class and mango and apple are its objects. So the properties that the fruit class defines are all inherited by mango and apple. If we were not to consider the hierarchy, we would have to define the general properties for apple and the mango individually. To avoid the excessive work the concept of inheritance was introduced. The figure shows an example of inheritance. The sub-classes inherit the properties of the super class while can define their own properties.
  3. Abstraction: It is on essential feature of OOP. We manage the complexity by abstraction. The concept will be more clear when we’ll take up a practical example in future.
  4. Polymorphism: Polymorphism is a feature that allows one interface to be used for a general class of action. For example consider a stack. A stack can contain integer values, another can contain floating value while yet another can contain characters. The algorithm that implements each stack is the same even though the data that it contains is different. The concept of polymorphism is often contained in one phrase “one interface, multiple methods”. This helps in reducing the complexity by allowing same interface to define different class of action.

JAVA:Three

Posted: June 17, 2012 by neel131193 in Java, Uncategorized
Tags: , ,

Editing: Beginners should always use simple text editors for editing like notepad.

Saving and Compiling your code:

  1. After you have finished editing your code, you need to save this text file. The location of this file is important. There are two ways one is saving the file in the bin folder inside the jdk1.3.0 folder  and the other is setting the class path. I wouldn’t suggest the beginners to use that method. So to save the file click on ‘file’(ATL+F) and click on ‘save’(S).There open the bin folder. The name of the file should be same as that of the class that contains the ‘main’ method. In this case ‘mainMethod’. The name should be followed by the extension ‘.java’
  2. Once you have successfully saved the file, you need to compile the file and execute the code. To compile the file open up the command prompt from the Start menu. Now go to the location where you have saved the file using the ‘cd’ (change directory) command.
  3. Now you need to compile the code. To this you need to use the ‘javac’ command. Type this command followed by the name of the file to be compiled, here mainMethod.java.
  4. Assuming the code you are trying to compile has no error, the compiler will successfully generate a ‘.class’ file of the file that it just compiled in the same in the same location as of the source folder. The ‘.class’ file has the byte code of the compiled file. To execute this code use the command ‘java’ followed by the name of the file without the extension.
  5. When you hit enter the program will get executed.
// Program to scan values of two variables from user and to print their addition.
import java.util.Scanner;
class printAdd
{
 int a,b;

 void getData()
 {
 Scanner n=new Scanner(System.in);
 a=n.nextInt();
 b=n.nextInt();
 }
 void printData()
 {
 System.out.println("The addition of the two numbers is:"+(a+b));
 }
}
class mainMethod
{
 public static void main(String args[])
 {
 printAdd ob1=new printAdd();
 ob1.getData();
 ob1.printData();
 }
}

This is how a simple Java code looks like. For people who have some programming experience this will not be entirely weird. Lets see what the terms in the above code snippet mean:

The first line is the comment line. The ‘//’ at the beginning indicate the start of a single line comment that is not compiled. Analogous to the #include statement in C and C++ , to import packages the keyword ‘import’ is used. What packages are and how to import them will be discussed later. For now all we need to know that “Scanner” is something important we need  during the execution of our code and hence we have ‘Imported’ it from somewhere.

In the next line we have declared a class named printAdd. Remember that JAVA is a case sensitive language. Also unlike C it supports upper case letters in identifiers. Inside this class we declared two integer variables a and b.

Void getData() and void printData() are two ‘methods’.Methods are similar to ‘functions’ in C. The void keyword denotes that these methods don’t return any value to the place where they are called from.

Now outside the class printAdd we declare another class mainMethod(). This is our main class which includes our main method. Inside mainMethod() is the beginning of the main method. We have included the keywords ‘public’, ‘static’ ,’void’ in front of  main. ‘Public’ is an access specifier. ‘static’ is the keyword that helps us to use the main class without having to declare any ‘object’ of that class. Strings args[] indicates that a string of characters args[] is passes as parameter to the main method.

Inside main an ‘object’ of theclass printAdd declared. This is done using ‘new’ keyword. Once an object of some class is declared the methods in that class can be accessed from the main method. On the next line we have called the getData() method.

Inside the getData() method we can see that an object n of the Scanner class is declared. Use now why we imported the Scanner class in our very first line of the code? ‘System.in’ indicated that the input is to be taken from the keyboard, Then the value is accepted by calling the nextInt() method from the Scanner class. This is done by placing a ‘period’ symbol(dot) after n just as the members of a structure are accessed in C. These values are stored in a and b.

Then again in the main method we call the printData method of the object ob1. In this the addition of the number gets printed. Meaning of ‘System.out.println’ will be explained later.

So this is an example of a simple Java code and overview of main features of it.

JAVA :One.

Posted: June 16, 2012 by neel131193 in Java, Uncategorized
Tags:

Java is a programming language that evolved from C and C++. I won’t be telling you about the genesis of Java language because for two reasons: one google Java and you’ll get atleast ten search pages worth information and two nobody cares! I this post I’ll tell you some things you need to know about Java.

The only illogical thing about Java as a programming language is its name. It comes from the famous Java coffee that the creators of Java consumed .The important features of JAVA are:

  1. Simple: Java was designed to be easy for the professional programmers  to use effectively. If you have some programming experience it’ll not be difficult for you to master .
  2. Secure
  3. Object-Oriented: This is one of the best features of Java. Java evolved from C++ which is an object oriented language.
  4. Robust
  5. Multi-Threaded
  6. Architecture-neutral
  7.  Interpreted
  8. High performance
  9. Distributed
  10. Dynamic

Java has three main types:

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

J2SE  is mostly used to create JAVA applications. J2EE is used to create web applications while J2ME is used for embedded systems.

The meaning of all the terms will become clearer as we proceed. Any Object oriented language has three main features : 1.Encapsulation and Data Abstraction, 2: Polymorphism, 3: Inheritance. C++ doesn’t have the concept of encapsulation.

  1. Encapsulation:Encapsulation is the process of keeping state and behavior of the object in a single unit and exposing public methods to act upon the state means hiding internal data of the object from the outside world.
  2. Abstraction: Abstraction is a way to remove the association of the behavior of an object with the actual details behind the scenes which implement that object’s behavior.
  3. Polymorphism: Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
  4. Inheritance: Inheritance can be defined as the process where one object acquires the properties of another.

These concepts will be better understood when we will actually use them in our codes.

WTH is OOPs ?

Posted: March 28, 2012 by Shashwat Pradhan in Java
Tags: , , , , , ,

So everyone must have heard about OOPs . It’s definitely no rocket science!!

Lets start from basics.   We will discuss a basic program implementing some concepts of OOPs . You can check the previous java articles to learn about the basic syntax: https://decodingc.wordpress.com/java-tuts/.

So what is OOPS ?

Stands for “Object-Oriented Programming.” OOP (not Oops!) refers to a programming methodology based on objects, instead of just functions and procedures.
Object oriented programming is a programming paradigm which uses objects and its interactions to design applications and computer programs.(As objects are real world entities)
Programming techniques may include features such as data abstractionencapsulationmessagingmodularity,polymorphism, and inheritance.

Let’s demonstrate with a Java program:

class rectangle{
  int rectarea(int length,int breadth){
 int area = length * breadth;
  return(area);
}
}
public class ObjectClass{
public static void main(String args[]){
  int rectarea;
  rectangle rect = new rectangle();
 int x = 10, y = 20;
  System.out.println(“Length of Rectangle side= ” + x);
    System.out.println(“Breadth of Rectangle side = ” + y);
rectarea = rect.rectarea(x,y);
System.out.println(“Area of the Rectangle = ” + rectarea);
}
}

Output of the program(in cmd):

C:\java>javac Oopsclass.javaC:\java&gt java Oopsclass

Length of Rectangle side = 10

Breadth of second Rectangle = 20

Area of the Rectangle = 200

You can check out the basic theoretical concepts on the above wikipedia links .We will post more java and android articles ASAP. We will discuss on each OOP feature in detail  my next article.

Looking forward to your comments on our Facebook page!