You are on page 1of 40
Introduction to JAVA ® Infobizzs.com ) Introduction ¢ History e Java is related to C++, which is a direct descendent of C. Much of the character of Java is inherited from these two languages. ¢ From C, Java derives its syntax. Many of Java’s object-oriented features were determined by C++. e Java was developed by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and 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. Infobizzs.com eo Comparison between OOP and pure OOP e Unlike C++, java is pure object oriented programming language. e Pure OOP organizes a program around its data (i.e. Objects) and set of well defined interfaces (i.e. methods) to that data. e Pure OOP means all of the data types in the language are objects and all of the operations on those objects are methods. e You can’t code anything in java without declaring classes and objects. e Everything in java is an object. All the program codes and data resides within classes and objects. —_ Infobizzs.com > e Advantages of java ¢ Simple: ¢ If you already understand the basic concepts of object-oriented programming, moving to Java will require very little effort because Java inherits the C/C++ syntax and many of the object-oriented features of C++. e Java was designed to be easy to use and is therefore easy to write, compile, debug, and learn than other programming languages. Infobizzs.com C:) Continue... ¢ Secure: e When you use a Java-compatible Web browser, you can safely download Java applets without fear of viral infection. e Java achieves this protection by providing a “firewall” between a networked application and your computer. e Java system not only verify all memory access but also ensure that no viruses’ are communicated with an applet. e The absence of pointers in java ensures that programs cdapkbbgais.egsess to memory Continue... ¢ Portable and Platform Independent: ¢ One of the most significant advantages of Java is its ability to move easily from one computer system to another. e Today many types of computers and operating systems are in used throughout the world and many are connected to the Internet. e We can download a java applet from a remote computer onto our local system via internet and execute it locally. e You can run java program on any type of operating sysignf 6éspzseohitsypprtability. e Continue... ¢ Object- Oriented: ¢ One of the most key features of java is Object Oriented. e Java is object-oriented because programming in Java is centered on _ creating objects, manipulating objects, and making objects work together. This allows you to create modular programs and reusable code. Infobizzs.com Continue... ¢ Robust: « Robust means reliable. e It provides many safeguards to ensure reliable code. e It has strict compile time and runtime checking for data types. e In C/C++, the programmer must manually allocate and free all dynamic memory. This sometimes leads to problems, because programmers will either forget to free memory that has been previously allocated or, worse, try to free some memory that jargtisizeeast @b their code is still picing Continue... e Java virtually eliminates these problems by managing memory allocation and deallocation. e Deallocation is completely automatic, because Java provides garbage collection for unused objects. e Java also incorporates the concept of exception handling which captures serious errors and eliminates any risk of crashing the system. Infobizzs.com Continue... ¢ Multithreaded: e Java supports multithreaded programming, which allows you to write programs that do many things simultaneously. e Architecture-Neutral: e Nowadays Operating system upgrades, processor upgrades, and changes in core system resources can all combine that make a program failure and doesn’t work properly. ¢ Once you written a java program, you can run this program anywhere, anytime forever. Infobizzs.com C) Continue... ¢ Interpreted and High Performance: ¢ The output of a Java compiler is not executable code. Rather, it is bytecode. e The Java bytecode was carefully designed so that it would be easy to translate directly into native machine code for very high performance by using a just-in-time compiler. Infobizzs.com Continue... ¢ Compiled and Interpreted: Usually, a computer language is either compiled or interpreted. Java combines both these approaches. Thus making java a two stage system. First, java compiler translates source code into bytecode. Bytecodes are not machine instructions and therefore, in the second stage, java interpreter generates machine code that can be directly executed by the machine that is running the java program. We can thus say that java is both a compilgg anda seesiee language. Continue... ¢ Distributed: Distributed computing involves several computers on a network working together. Java is designed to make distributed computing easy with the networking capability that is inherently integrated into it. e Writing network programs in Java is like sending and receiving data to and from a file. For example, three programs running on three different systems, communicating with each other to perform a joint task. Infobizzs.com Continue... e Dynamic: e Java programs carry with them substantial amounts of run-time type information that is used to verify and resolve accesses to objects at run time. This makes it possible to dynamically link code in a safe manner. oe Infobizzs.com Features of java ¢ Encapsulation Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse. Or The wrapping up of data and methods into single unit is known as encapsulation. Infobizzs.com Continue... ¢ 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. e For example, the bird sparrow is a part of the class flying bird, which is again a part of the class bird. Infobizzs.com e Continue... Flying Bird Bird Non Flying Bird Sparrow Crow Penguin Infobizzs.com e Continue... ¢ Polymorphism ¢ Polymorphism is a feature that allows one interface to be used for a general class of actions. e The concept of polymorphism is often expressed by the phrase “one interface, multiple methods.” This means that it is possible to design a generic interface to a group of related activities. Infobizzs.com What is byte code? e The output of a Java compiler is not executable code. it is bytecode. ¢ Bytecode is a highly optimized set of instructions to be executed by the Java run-time system, which is called the Java Virtual Machine (JVM). JVM is an interpreter for Bytecode. ¢ Bytecodes are a set of instructions that is not specific to any one processor. e When we compile a program written in C or in most other languages, the compiler translates our program into machine codes. Those instructions are specific to the processor your computer is running. © Infobizzs.com Continue... ¢ So, for example, if you compile your code on a Pentium system, the resulting program will run only on other Pentium systems. If you want to use the same program on another system, you have to go back to your original source, get a compiler for that system, and recompile your code. e Things are different when you write code in Java. The Java development environment has two parts: a Java compiler and a Java interpreter. The Java compiler takes your Java program and instead of generating machine codes from your source files, it.,generates bytecodes. Infobizz .com Continue... Binary File (Pentium) Nour Code Compiler (Pentium) Binary File (PowerPC) C) Compiler (PowerPC) Binary File (SPARC) Compiler (SPARC) Traditional compiled programs Infobizzs.com Continue... ¢ To run a java program, you run a program called a bytecode interpreter, which in turn executes your Java program. e Having your Java programs in bytecode form means that instead of being specific to any one system, your programs can be run on any platform and any operating or window system as long as the Java interpreter is available. e Only the JVM needs to be implemented for each platform. once the run-time package exists for a given system, any Java program can run on it. Infobizzs.com Continue... Java Bytecode (Platform Java Compiler 7 Independent) (Pentium) Java Code Java Compiler (PowerPC) ava Compiler (SPARG) Java Interpreter (SPARC) Java programs Infobizzs.com Applications of java ¢ To develop web browser ¢ To enhance capabilities of web browser. ° To develop web server e To enhance capabilities of web server ¢ To develop protocols e Java encryption/decryption algorithm implementation enables security on the internet. Infobizzs.com Basic structure of java program ‘Documentation Section |*#— suggested ‘Package Statement l¢—— optional Statements Import \<— optional Tnterface Statements < optional Class Definitions I<— optional ‘Main Method Class \<— Essenti ‘Main Method Definition al Infobizzs.com Continue... ¢ Documentation Section : ¢ The documentation section comprises a set of comment lines. In addition to two styles of comments, java also uses a third style of comment /**.....*/, known as documentation comment. e Package Statement : e The first statement allowed in a java file is a package statement. This statement declares a package name and informs the computer that the classes defined here belong to this package. The package statement is optional. Ex: package FES Hiz75.com Continue... ¢ Import Statements: ¢ The next thing after package statement is import statements. This is similar to the #include statement in C. Ex: import student test; e This statement instructs the interpreter to load the test class contained in the package student. Using import statements, we can have access to classes that are part of other named packages. Infobizzs.com Continue... ¢ Interface Statements: e An interface is like a class but includes a group of method declarations. This is also an optional section and is used only when we wish to implement the multiple inheritance feature in the program. e Class Definitions: eA java program may contain multiple class definitions. Classes are the primary and essential elements of a java program. Infobizzs.com First simple program Example.java class Example { public static void main (String args []) { System.out.printin ("Hello World."); } 6 Infobizzs.com Compiling the program ¢ To compile the Example program, execute the compiler, javac, specifying the name of the source file on the command line, as shown here: C:\>javac Example.java e The javac compiler creates a file called Example.class that contains the bytecode version of the program. The Java bytecode is the intermediate representation of your program that contains instructions the Java interpreter will execute. Thus, the output of javac is not code that can be directly executed. 6 Infobizzs.com oe Run the program e To actually run the program, you must use the Java interpreter, called java. To do so, pass the class name Example as a command-line argument, as shown here: C:\>java Example e When the program is run, the following output is displayed: Hello World. Infobizzs.com Continue... e When Java source code is compiled, each individual class is put into its own output file named after the class and using the .class extension. e When you execute the Java interpreter as just shown, you are actually specifying the name of the class that you want the interpreter to execute. e It will automatically search for a file by that name that has the .class extension. If it finds the file, it will execute the code contained in the specified class. Infobizzs.com e Description ¢ class Example « This line uses the keyword class to declare that a new class is being defined. Example is an identifier that is the name of the class. ¢ public static void main (String args []) e This line begins the main ( ) method. This is the line at which the program will begin executing. All Java applications begin execution by calling main (). Infobizzs.com Continue... ¢ The public keyword is an access specifier, which allows the programmer to control the visibility of class members. In this case, main ( ) must be declared as public, since it must be called by code outside of its class when the program is started. The keyword static allows main ( ) to be called without having to instantiate a particular instance of the class. This is necessary since main ( ) is called by the Java interpreter before any objects are made. The keyword void simply tells the compiler that main () does not return a value. Infobizzs.com Continue... e In main (), there is only one parameter, String args [ ], which declares a parameter named args, which is an array of instances of the class String. In this case, args receives any command- line arguments present when the program is executed. e System.out.printin ("Hello World."); ¢ This line outputs the string “hello World.” followed by a new line on the screen. Output is actually accomplished by the built-in printin ( ) method.In this case,printIn( ) displays the string which is passed to it. Infobizzs.com Continue... e System is a predefined class that provides access to the system, and out is the output stream that is connected to the console. 6 Infobizzs.com Java input output operators e I/O streams e Astream in java is a path along which data flows. e It has a source and destination. e Input stream reads the data from source and pass the data to program. ¢ Output stream writes the data from program to destination. Infobizzs.com Java stream classes Java streams Byte Stream lente Sl) URS) t= Data I/P Stream } Data O/P Stream e)e)(Telm ded Object O/P Stream Stream ito ae ir SMe to RO las oo Infobizzs.com Continue... ¢ The most basic java I/O is writing to and reading from console. e Java contains the built-in system, input stream and print stream classes to perform these operations. ¢ To read from the keyboard, the statement ¢ System.in.read(); e It will read a sequence of characters until the end of stream, i.e.the enter key has been detected or an exception has been thrown. ¢ To write data to the screen, « Sytem.out.printin(“hi’); Infobizzs.com Reading a data from keyboard import java.io.*; public class Echo { public static void main(String args[]) throws IOException { InputStreamReader reader; BufferedReader bufReader; reader=new InputStreamReader(System.in); bufReader=new BufferedReader (reader); String s; while(nulll=(s=bufReader.readLine())) System. out.println(s); Hello world } lalcomUTolaLe} Infobizzs.com

You might also like