Java Unit-1 Part 1 Introduction PDF
Document Details
Uploaded by Deleted User
Heta S. Desai
Tags
Summary
This document provides an introduction to Java programming, covering object-oriented concepts, and features like platform independence. It details the functionalities and advantages of Java compared to other programming languages. The content also discusses the structure and execution of Java programs.
Full Transcript
Unit 1: Java programming language Java I t is one of the programming language or technology used for developing web applications. Using this technology you can develop distributed application. Java language developed at SUN Micro Systems under the guidance of James...
Unit 1: Java programming language Java I t is one of the programming language or technology used for developing web applications. Using this technology you can develop distributed application. Java language developed at SUN Micro Systems under the guidance of James Gosling and there team. In other word it is a programming language suitable to the development of web applications. It is also used for develop desktop and mobile application. Java is an object oriented Programming- 1. Objects and Classes 2. Data abstraction and Data Encapsulation 3. Inheritance 4. Polymorphism 5. Dynamic binding 6. Message passing Features of java: Why java is so popular? The java Programming language has a number of features that makes it the language of choice for most of the developers. The reason Java is so popular is that it is object oriented, is platform independent, does not use pointers, has support for multi-threading, has a robust exception handling mechanism, has good security features and the most importantly java is so popular because java is easy. 1. Platform-Independent 2. Object Oriented BY: Heta S. Desai Page 1 Unit 1: Java programming language 3. Robust and Secure 4. Multithreaded 5. Distributed 6. Simple, small and Familiar 7. Dynamic and Extensible 8. High Performance Platform-Independent: What is meant by platform-Independent? Does it mean that it runs on any platform? Even c code can run on any platform. We just need to compile it for each platform. A C source file is first compiled and an executable file is created. An executable is made up of instructions, which are from specific instruction set. So C file will run only on the platform for which it has been compiled. In case of Java, java source file is also first compiled. The output of this compilation is not an executable which would work only on the platform on which it compiled, but it is the java Bytecode. Java Bytecode is set of instruction. This set of instruction is used by JVM (Java Virtual Machine). This Java bytecode can run on any platform for which the JVM is available. Object Oriented: Java is a true object-oriented language. Almost everything in java is an object. All program code and data reside within objects and classes. Robust and Secure: BY: Heta S. Desai Page 2 Unit 1: Java programming language Java is a robust language. It has strict compile time and run time checking for data types. Java has features of automatically memory allocation and garbage collection mechanism. It provides powerful exception handling and type checking mechanism compare to other programming language. Security becomes an important issue for a language that is used for programming on internet. Java system not only verifies all memory access but also ensure the no viruses are communicated with an applet. The absence of pointer in java ensures that programs cannot gain access to memory location without proper authorization. Multithreaded: With Java's multithreaded feature it is possible to write programs that can perform many tasks simultaneously. This design feature allows the developers to construct interactive applications that can run smoothly. Distributed: Java is designed as a distributed language for creating application on networks. It has the ability to share both data and programs. Java applications can open and access remote objects on internet as easily as they can do in a local system. Simple, Small and Familiar: Java is a small and simple language. Many features of C and C++ that are either redundant or sources of unreliable code are not a part of java. For example, Java does not support pointer, preprocessor header files, goto statement and other. It is also eliminated operator overloading and multiple inheritance. Java uses many constructs of c and c++ and therefor, java code looks “like a c++”. BY: Heta S. Desai Page 3 Unit 1: Java programming language Dynamic and Development: Java is a dynamic Language. Java is capable of dynamically linking in new class libraries, methods and objects. Java programs support functions written in other programming language such as C and C++. High performance: Java performance is impressive because the use of intermediate bytecode. Java architecture is also design to to reduce overheads during runtime. Comparison of Java with C++: Java is lot like C/C++ but the major difference is there. Listed below are some major c++ features: Java does not support operator overloading. Java does not supports multiple inheritance of classes this is accomplished using introduce new concept called “interface”. Java does not support global variable. Every methods and variable must be declared within a class. Java does not use pointer. There are no header files in java. Java has replaced the destructor function with a finalize() function. BY: Heta S. Desai Page 4 Unit 1: Java programming language Java Environment: Java Environment includes a large number of development tools and numbers of classes and methods. The development tools are part of the system called Java Development Kit (JDK) and classes and methods are part of the Java Standard Library (JSL). Java Development Kit (JDK): Java development kit includes collection of tools that are used for developing and running java programs: Javac (java compiler): The java compiler, which translates java source code to bytecode that the interpreter can understand. Java source code must be contained in files whose filenames end with the.java extension. The file name must be constructed from the class name, as classname.java, if the class is public or is referenced from another source file. For every class defined in each source file compiled by javac, the compiler stores the resulting bytecodes in a class file with a name of the form classname.class. Unless you specify the -doption, the compiler places each class file in the same directory as the corresponding source file. Java (java interpreter): Java Interpreter, which runs applet and applications by reading and interpreting bytecode files. BY: Heta S. Desai Page 5 Unit 1: Java programming language Java Runtime Environment (JRE): Java Runtime Environment (JRE) is a part of Java Development Kit (JDK). The java Runtime Environment facilitates the execution of programs developed in java. It contains the following: Java Virtual Machine(JVM): JVM is an abstract machine. A java program execution uses a combination of compilation and interpretation. Programs written in Java are compiled into machine language, but it is a machine language for a computer that is, virtual and doesn't really exist. This so-called "virtual" computer is known as the Java virtual machine (JVM). The java compiler produces an intermediate code called as bytecode for a machine that does not exist. The machine is called the Java virtual Machine and only exist inside the computer memory. Java Program Java Compiler Virtual Machine Process of Compilation The machine language for the Java virtual machine is called Java bytecode. Java Virtual Machine is a program that runs pre compiled Java programs, which mean JVM executes.class files (byte-code) and produces output. The JVM is written for each platform supported by Java included in the Java Runtime Environment (JRE). Runtime class library: BY: Heta S. Desai Page 6 Unit 1: Java programming language These are a set of core class libraries are required for the execution of java programs. Simple java Program: public class hello { public static void main(String [] args) { System.out.println("Hello world"); } } Note: class keyword is used to declare a class in java. public keyword is an access modifier which represents visibility, it means it is visible to all. static is a keyword, if we declare any method as static, it is known as static method. The core advantage of static method is that there is no need to create object to invoke the static method. The main method is executed by the JVM, so it doesn't require to create object to invoke the main method. So it saves memory. void is the return type of the method, it means it doesn't return any value. main represents startup of the program. String args[] is used for command line argument. BY: Heta S. Desai Page 7 Unit 1: Java programming language System.out.println() is used print statement. We will learn about the internal working of System.out.println statement later. About Java programs, it is very important to keep in mind the following points. Case Sensitivity − Java is case sensitive, which means identifier Hello and hello would have different meaning in Java. Program File Name − Name of the program file should exactly match the class name. When saving the file, you should save it using the class name (Remember Java is case sensitive) and append '.java' to the end of the name (if the file name and the class name do not match, your program will not compile). Example: Assume 'hello' is the class name. Then the file should be saved as 'hello.java' public static void main(String args []) − Java program processing starts from the main() method which is a mandatory part of every Java program. Public : is an Access Modifier, which defines who can access this Method. Public means that this Method will be accessible by any Class (If other Classes are able to access this Class.). Static : is a keyword which identifies the class related thing. This means the given Method or variable is not instance related but Class related. It can be accessed without creating the instance of a Class. BY: Heta S. Desai Page 8 Unit 1: Java programming language void : is used to define the Return Type of the Method. It defines what the method can return. Void means the Method will not return any value. main: is the name of the Method. This Method name is searched by JVM as a starting point for an application with a particular signature only. String args[] : is the parameter to the main Method. Java Program Structure A java program may contain many classes of which only one class defines main method. A Java program consists of different sections. Some of them are mandatory but some are optional. The optional section can be excluded from the program depending upon the requirements of the programmer. [Genral structure of java program] BY: Heta S. Desai Page 9 Unit 1: Java programming language Documentation Section It includes the comments to tell the program's purpose. It improves the readability of the program. Single line (or end-of line) comment: It starts with a double slash symbol (//) and terminates at the end of the current line. The compiler ignores everything from // to the end of the line. For example: // Calculate sum of two numbers Multiline Comment: Java programmer can use C/C++ comment style that begins with delimiter. All the text written between the delimiter is ignored by the compiler. This style of comments can be used on part of a line, a whole line or more commonly to define multi-line comment. For example. Package Statement It includes statement that provides a package declaration. Import statements It includes statements used for referring classes and interfaces that are declared in other packages. Interface Section It is similar to a class but only includes constants, method declaration. Class Section It describes information about user defines classes present in the program. Every Java program consists of at least one class definition. This class definition declares the main method. It is from where the execution of program actually starts. BY: Heta S. Desai Page 10 Unit 1: Java programming language Note: Java can be created two types: 1. Application 2. Applet Application: It is a program similar to the c language that runs under the c language. In java all the codes must be resides inside the class which have data member and methods that operate data member of the class. Java program may contain one or more classes of which only one class define main method from were program execution starts. Applet: It is a small window base program that runs on any HTML page. It has run any GUI based interface. To run java applet we need java applet viwer. BY: Heta S. Desai Page 11