You are on page 1of 18

Java Session 01 Document

___________________________________________________________________________________________________
Copyrights Reserved Page 1 of 18
Topics Covered

Introduction to Java
Data Types Variables
Array Operators
Control Statements and
Introduction to Classes

___________________________________________________________________________________________________
Copyrights Reserved Page 2 of 18
Introduction to Java
Java programming language is being used for programming web applications. It is
also widely used for mobile and electronic items. Java is a high-level object-oriented
programming language developed by the Sun Microsystems. Though it is associated with
the World Wide Web but it is older than the origin of Web.

History of Java :
In the early 90s, extending the power of network computing to the activities of
everyday life was a radical vision. In 1991, a small group of Sun engineers called the "Green
Team" believed that the next wave in computing was the union of digital consumer devices
and computers. Led by James Gosling, the team worked around the clock and created the
programming language that would revolutionize our world – Java.
The Green Team demonstrated their new language with an interactive, handheld
home-entertainment controller that was originally targeted at the digital cable television
industry. Unfortunately, the concept was much too advanced for the them at the time. But it
was just right for the Internet, which was just starting to take off. In 1995, the team
announced that the Netscape Navigator Internet browser would incorporate Java technology.
Today, Java not only permeates the Internet, but also is the invisible force behind many
of the applications and devises that power our day-to-day lives. From mobile phones to
handheld devises, games and navigation systems to e-business solutions, Java is everywhere!

Comparing C, C++ and Java:


Features C C++ JAVA

Paradigms OOP, Procedural Procedural, OOP, Generic


Generic Programming Programming (from
Java 5)

Form of Compiled Executable Native Code Executable Native Java bytecode


Source Code Code

Memory Manual Manual Managed, using a


management garbage collector

Pointers Yes, very commonly Yes, very commonly No pointers;


used. used, but some form references are used
of references available instead.
too.

Preprocessor Yes Yes No

String Type Character arrays Character arrays, Objects


objects

___________________________________________________________________________________________________
Copyrights Reserved Page 3 of 18
Complex Data Structures, unions Structures, unions, Classes
Types classes

Inheritance N/A Multiple class Single class


inheritance inheritance, multiple
interface
implementation

Operator N/A Yes No


Overloading

Automatic Yes, with warnings if Yes, with warnings if Not at all if loss
coercions loss could occur loss could occur could occur; must
cast explicitly

Variadic Yes Yes No


Parameters

Goto Statement Yes Yes No

Basics of Java Technology :


This section provides an overview of Java technology as programming language and a
platform. Java technology is a simple, secure, robust, complete object oriented and platform
independent high level programming language. It is also portable, high performance, multi
threaded and networksavy that enable it in constructing software that can run along in small
machines. The whole technology is based on the concept of Java Virtual Machine (JVM) that
acts as a translators of byte code into machine language. In other words JVM converts the
java byte codes into platform specific machine language.

Understanding the Java Technology :


Java technology is high-level, object-oriented, very robust programming language.
Java is platform independent programming language and you can run your compiled code
on any operating system without recompiling your source code. Java technology is based on
the concept of a single Java virtual machine (JVM) -- a translator between the language and
the underlying software and hardware. All implementations of the programming language
must emulate the JVM, enabling Java programs to run on any system that has a version of the
JVM.

Features of Java :
 Object-oriented
 Simpler language
___________________________________________________________________________________________________
Copyrights Reserved Page 4 of 18
Compared to earlier OO languages like C++, it is simple
Designed considering the pitfalls of earlier languages
 Robust
 Architecture Neutral / Portable :
Java code compiled on Windows can be run on Unix without recompilation
 Secure :
Built -in security features like absence of pointers and confinement of the java program
within its runtime environment.
 Support for Multi-threading at language level
 Designed to handle Distributed applications

Different Editions and Releases of Java Technology :


Releases Event Month Year

Java 1.5.0_09 October 2006

Java 1.5.0_08 August 2006

Java 1.5.0_06 December 2005

Java 1.5.0_05 October 2005

Java 1_5_0_04 July 2005

Java 1_5_0_01 February 2005

Java 2 Platform, Standard Edition 1.4


February 2002
(J2SE 1.4)

Java 2 Platform, Standard Edition 1.3


May 2000
(J2SE 1.3)

Java 2 Platform, Enterprise Edition


December 1999
(J2EE)

Java 2 Platform, Standard Edition (J2SE) August 1999

Java Development Kit 1.2 (JDK 1.2) December 1998

Java Development Kit 1.1 (JDK 1.1) February 1997

Java Development Kit 1.0 (JDK 1.0) January 1996

