Late GN Sapkal College Of Engineering Nasik. Department Of Electronics & Telecommunications Title: Basic program. Aim: Write some simple programs in Java such as i) To find factorial of number. ii) To display first 50 prime numbers. iii) To find sum and average of N numbers.

Objectives: To learn how to take input from user and display the output on terminal in java. Theory: What is Java?

Java is a programming language & a platform. Java is a high level, robust, secured & object oriented programming language. It is used to develop software which can run on variety of devices. With invention of internet & changing requirement, , C++ could not fulfill the programming requirement. Java was invented around 1995 & becomes one of the most accepted & used language by the . They provide rich set of functions by which any kind of software can be developed very easily & can also develop complicated software. Java program can run on any platform so programmer can develop software without considering the underlying platform. During initial phase java was used for desktop application but slowly it becomes the first choice of programmer for internet programming. Famous mobile platform Android is also based on Java Programming language.

Evolution of Java:-

Java was conceived by James Gosling, Patrick Naughton, Chris Warth, Ed frank, & Mike Sheridan at Sun Microsystems, Inc. in 1991. It took 18 months to develop the first working version. This language was initially called “Oak” but was renamed “Java” in 1995. Between the initial implementation of Oak in the fail of 1992 & the public announcement of Java in the spring of 1995, many more people contributed to the design & evolution of the language. Bill Joy, Arthur van Hoff, Jonathan Payne, Frank Yellin, & tim Lindholm were key contributors to the maturing of the original prototype.

Somewhat surprisingly, the original impetus for Java was not the Internet ! Instead, the primary motivation was the need for a platform-independent (that is, architecture-neutral) language that could be used to create software to be embedded in various consumer electronic devices, such as microwave ovens & remote controls.

Many different types of CPUs are used as controllers. The trouble with C & C++ (& most other languages) is that they are designed to be compiled for a specific target. Although it is possible to compile a C++ program for just about any type of CPU, to do so requires a full of C++ compiler targeted for that CPU. The problem is that compilers are expensive & time-consuming to create. An easier & more cost efficient solution was needed.

Late GN Sapkal College Of Engineering Nasik. Department Of Electronics & Telecommunications In an attempt to find such a solution, Gosling & others began work on a portable, platform- independent language that could be used to produce code that would run on a variety of CPUs under differing environments. This effort ultimately led to the creation of Java. The second force was, the World Wide Web. However, with the emergence of the World Wide Web, Java was propelled to the forefront of computer language design, because the Web, too, demanded portable programs. Over the period of time Java has evolved with many versions. Every version is trying to add new features in it. Complete evolution is given below :-

JDK Alpha & Beta (1995) JDK 1.0 (23rd Jan 1996) JDK 1.1 (19th Feb 1997) J2SE 1.2 (8th Dec 1998) J2SE 1.3 (8th May 2000) J2SE 1.4 (6th Feb 2002) J2SE 5.0 (30th Sep 2004) Java SE 6 (11th Dec 2006) Java SE 7 (28th July 2011) Java SE 8 (18th March 2014) Types of Java:- It is available in different types as per the requirement of the application. It is available in 3 forms.

1. J2SE :- It is nothing but Java 1 Standard Edition. It is known as Core Java. Most basic & standard version of java. It is foundation for rest of the editions. Mostly used to create desktop applications. It contains all fundamental building block of language like variable, data types, control statement, arrays, strings, databases connectivity, etc.

2. J2EE :- It is Java 2 Enterprise Edition. Most of the java applications are designed in J2EE. Useful in creating applications like Web applications, Web Services, Networking, Server side scripting, etc. It is also referred as advance java.

3. J2ME :- It is java 2 Mobile edition. It is used for embedded devices like mobile. Application developer need to consider mobile limitations like limited processing power, battery backup, small display, etc. Many libraries & API are used from J2SE only. Java Environment:-

JVM :- It is Java virtual Machine. Used to execute Java Program. It runs the bytecode. Bytecode can be understood by JVM. JVM is platform dependent. If we change the platform, JVM is going to change. It interprets the bytecode. It is also responsible for garbage collections, array bound checking, etc. It is called Virtual because it gives machine interface which is independent of & computer hardware. It generally performs code loading, verifying code, code execution & providing runtime environment.

Late GN Sapkal College Of Engineering Nasik. Department Of Electronics & Telecommunications JRE :-

It is nothing but Java Runtime Environment. It provides libraries to run the program. It also contains java plug-in. It is implementation of JVM. It contains set of libraries & other files. It is also platform dependent.

JDK :-

