www.acompworld.com
Core Java Programming
Session-1
www.acompworld.com
This stunning image of the sunset on planet Mars wouldn’t have been possible without Java
NASA used Java as the driving force behind its Mars Rover missions. Computers, mobile phones, set-top boxes, and even digital television sets can all be programmed using the Java programming language. Robots, air traffic control systems and the barcode scanners in your favourite supermarkets are all being programmed in Java.
www.acompworld.com
Topics
• Java Basics• Variables• Data Types• Operators• Conditional Structures
www.acompworld.com
Programming Approaches
• Functional/Procedural Programming• Object Oriented Programming
www.acompworld.com
Procedural Programming
• Program contain a series of computational steps to be carried out
• Procedural programming is not suitable for solving large and complex problem
www.acompworld.com
Object Oriented Programming
• Program is composed of a collection of objects that communicate with each other
• Object oriented programming makes it easier to model real world scenarios and makes it easier to handle fairly large and complex problems
www.acompworld.com
Main Concepts in OOP
• Object• Class• Inheritance• Encapsulation• Polymorphism
www.acompworld.com
Objects
• An object is composed of members and methods
• The members, also called data members, characteristics, attributes, or properties, describe the object
• The methods generally describe the actions associated with a particular object
www.acompworld.com
Object
• For example, a sports car is an object. Some of its members might be its height, weight, acceleration, and speed
• An object’s members just hold data about that object. Some of the methods of the sports car could be "drive", "park", "race", etc.
www.acompworld.com
Class
• Class is a blueprint or template for creating objects
• Just as a single blueprint can be used to build multiple buildings, a class can be used to create multiple copies of an object
• When an object is created by a constructor of the class, the resulting object is called an instance of the class
www.acompworld.com
Inheritance
• Under inheritance, a class can inherit the properties of an existing class.
• Inheritance makes it possible to define a variation of a class without redefining the new class from scratch.
• Shared properties are defined only once, and reused as often as desired.
www.acompworld.com
Encapsulation
• In object oriented programming, the data and related functions are bundled together into an "object".
• Ideally, the data inside an object can only be manipulated by calling the object's functions.
• This means that your data is locked away inside your objects and your functions provide the only means of doing something with that data
www.acompworld.com
Polymorphism
• The word polymorphism literally means a state of having many shapes or the capacity to take on different forms.
• When applied to object oriented programming languages like Java, it describes a language’s ability to process objects of various types and classes through a single, uniform interface.
www.acompworld.com
OOPs Benefits
• Modularity– source code for an object can be written and
maintained independently of the source code for other objects
– easier maintenance and reuse
• Information hiding– other objects can ignore implementation details– security (object has control over its internal state)
• but– shared data need special design patterns – performance overhead
www.acompworld.com
Why Java ?
• Portable• Easy to learn• Designed to be
used on the Internet
www.acompworld.com
What is Java?
• It is an object-oriented language developed by Sun in the mid 1990s– Original language called Oak– Intended for embedded systems
• Unlike C++, it was developed from scratch– The syntax is very similar to C++
• Sun describes it as– "A simple, object-oriented, distributed, interpreted,
robust, secure, architecture neutral, portable, high-performance, multi-threaded and dynamic language."
www.acompworld.com
What is Java? (cont)
• Object-Oriented– Designed to support Object-Oriented concepts– However, does contain non-Object-Oriented
primitive data types
• Distributed– Applications are constructed using objects.
Objects can be distributed in multiple locations within a network environment.
– Extensive integration with TCP/IP
www.acompworld.com
What is Java? (cont)
• Interpreted– Java compiles to byte-code (not machine code).
Byte code is interpreted. – Most Java versions after 1.2 include a JIT (Just-
In-Time) compiler which compiles byte code to machine code.
• Robust– Memory management is done automatically– Use of pointers is limited
www.acompworld.com
What is Java? (cont)
• Secure– All Java code is subject to a security model
• Architecture-Neutral/Portable– Compiled Java (byte code) will run on any
platform which has a Java Virtual Machine– The Java Virtual Machine is available for almost
all platforms... even mainframes
www.acompworld.com
What is Java? (cont)
• High-Performance– Originally, Java's performance was poor– Now, Java's performance rivals C++
• Multi-Threaded– Processes contain multiple threads of execution– Similar to multi-tasking but all threads share the same
memory space
• Dynamic– Makes heavy use of dynamic memory allocation– Classes can be dynamically loaded at any time
www.acompworld.com
Platform Independence. How does Java do it?
• Java has been described as WORA (Write once, Run Anywhere)
• Because Java source code is compiled to byte code and the byte code is interpreted, Java code can be executed anywhere an interpreter is available.
• The "Interpreter" is call the Java Virtual Machine
www.acompworld.com
Traditionally, source code had to be compiled for the target hardware and OS platform:
Source.cpp
i386 binary
SPARC binary
PPC binary
Windows Compiler
SolarisCompiler
MacCompiler
The Java Virtual Machine
www.acompworld.com
The Java Virtual Machine
Java source files (.java) are compiled to Java bytecode (.class)
Bytecode is interpreted on the target platform within a Java Virtual Machine
Source.java
i386 VM
SPARC VM
PPC VM
JavaCompile
r
Java Bytecode
Source.class
www.acompworld.com
The Java Software Development Kit (SDK)
• The Java SDK comes in three versions:– Java ME - Micro Edition (for handheld and portable devices)– Java SE - Standard Edition (PC development)– Java EE - Enterprise Edition (Distributed and Enterprise Computing)
• The SDK is a set of command line tools for developing Java applications:– javac - Java Compiler– java - Java Interpreter (Java VM)– appletviewer - Run applets without a browser– javadoc - automated documentation generator– jdb - Java debugger
• The SDK is NOT an IDE (Integrated Development Environment)– Command line only. No GUI
www.acompworld.com
Integrated Development Environments (IDEs)
• There are many IDEs available. Some are public domain and some are commercial:– Eclipse– Netbeans– Jcreator– IBM Websphere– Oracle JDeveloper
www.acompworld.com
Obtaining the Java SDK
• You can download it from Oracle website. The file size is ≈ 120 MB
• If you only wish to run Java programs, you do not need to install the SDK. Instead, you can install the JRE– Smaller installer file– Less time to download– No compiler or development tools. Just Java
VM and support libraries for specified platform.
www.acompworld.com
Packages
• When you view the Java API Documentation, you'll note that the classes are grouped into logical units called "Packages"
• Because there are so many classes, packages provide a mechanism for classifying classes so that they are easier to learn and use
• Developers can also make use of packages to classify their own classes
www.acompworld.com
While it should be your goal to learn as many packages as you can, there are some packages you will use more than others:Language(general)
GUI
Misc. Utilities and Collections
Input/Output
Networking
java.lang
java.awtjava.awt.eventjavax.swing
java.util
java.io
java.net
Common classes used for all application development
Graphical User Interface, Windowing,Event processing
Helper classes, collections
File and Stream I/O
Sockets, Datagrams
Commonly Used Packages
www.acompworld.com
Java Version History
• Even though Java is not very old, there are several key versions to be aware of:
• JDK Alpha and Beta (1995)• JDK 1.0 (January 23, 1996)• JDK 1.1 (February 19, 1997)• J2SE 1.2 (December 8, 1998)• J2SE 1.3 (May 8, 2000)• J2SE 1.4 (February 6, 2002)• J2SE 5.0 (September 30, 2004)• Java SE 6 (December 11, 2006)• Java SE 7 (July 28, 2011)
www.acompworld.com
HOW TO COMPILE AND EXECUTE A SIMPLE PROGRAM
www.acompworld.com
Java Programs
• Java programs are collections of class definitions
• Edit Java source file (*.java) with any editor, or use one of many IDEs
• Compile source to Java Byte Code• Execute Byte Code
www.acompworld.com
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello World");}
}
HelloWorld.java
• Here is Java's "HelloWorld" implementation:– In the file, HelloWorld.java:
www.acompworld.com
Running HelloWorld
• To compile HelloWorld.java, use the compiler. If successful, it will produce a file called HelloWorld.class in the same directory
> javac HelloWorld.java [compiler output]
• To execute, run the Java VM and include the name of the class which contains the "main" method as the first command line parameter
> java HelloWorld Hello World