Header Ads

Introduction to Java



Java is an Object Oriented Programming language derived from C++. Java was first developed by James Gosling, Patrick Naughton, Chris Warth, Ed Frank and Mike Sheridan at Sun Microsystems, Inc. in 1992. The language was initially called, Oak" but was renamed to Java in 1995. In year 2010, Sun microsystem is acquired by Oracle. 

Java Development Kit (JDK): Java Software is available downloaded (free) in different versions of JSDK (Java System Development Kit) also called as JDK. JDK are available in single Executable file on net. JDK stands for Java Development Kit, also called as Java System Development Kit (JSDK). It is a software made up of different utilities, using which user can compile, debug (check for errors) and execute Java programs. Java (JDK) consists of Java Compiler, Java Runtime Environment (JRE) and some demo Java programs. 

JRE: This mainly consists of JVM and Java API. JVM stands for Java Virtual Machine and also called as Java interpreter. This is used to execute Java programs. Java consists of hundreds of in built Classes / interfaces and thousands of methods in them. This set of classes and interfaces is called as Application Programming Interface (API). 

Demo programs: This consists of some Java programs to assist new Java programmer to develop small program. 

The classes and interfaces (i.e. Java API) are categorized according their use/purpose and are stored under multiple packages. 

For different operating systems different versions of JDK are available. For example, for Java programming in Windows, one should use JDK for Windows, and programming on UNIX system one should use JDK for UNIX. 

The commonly used utilities of JDK are: 

1. Java Compiler: This is a javac.exe. Used to compile Java application. It compiles the Java files into class files i.e. Bytecode files. Like any other compiler it checks syntax errors and displays them. If there is no error in a program it is compiled to one or more class files. 

2. Java Virtual Machine (JVM): This is java.exe. Used to run a Java application. JVM loads a specified Class file (byte-code file) into computer and step by step interprets the instructions and executes them. This is an important part of Java that makes the language Platform independent. 

3. Java disassembler: This is javap.exe. Enables to convert bytecode file into program description (actually class description).

4. Java Debugger: This is jdb.exe. Using this programmer can move through a program step by step and check errors (called as debugging). 

5. Appletviewer: This is appletviewer.exe. It is a small Java browser. Appletviewer enables us to run Java applets (without using Java enabled browser like IE). 

6. JAR: It is a Java Archive utility. This is commonly used to compress multiple files (like Java source file, class files and other files like image, sound files) into one JAR file i.e. we can create one jar file using this utility. 

Comparison between Procedural Programming Language and Object Oriented programming

Comparison between Procedural Programming Language and Object Oriented programming


Drawbacks of Object Oriented Programming 

1. Object oriented programming is complex to implement because every entity in it is object. We can access the methods and attributes of particular class using object of that class. 

2. If some of the members are declared as private, then those members are not accessible by the object of another class. In such a case you have to make use of inheritance property. 

3. In Object oriented programming, everything must be arranged in the forms of classes and modules. For the lower level applications, it is not desirable feature. 

Applications of Object Oriented Programming: Various applications in which object oriented programming is preferred are- 

• Business logic applications. 

• Real time systems 

• Knowledge based and expert systems 

• Web based applications 

• Simulation and modeling 

• Object oriented databases 

• Applications in distributed environment 

• CAD/CAM Systems 

• Office automation system 

• Games programming 

Importance of Java to the Internet 

  • The evolution of Internet helped make Java a leading programming language. Over the network there  are two very broad categories of objects that are transmitted between the  server and the client's computer. 
  • These are the passive and active objects in the form of information and active programs. When an email is viewed it is viewing passive data. 
  • A downloaded program until it is executed is also passive data. A self-executing program  is dynamic data. This data is called as a active agent on the client computer. 
  • These dynamic networked programs create problems in the area of security and portability. Earlier to the evolution of Java, the cyberspace was closed to half the entities that are now present there. 
  • Java effectively handled these concerns. This was done by introducing a new form of program, an Applet

a. Java Applets:

  • An alphabet is a special program that is transmitted over the Internet. It gets automatically executed by a Java- compatible web browser. An applet is downloaded only on the demand of the user.
  • The applet is said to be an "Intelligent Program". This is because the applet tends to react to user input and change its status dynamically.
  • Working with applets very exciting. This would not have been possible without a programming language like Java. Java took care of two of the major issues of concern while dealing with applets. These were security and portability of any program code over the Internet.

b. Security:

  • Whenever a program is downloaded from the Internet, there is a risk of virus infection to the computer Before the evolution of Java not much programs were downloaded from the Internet due to the fear of virus.
  • Also certain malicious programs, that gather private information like credit card numbers, bank account numbers/balances, could be downloaded. Java provides a solution to this by providing a "firewall" between the downloaded application and the computer.
  • Whenever a Java program/applet a downloaded and executed on a Java-compatible we browser, there is no fear of virus infection.
  • This is because the Java program is confined or restricted to the Java execution environment. It is not allowed to access other parts of the computer. Thus assuring security.