___________________________________________________________________________________________________
Copyrights Reserved Page 5 of 18
a) Java SE :
Java SE or Java Standard Edition provides tools and API's that you can use to create
server applications, desktop applications, and even applets. These programs developed using
Java SE can be run on almost every popular operating system, including Linux, Macintosh,
Solaris, and Windows.
b) JEE :
Based on the foundation framework of the standard edition, Java Enterprise Edition
helps in web application service, component model and enterprise class service oriented
architecture (SOA).
c) JME :
Java Micro Edition or JME for short is an accumulation of Java APIs that are used for
the development of software for devices like mobile phones, PDAs, TV set-top boxes, game
programming. The platform of micro edition generally consists of an easy user interface, a
robust security model and a wide variety of built-in networks for running Java based
application.

New Features in JDK 5 :


Here are the list of new Features added to the JDK 5.
1. Meta data 11. JVM Improvements
2. Auto-boxing and auto-unboxing of 12. Enhanced for Loop
primitive type variables 13. RMI compiler-rmic
3. Var-args 14. Class-data sharing
4. Formatted Output using The print f 15. Monitoring
Method
16. JVM Profiling API (JVMTI)
5. Formatted Input
17. Improved diagnostic
6. Concurrency utilities capability
7. Scanners 18. Desktop Client
8. Static Imports 19. Core XML support
9. Enumerated Types 20. Supplementary character
10. Generics support
21. JDBC row sets

New Features in SE 6 :
Here are the list of new Features added to the JDK 6.
1. Changes in I/O 10. Enhancements in java.lang.Class and
___________________________________________________________________________________________________
Copyrights Reserved Page 6 of 18
2. Collections Framework Enhancement java.lang.reflect
3. Changes in jar and zip 11. Enhancement in RMI for JDKTM 6
4. Java Web Start enhancements in 12. JAVA SE 6 Security Enhancements
version 6 13. Serialization Changes and
5. JMX API Enhancements Enhancements in JAVA SE
Development Kit 6
6. Java Platform Debugger Architecture
Enhancements 14. JavaTM Virtual Machine Technology
7. Java SE 6 Monitoring and 15. Scripting for the Java Platform
Management Enhancements 16. Leveraging Security in the Native
8. New Package java.util.spi in JDK 6 Platform Using Java SE 6 Technology
9. Networking features and 17. JAX-Web Services 2.0 With the Java
enhancements in Java SE version 6.0 SE 6 Platform

Java Tool :
To commence with Java programming, significance of Java Compiler.

Java Compiler :

Java Interpreter :

___________________________________________________________________________________________________
Copyrights Reserved Page 7 of 18
Java Debugger :
Java debugger helps in finding and the fixing of bugs in Java language programs. The
Java debugger is denoted as jdb. It works like a command-line debugger for Java classes.
Java (Header File Generator) :
In Java programming we need to implement some native methods. You must be
wondering about what's native methods.
Firstly, The native methods are in pure C code, not C++. The function prototypes are in
an object-oriented form of C which are being provided by java , but they are still not object
methods. Secondly, We can call native methods applications only. However due to some
security reasons, we cannot call applets from native methods. Thirdly, native methods are
platform-specific. This is the most important point to remember, you have to build a
dynamically loadable library to link your java application with the native operating system
(Windows OS, Machintosh, Linux, Unix ..). For each native platform your application targets,
a dynamically loadable library is needed to be shipped.. That means any system-specific code
has to be ported along with the java code.
Javadoc (Documentation):
This tool is used to generate API documentation into HTML format from Java source
code. It is interesting to know that Javadoc is the industry standard for documenting Java
classes.
Compile and Run a Java Program :
This tutorial will take you step by step through the process of writing a java program,
compiling and running it. The best way to learn is to compile and run these programs
yourself. Comments such as /* this is a comment */ or // this is another comment are
inserted to explain what does the line of code do. The programs are kept simple for the
purpose of concentrating on the main idea in question.

Type the following program and save it in a file with the name "First.java"
___________________________________________________________________________________________________
Copyrights Reserved Page 8 of 18
/*
This is my first java program
*/
// This is the class definition, First is the name of the class
class First{
// this program will begin with a call to main() defined as follows
public static void main(String args[]){
System.out.println("My first java program");
}
}
Compile the First program as follows
c:\jdk\application>javac First.java

The java compiler creates a file called First.class in the directory application. This
class contains the program but in bytecode form ready to run.
Run the program as follows
c:\jdk\application>java First
The output of this program is : My first java program
NOTE :
1-java is case sensitive, first is different from First.
2-By convention the name of the file should match the name of the class.

Object Oriented Programming :


