You are on page 1of 32

1.

2 Basics of Java Programming


Language
Dr.Narayana Swamy Ramaiah
Assoc.Prof, Dept of Electrical and
Computer Engineering,
Arba Minch University
• Massive growth of internet and WWW (client server,
distributed)

• Java is a general purpose, object oriented programming


language developed by sun Microsystems of USA in 1991.

• Reliability and portability are limitations of C and C++


• Java Technology is both a programming language and
platform.

• Java is
– simple, reliable, portable, dynamically adaptable and
powerful language.
– It is a platform-neutral language, It is first programming
language which is not tied to any particular hardware or
operating system.
– It is first application language of the world wide web
– It will also become premier language for general purpose
stand alone applications
Java programming language,
•All source code is first written in plain text files ending with the .java extension. Those
source files are then compiled into .class files by the javac compiler.

•A file does not contain code that is native to your processor; it instead
.class
contains byte codes — the machine language of the Java Virtual Machine (Java
VM).

•The java launcher tool then runs your application with an instance of the Java
Virtual Machine. Because the Java VM is available on many different operating
systems, the same .class files are capable of running on Microsoft Windows, the
Solaris™ Operating System (Solaris OS), Linux, or Mac OS
• An overview of the software development process.
The Java Platform
•A platform is the hardware or software environment in which a program runs, like Microsoft
Windows, Linux, Solaris OS, and Mac OS. Most platforms can be described as a combination of
the operating system and underlying hardware.

•The Java platform differs from most other platforms in that it's a software-only platform that
runs on top of other hardware-based platforms.

•The Java platform has two components:


•The Java Virtual Machine
•The Java Application Programming Interface (API)
•Java Virtual Machine; it's the base for the Java platform and is ported onto various hardware-
based platforms.
•The API is a large collection of ready-made software components that provide many useful
capabilities. It is grouped into libraries of related classes and interfaces; these libraries are known
as packages.

•The API and Java Virtual Machine insulate the program from the underlying hardware.
•As a platform-independent environment, the Java platform can be a bit slower than native code.
However, advances in compiler and virtual machine technologies are bringing performance close to
that of native code without threatening portability.
JAVA Features
• Compiled and interpreted
– Usually a computer language is either compiled or
interpreted
– JAVA combines both of these approaches thus
making JAVA a two stage system
– First stage - JAVA compiler translates source code
into what is known as byte code instructions.
– Second stage – interpreter generates machine
code from Byte code instructions
• Platform independent and portable
– Byte code instructions can be implemented on
any machine
– Size of the primitive data types are machine
independent.
• Object oriented
– Java is true object oriented. Almost everything in
java is object
– All program code and data reside within object
and classes
– Java comes with extensive set of classes, arranged
in packages, that we can use in our programs by
inheritance.
– Object model in java is simple and easy to extend.
• Robust and secure
– Robust
• Java is a robust language. It provides many safeguards to
ensure reliable code. It has strict compile time and run time
checking for data types.
• It is designed as a garbage collected language relieving the
programmer virtually all memory management problems.
• Java also incorporates the concept of exception handling
which captures series errors and eliminates any risk of
crashing the system.
– Security
• Threat of viruses and abuse of resources are every where
• Java system not only verify all memory access but also
ensure that no viruses are communicated with an applet.
• The absence of pointers in JAVA ensures that programs
cannot gain access to memory locations with out proper
authorization.
• Distributed
– Java is designed as a distributed language for
creating applications 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.
– This enables multiple programmers at multiple
remote locations to collaborate and work together
on a single project.
• Simple, Small and familiar
– Many features of c and c++ that are either redundant
or sources of unreliable code are not part of java.
• For example java does not use pointers, pre-processor
header files, goto statement and many others.
• It also eliminates operator overloading and multiple
inheritance.
• Multithreaded and interactive.
– Multithreading means Handling multiple tasks
simultaneously,
• for example we can listen to an audio clip while scrolling a
page and at same time download an applet from an distant
computer. This feature greatly improves the interactive
performance of graphical applications.
• Java runtime comes with tools that support multiprocessor
synchronization and construct smoothly running interactive
systems.
• High performance
– Java performance is impressive for an interpreted language,
mainly due to the use of intermediate bytecode.
– Java speed is comparable to native c/c++
– Java architecture is also designed to reduce over heads during
runtime.
– Further incorporation of multithreading enhances the overall
execution speed of java programs.
• Dynamic and extensible
– Java is a dynamic language.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 program supports function written in other languages such
as c/c++. These functions are know as native methods.
– This facility enables the programmers to use effective functions
available in these languages.
– Native methods are dynamically linked at runtime.
• Ease of development
– J2SE supports features such as generics,enhanced for loop,
autoboxing or unboxing. Typesafe enums, varags , static import
and annotation.
– These features reduce the work of the programmer by shifting
the responsibility of creating the reusable code to the compiler.
– The resulting code from compiler is free from bugs because the
errors made by the compiler are less compared to made by
programmers.

• Scalability and performance


