Wednesday, 13 August 2014

Java Program to Calculate the Square Root of a nmuber

As in our previous program we have just printed the message using System.out.println() method now we are going to use more features of Java .We will use some packages(like header files of C++).It would be coming in your mind that what is package. Actually package is a set of java code that is already written by Java developers and we are going to use them in our program(we will see packages in details in upcoming blog post).
import java.lang.Math;           //import package statement
class SquareRoot
{
   public static void main(String arg[])
   {
       double num=5;                //declaration and initialization of variable
       double sqResult;
       sqResult=Math.sqrt(num);     //This statement will calculate the Square-root 
       System.out.println("Square root of num 5 is="+sqResult);
   }
}
In this program the structure is same as in the previous post program.But the new code that is there in the example is as following:-

  • variable declaration statement

double num=5;
this statement is a variable declaration statement that has variable name
num
,this name is used in further code as a reference of value 5.we will see full explanation that shows how to declare variables and what rules are followed in the upcoming post.
double sqResult;
This statement also a variable declaration statement but we are not providing the initial(default) value.

  • Method call of a class

sqResult=Math.sqrt(num); 
In this statement we are saving the result value to the variable sqResult. On the right hand side of equal operator(=) we are calling a method(function) of class 'Math' that accept the variable 'num' as argument and do some calculation for us(the code is already written in package) and return the result back . = equal operator set the result into variable 'sqResult'.

  • Printing the result on the command line
  • System.out.println("Square root of num 5 is="+sqResult); 
    This statement will print the message that is written in the double quotes and the value of the variable 'sqResult'.You can see that the 'println' is also a method(function' that is accepting the message given by us and it will do some work for us to print it on the command prompt.
Execution:-

  • Open notepad and write the code:-




  • Save the code in file with name' SquareRoot.java':-

  • Compile and Run the program in command prompt:-




Monday, 24 March 2014

Implementing your first Java program


Implementation of java program involve the following steps:-

  • Creating the program
  • Compiling the program
  • Run the program
  • Creating the program:-To create the java program your can use text editor(like Notepad in windows).Open your text editor which you are going to use and type the following code in that:-
  • class MN
    {
     public static void main(String arg[])
     {
      System.out.println("Hello and Welcome to java programming");
     }
    }

    Save the file with MN.java (MN should be the name of file as the class name is name is MN)Name as in this screen short
    • Explanation:- 
      • Class Declaration:-The first line
        class MN
        
        This line declares a class .It is the first step to object-oriented programming.We know that Java is Object Oriented Programming ,so everything is is inside the class (class Name is MN here ).The keyword class is used to declare a class.

      • Opening Brace:-the braces { } are used to group the class definition.
        class MN
        {
        
        }
        
      • Main line-The line
        public static void main(String args[])
        
        In this line main is a method ,like we have in C and C++ main().In every Java application there must be main()method .Because it is the starting point of program execution by the interpreter.A Java program have only one main() method.With main() method there are other keywords are used explanation of them are as following:-
        public The keyword public is a access specifier that declares the main method accessible to outer classes this is because the Java program execution is started by the Operating System so main method is accessible to the Operating system.
        static The keyword static declares the main() method as one that belog to the entire class not a part of the object of the class,main method is declared as static so that Java interpreter can use the main method before any object is created of that (here MN) class in which the main is written.
        void The type modifier void states that the main() method will not return any value to the Operating system
        String args[] This is a parameter that is passed to the main() method ,at this movement just remember that it will hold the values that we will pass to the program when we will run the program
      • Output Line-The line
        System.out.println("Hello and Welcome to java programming");
        
        This is Similer to the printf() statement in C and C<< in C++.Similarly in Java System.out.println() is available.Because Java is truly Object Oriented Programming Language. So in this statement System is a class ,out is static member of System class and println() is a method of member out.

Saturday, 15 March 2014

Introduction to Java


Java is a general purpose object oriented programming language.We can develop two types of Java programs that are:-

  • Standalone applications
  • Web applets

  • Standalone applications:-Standalone applications are programs written in Java to carry out certain tasks on a standalone local Desktop Computer.The programs can be develop to do task on a local computer (like Calculator you use in your Operating System,Notepad can be developed in Java,Yahoo Facebook messenger etc).These Standalone application can do communication with the Internet also to do needful tasks. A good Example of Java standalone application is ThinkFree that is written in Java.It is just one example there are so many applications that are written in java and the advantage that these applications get is platform independent,means these are written once and can be run on any operating system that may be Windows,MAC,Linux etc.So the programs that we will develop in upcoming posts will be developed only once and without any change you will be able to run them on any operating system with any hardware lying below operating system.Developing a Java application involve following steps:-
    • Write The source-code in a file and store that with .java extension.
    • Compile the source-code file into byte code using javac compiler.(steps to install the JDK that has java compiler)
    • Execute the bytecode program using java Interpreter.

  • Web applets:-Java applets are small Java programs developed for internet applications .You may play online games that are mostly Java applets that run in your browser (downloaded through internet),you may notice some websites that have animations running and you interact with that animations that are all java applet programs that are written in Java.So we will see how to develop them in upcoming post.

Note:-These above types of programs types are developed using just J2SE that is Java 2 Standard Edition.Java has a wide range of other tools that are J2EE that are used to develop Enterprise applications.J2ME for mobile application development.

Tuesday, 11 March 2014

How to install and configure java(JDK) environment to develop programs?


Before going to start installation of java(JDK) ,I will suggest you that please check your system configuration and match that ,would your system is fulfilling the minimum requirements required to run java.I am pasting the link of ORACLE website were the minimum and maximum requirements are  available,I am not writing them here because these requirements change time to time.So here is the link:-


Installation Guide and System requirements on Oracle website:-
http://docs.oracle.com/javase/7/docs/webnotes/install/

Windows Requirements:-
http://docs.oracle.com/javase/7/docs/webnotes/install/windows/windows-system-requirements.html


So if your system fulfill the  hardware and software requirements then you can go to following steps of installation:-


  1. Download the java(JDK) form the Oracle website as per your Operating system,link of Oracle website for JDK is :-http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html    .This link when pasted the JDK7 was latest one development kit.
  2. Here I am using Windows 64 bit OS machine so I am downloading the Windows x64.After downloading the JDK you will get one file with extension .EXE(because I am using Windows OS,you have to download as per your OS),so double click on this setup file:-

Thursday, 6 March 2014

What is JDK?

JDK is a Java Development Kit ,that is a collection of tools that are used for developing and running Java Programs. JDK include the following:-

  • javac(java compiler)
  • appletviewer (used to view the applet programs)
  • javap(java disassembler)
  • javadoc(java HTML document creator)
  • javah(for C header file uses)
  • java(java interpreter)
  • jdb(java debugger)
Brief Description about the tools:-
  • javac:- java compiler is used to compile the java source code to bytecode(.class files are generated).It translate the java source code that is written by the programmer ,to bytecode that become input for the java interpreter. Syntax:-
                          javac <SourceFileName>.java
  • appletviewer:-It is used to run the applet programs and see the output of the applets ,it help to test that ,is the applet is running as per the internal coding or not.you do not need any browser to run the applet you can use the appletviewer tools to run applet.
                appletviewer <SourceFileName>.html
  • javap:-Java disassembler enables the programmer to convert the byte code files into a program description like package, protected, and public fields and methods of the classes
  • javadoc:-It create HTML formatted documentation form the java source code files.The developers use the comments ,doc tags and annotations in the programs which are the converted to HTML document for reference in future .
  • javah:-javah produces C header files and C source files from the java class files ,so that native method implementation can be done.
  • java:-java interpreter tools is used to  generate the machine code from the .class file,the end code generated by the java tool will be understandable by the machine hardware and output will be displayed to the user.
JDK tools used in java application development

Java Features



The main reason to develop a new language called Java ,was to offer solutions to those problems which are not possible to solve using existing languages(like using C ,C++),some of these problems were portability,reliability,distributional,compact etc.So there are some features of Java that make it popular in the market of general purpose stand-alone application development:-

  • Compiled and Interpreted Language:-We can search any language ,and it can be seen that a language it either compiled or interpreted.But Java is the combination of both,that is why Java is platform independent .Java code is first compiled (using compiler)and source code  is translated to byte code instructions .These byte code instructions are not machine independent,so they are interpreted (using interpreter) to machine code, that is directly executed by the machine ,on which java program is running.So java is both compiled and interpreted language.
  • Object Oriented Language:-Java is truly object oriented language ,in this everything is an Object ,everything is written the objects and classes(even main method is written in class that is not possible in C++ like language).Java has a extensive set of classes ,that are arranged in different packages.We can import (like we include in C++)them in our program and can use their functionality.
  • Platform Independent and Portable Language:-One of the reason Java is popular today is its feature of portability ,means Java programs can be moved from one computer to another computer any ware and any time , without any change in source code . Changes and up-gradations in the Operating Systems,in processors,in any other resources will not force any changes in the Java programs.That is why it is the first choice for software development.You may see the Applet programs running on your web browser when you open any website(advertisement or some popup) ,that are written in Java programming language ,and they are written only once ,when any person open the website that Applet programming run on the person's computer,what ever processor,operating systems he\she has.So java is platform independent and portable language.
  • Simple and Familiar Language :-Java code syntax has similarity with other languages like C ,C++.So the C++ developers can easy learn Java programming language and can start developing java programs.But there are some features which of C++ which are not supported by Java ,that are:-
    •  Pointer are not supported by Java.
    • Pre-processor  header files are not supported by Java.
    • goto statement is not supported by Java.
    • Operator overloading is  not supported by Java.multiple inheritance is not supported by java etc 
          So if your are a C++ developer and going to start with Java then you should keep in mind the                      difference in features and other thing which are not common in C++ and Java.

  • Robust and Secure Language:-Java is a robust language .It provide so many protection in code generation and execution to ensure the reliability.It has strict compile and run time checking of data types.Java has internal garbage collection mechanism that relieve the programmer from all memory management problems.Java has implementation to check all the exceptions and errors that may be risky while execution and it show all the errors and exception warning before execution.
               When it comes the point of secure language ,then it is clear cut answer is that Java does not have           pointer so the viruses and threats can not get access to the memory locations where data reside ,so              Java is a secure language.
  • Multitheaded Supported Language:-Java language support multithreaded  programming ,means you can create different threads for different tasks in your Java applications which will give better experience to end your in terms of faster results ,so your application will have better productivity ,if your will use Java mulithredded programming.But keep in mind that you should have complete knowledge about how to use multithreading  in your application.
  • Native method support:-Java support native methods means your can use the C++ written functions in Java programs ,this helps the programmers to use the already written code in the application development.
  • Ease of Development:-Java  has many features like Generics,Auto-boxing,Un-boxing,Annotations, Static import of packages etc ,these features reduce the overhead of the programmers and make the development easy.These also reduce the time of coding and application development can be go in next phase like testing phase .
  • Monitoring and Manageability APIs:-Java has a large no. of APIs one of them in JVM Monitoring and Management APIs  like JMX to monitor and manage Java applications.There are other tools like jconsole, jps,  jstats etc,these tools facilitate the management and monitoring tasks.

Thursday, 27 February 2014

Java History and future releases


Java is general purpose object oriented programming language,which .is developed by Sun Microsystems in 1991.Java was originally called Oak by James Gosling ,one of the inventors of the language,Java was actually designed for the software development of consumers electronic devices like TVs,, VCRs,etc.The goal has a impact on the development project to make the language simple portable and highly reliable.The project team discovered that the languages in the market like C ,C++  had limitations in terms of reliability and portability. So the project team pick up those  features of C and C++ which are good and leave those features which are causing security and other problems.So a new programming language was developed and released in the market.


Here is some Java Milestones :-
Java History
1990 Sun Microsystems decided to develop software that could be used to manipulate consumer electronic devices.A team headed by James Gosling  was formed to undertake this task.
 1991   After exploring the programming languages which were popular in the market like C ,C++ the team announced a new language named as "Oak".
 1992  The team (named as Green Project team by Sun Microsystems) demonstrated the application of there new language to control the home appliances using a hand held device. 
 1993  The world wide web appear on the internet that transformed the text based interaction into a graphical environment.The team come up with the idea of development of small tiny programs(applets) using new language which they have developed("Oak").These new programs can run on any type of computer connected to the internet. 
 1994  The team developed a new web browser called "HotJava" to run applet programs on internet. 
 1995  The language "Oak" was renamed as "Java" due to legal issues
 1996  Java established itself as a leader for Internet programming as well as general purpose  ,object oriented programming language .In this year the Java Development Kit 1.0 was released.
 1997  Sun has released Java Development Kit 1.1(JDK 1.1).
1998 Sun releases Java 2 with version 1.2 (SDK 1.2).
1999 Sun releases Java 2 Platform (J2SE) and (J2EE).
2000 J2SE with SDK 1.3 was released by Sun .
2002 J2SE with SDK 1.4 was released.
2004 J2SE with SDK 5.0 (instead of JDK 1.5 ) was released.
2006 J2SE with SDK 6.0 was released.
2011 J2SE with SDK 7.0 was released.
2014 J2SE with SDK 8.0 will be released.
2016 J2SE with SDK 9.0 will released.


Tuesday, 7 January 2014

Why is Java Strongly associated with the Internet?

Java  is strongly associated with the Internet because the first application program written in Java was HotJava,a browser to run the applet on Internet .So the Internet users use the Java to create the applet programs and run them locally using a 'Java-enabled browsers' like HotJava..The users can use the 'Java-enabled browsers to download the applet located on the computer system anywhere in the internet ans run it on their computer.

Downloading a applet via internet


So the Internet users  start setting up the their web sites  containing Java applets that could be used by the other remote internet users .So this ability make the Java programming language popular among the Internet programmers ,so the Java is popularly known as 'Internet Language'.