HHHHHHHHHHH
HHHHHHHHHHH
Unit-1
Chapter 2
Introduction to Java
2 marks questions:
1. List any four features of Java
Ans:
• Compiled and Interpreted
• Platform-Independent and Portable
• Object-Oriented
• Robust and Secure
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 neutrality?The answer is that the Java compiler
produces an intermedia code known as bytecode for a machine that does not
exist. This machine is called the Java Virtual Machine and it exists only
inside the computer memory.
can be implemented on any machine. Secondly, the size of the primitive data
types are machine independent
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. The object model
in Java is simple and easy to extend
Robust and Secure
Java is a robust language. It provides many safeguards to ensure reliable code
It has strict compile time andruntime checking for data types. It is a garbage
collected language. Java incorporates the concept of exception handling which
captures series errors and eliminates any risk of crashing the system. Java
systems ensure that no viruses re communicated with an applet
Distributed
Java is designed as a distributed language. It has the ability to share both data
and programs
Simple, Small and Familiar
Java is a simple and small language. Many features of C and C++ that are either
redundant or sources of unreliable code are not part of Java. Java does not use
pointer, preprocessor header files, goto statement and many others. It also
eliminates operator overloading and multiple inheritance. Java is modelled on C
and C++ languages. Java uses many constructs of C and C++. Java code looks
like a C++ code. Java is a simplified version of C++
Multithreaded and Interactive
Multithreaded means handling multiple tasks simultaneously. Java supports
multithreaded programs. We need not wait for the application to finish one task
before beginning another. This improves the interactive performance of
graphical applications
High Performance
Java architecture is designed to reduce overheads during runtime. Overall
executions peed of Java programs is good
Dynamic and Extensible
Java is capable of dynamically linking in new class libraries, methods and
objects. Java can also determine the type of class through a query, making it
possible to either dynamically link or abort the program, depending on the
response. Java programs support functions written in other languagessuch as C
and C++. These functions are called native methods. Native methods are linked
dynamically at runtime
Ease of Development
Java 2 Standard Edition (J2SE) 5.0 supports different features. These features
reduce the work of the programmer by shifting the responsibilities to the
compiler
The resulting source code is free from bugs because errors made by the
compiler are less when compared to those made by programmers
Scalability and Performance
Improves the startup time and reduces the amount of memory used in runtime
environment
Monitoring and Manageability
Java supports a number of APIs, such as JVM Monitoring and Management
API to monitor and manage Java applications
Desktop Client
J2SE 5.0 provides enhanced features to meet the requirement and challenges
of the Java desktop users.
Java application program must include the main() method. This is the starting
point for the interpreter to begin the execution of the program. This line
contains other keywords public, static and void
• public: It is an access specifies that declares the main method as
unprotected and therefore making it accessible to all other classes.
• static: The main must always be declared as static since the interpreter
uses this method before any objects are created
• void: It states that the main method does not return any value
String args[] declares a parameter named args, which contains an array of
objects of the class type String
The Output Line
The only executable statement in the program is:
System.out.println("Java is better than C++");
This is similar to the printf() statement of C or cout<<construct of C++. Since
Java is a true object-oriented language, every method must be part of an object.
The println method is a member of the out object, which is a static data
member of System class. This line prints the string Java is better than C++.
The method println always appends a newline character to the end of the string.
This means that any subsequent output will start on a new line. Every Java
statement must end with a semicolon.
A Java program may contain many classes of which only one class defines a
main method. Classes contain data members and methods. Methods may
contain data type declaration and executable statements. To write a Java
program, we first define classes and then put them together. A Java program
may contain one or more sections
Documentation Section
A documentation section comprises a set of comment lines giving the name of
the program, the author and other details. Comments must explain why and
what of classes and how of algorithms. Java permits both the single line and
multi-line comments. The single line comments begin with // and end at the end
of the line. For longer comments, we can create long multi-line comments by
starting with a /* and ending with a */. Java also uses a third style of comment
/**….*/ known as documentation comment
Package Statement
The first statement allowed in a Java file is a package statement. This statement
declares a package name and informs the compiler that the classes defined here
belongs to this package.
Example: package student;
The package statement is optional
Import Statements
This is similar to the #include statement is C.
Example: import student.test;
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
Interface Statements
An interface is like a class but includes a group of method declaration. This is
also an optional section and is used only when we wish to implement the
multiple inheritance features in the program
Class Definitions
A Java program may contain multiple class definitions. Classes are the primary
and essential elements of a Java program. The number of classes used depends
on the complexity of the problem