– J2SE assures a significant increase in scalability and performance
by improving the startup time and reducing the amount of
memory used in java2 runtime environment.
– For example the introduction of the class, data sharing in the
hotspot JVM improves the startup time by loading the core
classes from the jar files into a shared archive. Memory
utilization is reduced by sharing data in the shared archive
among multiple JVM processes.
• Monitoring and Manageability
– Java supports a number of APIs, such as JVM
Monitoring and management API, sun
management platform extension, logging,
monitoring and management interface, and java
management extension (JMX) to monitor and
manage java applications.
– For example java provides JVM monitoring and
management API to track the information at the
application level and JVM level when deploying a
large application. Java provides tools, such as
jconsole, jps and jstatd to make use of monitoring
and management facilities. For example , GUI
based tool call jconsole is used to monitor the
JVM.
• Desktop client
– J2SE5 Provides enhanced features to meet the
requirements and challenges of the java desktop
users. It provides an improved swing look and feel
called ocean. This feature is mainly used for
developing graphics applications that require open
hardware accelerations.
JAVA ENVIRONMENT
• Java environment include large number of
development tools and hundreds of classes
and methods.
– Development tools are part of system known as
Java Development Kit(JDK)
– Classes and methods are part of Java standard
library (JSL) also known as Java API.
JAVA API(Application programming interface)
• The JAVA standard library includes hundreds of classes
and methods grouped into several functional
packages . Most commonly used packages are:
– Language support package: a collection of classes and
methods required to implementing basic features of JAVA.
– Utilities package: a collection of classes to provide utility
functions such as date and time functions.
– Input/output package: a collection of classes required for
input/output manipulation.
– Networking package: a collection of classes for
communicating with other computers via internet.
– AWT package: the abstract window tool kit package
contains that implements platform independent GUI
– Applet package: this includes a set of classes that allows us
to create JAVA applets.
QUIZ
What is the incorrect statement about JDK, JRE, JVM and JIT

a JIT is Just In Time Compiler and it is inside the JVM


Java Compiler is included in JDK (Java Development
b
Kit)
What is the incorrect statement about JDK, JRE, JVM
and JRE contains JVM and the java class libraries and
c JIT
the run-time libraries
JVM contains JRE and the java class libraries and
d
the run-time libraries
Introduction to Java Programming
• Java is a general purpose OOP
• We can develop two types of java programs
– Stand alone applications
• Compiling source code into bytecode using javac compiler
• Executing the bytecode program using java interpreter
– Web applets
• Applets are small java program developed for internet
applications like simple animated graphics to complex games
and utilities
• Runs with in java enabled web browser
Two ways of using Java
Simple Java program

Class SampleOne
{
public static void main (string args[])
{
System.out.println (“Java is better than C++.”);
}
}
• Class declaration
– First line
class SampleOne

Keyword Identifier
• Declares a class, which is an object oriented construct
• Java is a true OOP language and therefore, everything
must be placed inside class
• Class is a keyword and declares that a new class
definition follows.
• SampleOne is a java identifier
• Opening Braces
– Every class definition in java begins with opening
brace “{“ and ends with closing brace “}”.
• The Main Line
– The third line defines a method named main, main(),
– Java application can have any number of classes but only
one of them must include a main method to initiate the
execution,
– Java applets will not use main method at all.
– Main method prints text to screen
main() Method is Starting point for
interpreter to begin execution of
application program

public static void main (String args[ ])


Keyword Keyword Keyword Parameters
Access Specifier that
Interpreter uses this Type modifier main Args, contains an array
declares main method
method before any method does not of objects of class
accessible to all
objects are created return any value type string
classes -unprotected
• The output line
– The only executable statement in the program

System.out.println (“Java is better than C++.”);


Class Object Method

– The println method is a member of out object, which is


static data member of system class.
– This line prints
Java is better than C++.
– Println appends new line character to end of the string
– Every statement in java must end with semicolon
Basic Syntax
• Case sensitivity- java is case sensitive, which means identifier Hello and hello
would have different meaning.

• Class names- for all class names the first letter should be upper case. If several
words are used to form a name of the class, each inner word’s first letter should be
in upper case.
– Example class MyFirstJavaClass

• Method Names- all method names should start with a lower case letter. If several
words are used to form the name of the method, then each inner word’s first
letter should be in upper case.
– Example Public void myMethodName()

• 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 and append
.java to the end of the name.
– Example MyFirstJavaClass is the class name. Then the file name should be saved as
MyFirstJavaClass.java
Examples
/* More java statements
* This program computes addition of two numbers
*/

import java.util.Scanner; //instructs interpreter to load scanner class from package util
class AddNumbers
{
public static void main(String args[])
{
int x,y,z;
System.out.println(“enter two integers to calculate their
sum”);
Scanner in = new Scanner(System.in); // new object in
x = in.nextint(); // executes method from in object
y = in.nextint(); // store values return value to variables
z = x+y;
System.out.println(“sum of entered integers = ” +z); // + concatenates two strings
}
}

Output for program


enter two integers to calculate their sum
26
sum of entered integers = 8
Java program structure
• A java program may contain one or more
sections as shown below
• Documentation Section
– Comprises of comment lines giving name of
program, the author and other details, which
programmer likes to refer at later stage. Mainly in
maintaining program.
• Package Statement
– The first statement allowed in a java file is a package
statement.
– Are used in java in order to prevent naming conflicts,
to control access, to make searching/locating and
usage of classes, interfaces, enumerations and
annotations easier, etc.
– This declares a package name and informs the
compiler that the classes defined here belong to this
package.
• Example package student;
– The package statement is optional. That is our classes
do not have to be part of package.
• Import statements
– next statement after a package statement is import statements.
– Example import student.test;
• This statement instructs the interpreter to load the test class contained in the
package student.

• Interface statements
– An interface is like a class but includes a group of methods
declarations.

• Class definition
– Java program may contain multiple class definitions. Classes are
primary and essential elements of a java program. These classes are
used to map the objects of real-world problems.

• Main Methods
– Creates objects of various classes and establishes communication
between them. On reaching end of main the program exits and control
will be transferred to operating system.

You might also like