c. Portability:

  • These are 'n' different types of computers with varying configurations of the CPU operating systems. These are connected to the Internet.
  • For a program that needs to be downloaded and then executed, means it is required to make the same program work on varied platforms i.e. a way needs to be found to make the same Java program portable.
  • Java provides the Java Virtual Machine (JVM) that answers this question.
  • Many types of computer are connected and operating systems are in use throughout the world and are connected to the internet. For downloading programs through different platforms connected to the internet, we require portable, executable code is needed.


Features of Java

Features of java


1. Simple Programming Environment & Powerful: It is simple because of the following factors:

a. It is free from pointer due to this execution time of application is improve [whenever we write a Java program without pointers then internally it is converted into the equivalent pointer program]

b. It have Rich set of API (application protocol interface).

c. It have Garbage Collector which is always used to collect un-Referenced (unused) Memory  location for improving performance of a Java program. It contains user friendly syntax developing any applications.

Java programming environment
2.  Platform Independent

 

Java platform independent

A program written in Java, is independent of the hardware and Operating System. A complied Java program (bytecode) can be run on any Operating Systems with the help of Java Virtual Machine (JVM). But JVM is platform dependent. 

3. Architectural Neutral 

Architectural neutral
Architecture represents processor. A Language or Technology is said to be Architectural neutral which can run on any available processors in the real world without considering their architecture and vendor (providers) irrespective to its development and compilation. The languages like C, CPP are treated as architectural dependent.

4. Portable

Portability platform independent + architecture

If any language supports platform independent and architectural neutral feature known as portable. The languages like C, CPP, Pascal are treated as non-portable language. It is a portable language.

5. Multithreaded

A flow of control is known as thread. When any Language execute multiple thread all a time that language is known as multithreaded Language. It is multithreaded Language.

6. Distributed

Using this language, we can create distributed application. RMI and EJB are used for creating distributed applications. In distributed application multiple client system are depends on multiple server systems so that even problem occurred in one server will never be reflected on any client system

7. Networked

It is mainly designed for web based  applications, J2EE is used for developing network based applications 

8. Robust

Simply means of Radiant is strong. It is robust or strong Programming Language because of its capability to handle Run-time Error, automatic garbage collection, lack of pointer concept,  Exception Handling. All these points make it robust Language

9. Dynamic