Object :
An object is a software bundle of related state and behavior. Software objects are often
used to model the real-world objects that you find in everyday life. This lesson explains how
state and behavior are represented within an object, introduces the concept of data
encapsulation, and explains the benefits of designing your software in this manner.
Class :
A class is a blueprint or prototype from which objects are created. This section defines
a class that models the state and behavior of a real-world object. It intentionally focuses on
the basics, showing how even a simple class can cleanly model state and behavior.
Inner classes :
An inner class is a class that is declared within another class. Scoping in this case is
similar to variables declared within a class.
Inheritance :
Inheritance provides a powerful and natural mechanism for organizing and
structuring your software. This section explains how classes inherit state and behavior from
their superclasses, and explains how to derive one class from another using the simple syntax
___________________________________________________________________________________________________
Copyrights Reserved Page 9 of 18
provided by the Java programming language.
Interface :
An interface is a contract between a class and the outside world. When a class implements an
interface, it promises to provide the behavior published by that interface. This section defines
a simple interface and explains the necessary changes for any class that implements it.
Packages :
Java organizes code pieces into packages. Packages are primarily an organizational
tool, but certain scoping constraints are impacted by them (that is, things in one package may
be usable by other things in that package but not outside it). In the 'import' statement above,
java.util is a package, which contains the object 'Vector'.
In your own work, a package is generally formed by having all the source files for all
the objects in the same directory, and having a statement at the beginning of the source file
like:
package myPackage;
Import :
Like many programming languages, Java programs are built in pieces that are
generally stored in separate files. A given file, if it refers to pieces that are stored in other files,
must be told where to find those other pieces. For example, Java provides a programming
object called a Vector, which is used to store data in a structured way. A piece of
programming code that makes use of the Vector object must contain a line like:
import java.util.Vector;
This line is placed before the class definition begins.
Access Modifiers :

Keyword Applicable To Who can Access

Private Data members and methods All members within the same
Class only

(No keyword, usually we call Data members, methods, All classes in the same
it default) classes and interfaces package
Protected Data members and methods All classes in the same
package as well as all sub
classes ie even sub classes
residing in a

Public Data members, methods, Any class


classes and interfaces

___________________________________________________________________________________________________
Copyrights Reserved Page 10 of 18
The 'main' method :
Java objects are used in one of two ways. The most common way is as a programming
component that is instantiated by another program. However, some objects are endowed
with the ability to be run as programs (it has to start running somewhere!). This is achieved
by putting a 'main' method in the object. Thus:
// Hello World
class HelloWorld {
private static void main (String[] args){
System.out.println("Hello, world!");
}
}
will result in a Java program that can be run and will print out 'Hello, world!'.

Build path :
When the Java compiler compiles the program from the source code as you've typed it
into code that can be run using a Java capable machine it needs to know where all of the
requisite pieces are. This is typically done in one of several ways. One way is that the
necessary files can all be placed in the same directory as the files you're writing, but this is a
difficult way to manage projects. As a second method, the compiler, when run from a
command line, allows a command line option to be entered that defines the "classpath",
which lists all directories that should be 'checked' for pieces needed by your code. A third
method allows a system variable to be defined that stores the 'classpath' permanently. A
fourth method is perhaps the simplest: the development environment can be configured to
examine a given build path. This is of special importance because libraries of pre-made Java
objects are often distributed and can be stored once and used in many different Java projects
simply by setting the build path.

API Specifications :
Java classes are documented using an API specification; API stands for Application
Programming Interface, and it provides the documentation necessary to make use of the
pieces that a collection of Java code provides. Of greatest importance, the Java language itself,
aside from core elements like syntax and data types, is described by the API that documents
all of the Java objects (like Vector, mentioned above) that are available in the core Java
environment.
Data Types :

___________________________________________________________________________________________________
Copyrights Reserved Page 11 of 18
Type Range

byte 8-bit signed two's complement integer (-27 to 27 - 1, inclusive)

short 16-bit signed two's complement integer (-215 to 215 - 1, inclusive)

int 32-bit signed two's complement integer (-231 to 231 - 1, inclusive)

long 64-bit signed two's complement integer (-263 to 263 - 1, inclusive)

char 1 6-bit unsigned Unicode character (0 to 216 - 1, inclusive)

float 32-bit IEEE 754 single-precision float

double 64-bit IEEE 754 double-precision float

returnAddress address of an opcode within the same method

reference reference to an object on the heap, or null

Operators :
The following are the operators supported by the Java programming language.
Simple Assignment Operator :
= Simple assignment operator

___________________________________________________________________________________________________
Copyrights Reserved Page 12 of 18
Arithmetic Operators :
+ Additive operator (also used for String concatenation)
- Subtraction operator
* Multiplication operator
/ Division operator
% Remainder operator
Unary Operators :
+ Unary plus operator; indicates positive value (numbers are positive
without this, however)
- Unary minus operator; negates an expression
++ Increment operator; increments a value by 1
-- Decrement operator; decrements a value by 1
! Logical compliment operator; inverts the value of a boolean
Equality and Relational Operators :
== Equal to
!= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
Conditional Operators :
&& Conditional-AND
|| Conditional-OR
?: Ternary (shorthand for if-then-else statement)
Type Comparison Operator :
instanceof Compares an object to a specified type
Bitwise and Bit Shift Operators :
~ Unary bitwise complement
<< Signed left shift
>> Signed right shift
>>> Unsigned right shift
& Bitwise AND
^ Bitwise exclusive OR
___________________________________________________________________________________________________
Copyrights Reserved Page 13 of 18
| Bitwise inclusive OR

