1 Introduction to java — Java Programming
1.1 Introduction
James Gosling Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan gave the specifications of Java. Most of the Java features were inherited from C++. Java has become very important to internet. Java gives us features to construct very simple to networked programs along with security and portability.
In this chapter, you will learn the essence of Java, how Java facilitates as an Object oriented language and the differences between Java and C++. You will run your first Java program and will also be oriented to the form and structure of Java programs.
1.2 Features of Java
The following are the key features of Java.
- Java is one of the few truly platform independent languages. This means that if you write a program in Java in one computer, you can run it on any computer running on any platform.
- Java is an object oriented programming language.
- Java enhances and refines the object-oriented paradigm used by C++.
- Java is best suited for distributed client/server systems on the net.
- Java supports applets that are sent on Net. It has classes for networking programming.
- Java is portable.
- Java is robust, It is well behaved and reduces system crashes.
- Java is secure. The tight security features protect the applications from vire etc.
- Java has high performance.
- Java supports reuse. It supports CORBA(Common Object Request Broker Architecture) and DCOM(Distributed Common Object Model).
- It supports multithreading, just in time compiling (JIT) and native code usage.
1.3 Object Oriented Programming and Java
The five basic characteristics which represent a pure approach to Object oriented programming are:
- Everything is an Object. An object stores data and performs operations on it.
- A program is a bunch of objects telling each other what to do by sending messages.
- Every object has its own memory made up of other objects.
- Every object has a type. Each object is an instance of a class, where class is a type.
- All objects of a particular type can receive messages. For example, an object of type circle is also an object of type shape, a circle is guaranteed to receive shape messages. This means that we can write code that talks to shapes and automatically handle anything that fits the description of a shape.
The four aspects of object oriented technology that are supported in Java:
- Encapsulation
- Inheritance
- Polymorphism
- Dynamic binding
1.4 Difference between C++ and Java
- There are no pointers in Java
- There is no garbage collection in C++.
- There are no structures and unions in Java.
- There are no define statements in Java
- There is no multiple inheritance in Java.
- There is no go to statement in Java.
- There are no individual functions in Java.
- There is no Operator overloading in Java.
1.5 Different places of storing the data
When a program is running, the following are the different places where data is stored. They are:
- Registers – CPU registers
- The stack – RAM area, accessed via stack pointer.
- The heap – RAM area allocated through new operator. It is a java operator to create an object.
- Static storage – This refers to some section in RAM. It contains data that is available for the entire time the program is running.
- Constant storage – Constant values are placed in program code, which is safe since, they can never change. They can be optimally placed in ROM.
- Non-RAM storage- The data exists even when the program is not running. In this case the program does not have any control over the data.
1.6 Compilation of Java programs
Before being executed, Java programs must be translated and compiled into a form the computer can best understand. Hence we use the Java complier to translate any program to an executable form. The executable form of the program is called a class file and has the suffix .class. Programs are compiled to Java machine code. The Java machines directly can execute such code. Hence we must run a Java machine simulator (interpreter) to execute our Java programs.
Take the following program first.java, and copy the file path.
Program 1.1 A Sample Java Program
//first.java //click here for code class first{ public static void main(String args[]){ System.out.println(“my first java program”); } }
Now compile it using the following command.
C:\ javac first.java
A class file first.class is generated, Now run it using.
C:\ java first
You will obtain the output:
my first java program
/**/ is a multiple line comment statement.
// is single line comment.
Note that class is a keyword to declare that a new class is being defined. Also first is an identifier, that is the name of the class.
The public keyword is an access specifier, which allows the programmer to control the visibility of class members. This means that the member can be accessed by code outside the class in which it is declared.
static allows main() to be called without having to instantiate a particular instance of the class.
void means main() function returns nothing.
String args[] declares a parameter named args, which is an array of instances of the class String. Every Java program is contained inside a class definition.
System.out.println(“my first program”);
System is a predefined class that provides access to the system, out is the output stream that is connected to the console. All statements in Java end with a semicolon.
{ and } indicate beginning and ending of class or methods. The code between these two is called a block.
Note: For a few programs after compiling you may get a warning as shown below, you can simply ignore it and continue.
Student.java uses or overrides a deprecated API. Recompile with “-deprecation for details. 1 warning
1.7 Summary
A discussion on Java and its importance as an OOP language is given. Also, the procedure to compile and run a simple Java program is discussed.