It is nothing but Java Development Kit. It contains JRE & development tools. It contains compilers, debuggers, etc. It is also platform dependent.

The Relation between JVM, JRE & JDK is as Figure 1 :-

Figure 1:- Relation between JVM, JRE, and JDK

Use of Java:-

Java is used to develop variety of applications. Useful to program variety of devices. Useful to create desktop applications like Acrobat Reader, Media Player, Antivirus, etc. It can be used to develop web applications like IRCTC. Enterprise applications can be developed using java like core banking applications. It is also used in mobile devices, embedded system, robotics, gaming, etc.

Late GN Sapkal College Of Engineering Nasik. Department Of Electronics & Telecommunications Following types of applications can be developed using java :-

1. Standalone Application. 2. Web Application. 3. Enterprise Application. 4. Mobile Application.

Java Features:-

Figure 2:- Java Features

1.Simple:- Syntax of language is very simple to use as compared to other programming language.

2. Platform Independent:- It is OS & hardware independent. It is possible due to java bytecode. Bytecode is universally accepted code by any JVM implementation. Though JVM is dependent, bytecode is platform independent & because of which java can be considered as platform independent language.

3. Secured:- It is most secured language because it does not provide the pointers. Security is added at different layers. It is added during the programming in terms of methods & private variables. JVM verifies the bytecode means another layer of security is added.

4. Robust:- It means strong. It uses very efficient memory management. It does not provide pointers so avoids security problems. Automatic garbage collection is supported by java.

Late GN Sapkal College Of Engineering Nasik. Department Of Electronics & Telecommunications 5. Protable :- Java application can be installed on any device without any change.

6. Multithreaded :- To utilize the processor power, multithreading is supported by Java. Separate library is provided to multithread the program.

7. Distributed :- Distributed application development is possible using java. RMI & EJB can be used for it. File access through internet is possible.

8. Architecture Neutral :- All programs can run on any platform & on any operating system.

9. Dynamic :- Memory allocation to object of class is dynamic.

10. Interpreted :- Java byte code is translated on the fly to native machine instructions & is not stored anywhere. The development process is more rapid & analytical since the linking is an incremental & light weight process. Tools Required:-

Different tools are required to create java applications. It includes editors, compilers, debuggers, etc.

Main tool required for java applications are as follows :-

1. Editors :- It is used to type the program. Famous editors are notepad, gedit, vi editor, , Netbeans, Jcreator, etc.

2. Compliers :- It is used to check the correctness of program.

3. Debuggers :- It is used for step by step execution of the program. It assists in identifying the logical errors within the program. After every step we can check variable value.

4. Libraries :- Predefined libraries are useful in faster software development. Different languages provide different libraries. Some of the famous libraries are string, Stream, Files, Thread, etc. Sample Java Program:- Steps to create & run program with Eclipse Step 1 :- Install JDK & Eclipse. Step 2 :- Open Eclipse. Step 3 :- go to File New Java Project. Enter suitable project name. Step 4 :- Right click on Project & select Class. Enter Name of program & click on Finish. Step 5 :- Type Trial program. Make sure program name & class name is same. 1 public class Trial 2 { 3 public static void main (String [] args) 4 { 5 System.out.println (“ Welcome to the Java‟s World !! ”) ; 6 } 7 } Step 6 :- Save Program Late GN Sapkal College Of Engineering Nasik. Department Of Electronics & Telecommunications Trial . java Step 7 :- Build Project (goto Project Build Project). Step 8 :- Run Program (go to Run Run) Output:- Welcome to the Java‟s World !!

Line Number Explanation

1 It is used to start class. Public keyword is used to treat class as public which should be accessed by anyone. Class keyword is used to create class. Trail is name of class. 2 “ { ” is start of class definition. 3 It is start of main function. Public access specifier is used to make main function public. Static keyword is for main function. Static keyword allows calling function without object of class. Generally function should be called using object only. But main function called by JVM without using any object. That is why static keyword is used. Void is used to return nothing. String [ ] args is used to pass argument to main function. 4 “ { ” is used to start main function. 5 It is used to print message on screen. Message can be anything & which should be enclosed in double quotes “ ”. 6 “ } ” is used for closing main function definition. 7 “ } ” is used for closing class definition.

Input: - User should give the input as a number. Output:- The appropriate factorial of number would be as output, display first 50 prime numbers, and also shows sum and average of „n‟ number. Conclusion:- Understand the basics of java and learn how to write java program. References:- [1] Herbert Schildt, Java: The complete reference, Tata McGraw Hill, 7th Editon.