Control Flow Statements :


The if-then statement is the most basic of all the control flow statements. It tells your
program to execute a certain section of code only if a particular test evaluates to true. The if-
then-else statement provides a secondary path of execution when an "if" clause evaluates to
false. Unlike if-then and if-then-else, the switch statement allows for any number of possible
execution paths. The while and do-while statements continually execute a block of statements
while a particular condition is true. The difference between do-while and while is that do-
while evaluates its expression at the bottom of the loop instead of the top. Therefore, the
statements within the do block are always executed at least once. The for statement provides
a compact way to iterate over a range of values. It has two forms, one of which was designed
for looping through collections and arrays.

Arrays :
An array is a container object that holds a fixed number of values of a single type. The
length of an array is established when the array is created. After creation, its length is fixed.
You've seen an example of arrays already, in the main method of the "Hello World!"
application. This section discusses arrays in greater detail.

An array of ten elements


Each item in an array is called an element, and each element is accessed by its
numerical index. As shown in the above illustration, numbering begins with 0. The 9th
element, for example, would therefore be accessed at index 8.

Summary of Introduction:
History of Java
Comparing C, C++ and Java:
Basics of Java Technology
Different Editions and Releases of Java Technology
Java Tool
Compile and Run a Java Program
Command Line Arguments
Polymorphism
Inheritance
___________________________________________________________________________________________________
Copyrights Reserved Page 14 of 18
Packages
Access specifiers
Jar Files
Data Types

SESSION : I

1. What is Oops?
2. What is Java?
3. How many types of comments are used in Java?
4. What is a Keywords, Data types, Control Statements, Operators in java ?
5. What are the primitives data types and wrapper classes?
6. What is the argument type of a program's main() method And what is its return type?
Step1:
class HelloWorld {

private static void main (String[] args){


System.out.println("Hello, world!");
}
}
What would be the results of compiling and running the following class.
Step2:
class test {
public static void main() {
System.out.println("test");
}
}
A. The program does not compile as there is no main method defined.
B. The program compiles and runs generating an output of "test"
C. The program compiles and runs but does not generate any output.
D. The program compiles but does not run.
7. What is the operator precedence and what will be the output of this expression "4 + 5 *
6 / 3" ?
8. What gets displayed on the screen when the following program is compiled and run.
public class test {
___________________________________________________________________________________________________
Copyrights Reserved Page 15 of 18
public static void main(String args[]) {
boolean x = true;
int a;
if(x)
a = x ? 1: 2;
System.out.println(a);
}
}
A. 1
B. 2
C. 3
D. 4
9. what is the output when the following code is compiled and run.
public class test {
public static void main(String args[]) {
outer : for(int i = 0; i < 3; i++) {
Inner : for(int j = 3; j >= 0; j--) {
if(i == j) break outer;
System.out.println(i + " " + j);
}
}
}
}
10. What all gets printed when the following gets compiled and run.
public class example {
public static void main(String args[]) {
int x = 0;
if(x > 0) x = 1;
switch(x) {
case 1: System.out.println(1);
case 0: System.out.println(0);
case 2: System.out.println(2);
break;
___________________________________________________________________________________________________
Copyrights Reserved Page 16 of 18
case 3: System.out.println(3);
default: System.out.println(4);
break;
}
}
}
11. What gets printed when the following program is compiled and run?
class test {
public static void main(String args[]) {
int i;
do {
i++;
}
while(i < 0);
System.out.println(i);
}
}
12. What gets displayed on the screen when the following program is compiled and run?
import java.util.Vector;
public class GarbageCollector{
public static void main(String[] args) {
int SIZE = 200;
StringBuffer s;
for (int i = 0; i < SIZE; i++)
{
}
System.out.println("Garbage Collection started explicitly.");
long time = System.currentTimeMillis();
System.gc();
System.out.println("It took " + (System.currentTimeMillis()-time) + " ms");
}
}

___________________________________________________________________________________________________
Copyrights Reserved Page 17 of 18
Reference Book

Book : The Complete Reference 7th Edition


Author : Herbert Schildt
Topics: Java Language, Overview of java,Data types, Arrays,
Variables, Operators, Control Statements,
Introduction to class.

___________________________________________________________________________________________________
Copyrights Reserved Page 18 of 18

You might also like