It supports Dynamic memory allocation due to this memory wastage is reduce and imp[rove performance of application. The process of allocating the memory space to the input of the program it a run-time  is known at dynamic memory allocation, To programming to allocate memory space by dynamically we use an operator called ‘new’ ‘new’ operator is known as dynamic memory allocation operator

10. Secure

It is more secured language compare to other language; Is this language all code is covered into byte code after compilation which is not readable by human.

11. High performance

Java gives high performance because of following reasons; This language use Bytecode which is faster than ordinary pointer code so Performance of this language is high. Garbage collector,  collect the unused memory space and improve the performance of application. It has no pointers  so that using this language we can develop an application very easily. It supports multithreading, because of this time consuming process can be reduced to execute the program.

12. Interpreted

It is one of the highly interpreted programming languages.

13. Object Oriented

Is supports OOP's concepts because of this it is most secure language, for this topic  you can read our oop's concepts in detail.

Data Types in Java :

Integer Types

Java consists of four integer types: byte, short, int, and long, which are defined as 8-, 16-,32-, and 64-bit signed values as summarized in the Table below :

Data types in java

The Java integer primitive types.

Integer datatype in java

Floating-Point Types

floating point in java


By default, Java  treats floating-point type as double data-type.

Ex. value 2.5 is treated as double type data. To use the value as float type we attach letter f for F to the value i.e. 2.5f/2.5F is treated as float type value.

Boolean: Java supports two Boolean datatypes-true and false.

Character Literals : 

Type name Size in bits

char 16

This data is used in Java to hold characters, Java uses Unicode to represent characters.

String Literals: Although there it no string primitive type in Java, you can include string literals in your programs. Most applications and applets will make use of some form of string literal, probably at least for error message. A string literal consists of zero or more characters (including the escape sequences) enclosed in double quotes

KEYWORDS

keywords in java


Java Virtual Machine (JVM) & Byte Code

All language compilers translate source code into machine code for a specific computer. Java compiler also does the same thing. Then, how does Java achieve architecture neutrally? The answer is that the Java compiler produces an intermediate code coed known as byte code for a machine that does not exist. This machine is called the Java Virtual Machine and it exists only inside the computer memory. It is a simulated computer within the computer and does all major functions of a real computer. Figure illustrates the process of compiling a Java program into byte code and is referred as virtual machine code.

process of compilation

The virtual machine code is not machine specific. The machine specific code (known as machine code) is generated by the Java interpreter by acting as an intermediary between the virtual machine and the real machine as shown in Figure below. Remember that the interpreter is different for different machine. 

Process of Converting Byte Code into Machine Code

JVM consists of the following components as shown in the diagram. The components shown are abstract in nature. It completely depends upon the designer to develop these components. 

Class loader Sub-System 

JVM architecture

When a Java file is compiled, class files are generated. When we invoke a class file for execution, the class loader sub-system is called by Java Virtual Machine to load the class file into JVM memory space. The task of class loader sub-system is to load the classes and interfaces in the memory. Two types of loaders are available in a Java Virtual Machine: a boot strap loader, and a user-defined class loader. The job of the class loader sub-system is not only to load the class file but to also check the correctness of the loaded class. It allocates the required memory to static variables (static variables are explained in class fundamental). The class loader sub-system performs its task in a sequential way:

(a) It loads a class file.

(b) It checks the correctness of class file. If anyone has manipulated the class file, then that class file cannot be executed. The class loader sub-system is responsible for this behavior.

(c) It allocates memory to static variables. Static variables are otherwise known as class variables. They are the property of classes rather than the property of objects.

(d) It transforms symbolic reference into direct reference.

(e) It sets default value of all static variables.

Method Area: It is a logical memory component of JVM. This logical section of memory holds information about classes and interfaces. Static variables are treated as class variables, became they take memory from method area. The size of the method area is not fixed. It shrinks and expands according to the size of the application. In Java when an object or an array is created, memory is allocated to them from heap. The JVM through the use of new operator allocates memory from the heap for an object. The JVM has a demon thread known as Garbage Collector whose task is to free those objects from heap whose reference is not alive in stack.

Java stacks: Method codes are stored inside method area. For execution, a method needs memory because of the local variables and the arguments it has taken. This purpose is fulfilled by the Java stack.

Program counter (PC) registers: It keeps track of the sequence of execution of the program. PC registers or Program counter register holds the addresses of the instructions to be executed next.

Native method stacks: When a Java application invokes a native method, that application does not only use Java stack but also uses the native method stack for the execution of native methods. Native methods are generally in C/C++ and are executed inside the native method stack. The libraries required for the execution of native methods are available to the Java Virtual Machine through Java Native Interface.

Execution Engine: Execution engine generates and executes the Java byte code. It contains an interpreter and Just-in Time compiler.

BYTE CODE: It is the unique characteristic property of the Java programming language. It is something like a normal text file. Therefore, it cannot be affected by virus. It is an intermediate between a human readable source and a machine readable source. Byte codes are platform independent. Therefore, JVM is platform dependent to make Java programming platform independent. Just like C source codes, the byte codes are also portable. The cost of platform independent behavior of byte codes is paid by its slower execution speed when it is to the execution of native codes. 

Q. How Java is Platform Independent 

A programming language or technology is said to be platform independent if and only if which can run on all available operating systems with respect to its development and compilation. (Platform represents Operating System). 

Java is a platform independent programming language, Because when you install jdk software on your system then automatically jvm are install on your system. For every operating system separate jvm is available which is capable to read .class file or byte code. When we compile your java code then .class file is generated by javac compiler these code are readable by jvm and every operating system have its own jvm so jvm is platform dependent but due to jvm java language is become platform independent. 

java platform independent

Imp. Note: JAVA is platform independent but JVM is platform dependent.

Difference between JDK, JVM and JRE. 

Jvm, Jre, Jdk these all the backbone of java language. Each component has separate works. Jdk and Jre physically exists but Jvm are abstract machine it means it not physically exists.

JVM 

JVM (Java Virtual Machine) is a software. It is a specification that provides runtime environment in which java bytecode can be executed. It not physically exists. 

JVMs are not same for all hardware and software, for example for window os JVM is different and for Linux VJM is different. JVM, JRE and JDK are platform dependent because configuration of each OS differs. But, Java is platform independent. 

JRE 

JRE

The Java Runtime Environment (JRE) is part of the Java Development Kit-(JDK). It contains set of libraries and tools for developing java application. The Java Runtime Environment provides the minimum requirements for executing a Java application. It physically exists. It contains set of libraries + other files that JVM uses at runtime. 

JDK

The System class - The class provides System related information and also gives handle to some of the system functions like IO with console. 

Static Fields: in, out, err. 

System.out: It refers to output device. 

System.in: It refers to input device. 

System.err: It outputs error messages. 

Some methods: 

1. static long current Time Mills() - It returns the current time in milliseconds 

2. static void exit(int) - It terminates currently running JVM. 

3. static void gc() - It runs the Garbage Collector. 

4. static void arraycopy (object src, int srcStart, object dest, int destStart, int length) - It copies entire or part of one array to the other array. 

5. static String getenv(String name) - This method gets the value of the specified 

environment variable. 

6. static Properties getProperties() - This method determines the current system properties. 

7. static SecurityManager getSecurityManager() - This method gets the system security interface. 

8. static void load(String filename) - This method loads a code file with the specified filename from the local file system as a dynamic library. 

9. static void loadLibrary(String libname) - This method loads the system library specified by the libname argument. 

10. static long  nano Time() - This method returns the current value of the most precise available system timer, in nanoseconds. 

11. static void runFinalization() - This method runs the finalization methods of any objects pending finalization. 

12. static void setSecurityManager(SecurityManager  s)- This method sets the System security. 

Difference between Java and C++

difference between java and C++

                                                             


No comments

Powered by Blogger.