The JVM Runtime Environment The byte codes are stored in class files. At runtime, the bytecodes that make up a java software program are loaded, checked, and run in an interpreter. The interpreter has two functions: it executes bytecodes, and makes the appropriate calls to the underlying hardware. The Java runtime environment runs code compiled for a JVM and performs four main tasks: Loads Code - Performed by the class loader Verifies Code - Performed by the bytecode verifier Executes Code - Performed by the runtime interpreter Garbage Collection - De allocates memory not being used Class Loader The class loader loads all classes needed for the execution of a program. The class loader adds security by separating the namespaces for the classes of the local file system from those imported from network sources. Once all the classes have been loaded, the memory layout of the executable file is determined. At this point specific memory addresses are assigned to symbolic references and the lookup table is created. Because memory layout occurs at runtime, the java technology interpreter adds protection against unauthorized access into the restricted areas of code. Java software code passes several tests before actually running on your machine. The JVM puts the code through a bytecode verifier that tests the format of code fragments and checks code fragments for illegal code – code that forges pointers, violates access rights on objects, or attempts to change object type.

4.
History of JavaHistory of Java
Java started out as a research project.
Research began in 1991 as the Green Project at Sun
Microsystems, Inc.
Research efforts birthed a new language, OAK. ( A tree outside
of the window of James Gosling’s office at Sun).
Java is available as jdk and it is an open source s/w.

5.
Contd..Contd..
 It was started in 1991 as a project named “Green”, to
develop a platform-independent language that can be
used to create software to be embedded in various
consumer electronic devices, such as microwave ovens
and remote controls etc.
• Many different types of CPU’s are used as controllers.
• C & C++ ( and most other languages ) are designed to be
compiled for a specific target.
• It is possible to compile a C++ program for any type of CPU, but
requires a full C++ compiler for that CPU.
• Compilers are expensive and time-consuming to create.
.

6.
History of Java (contd…)History of Java (contd…)
Language was created with 5 main goals:
 It should be object oriented.
 A single representation of a program could be executed on
multiple operating systems. (i.e. write once, run anywhere)
 It should fully support network programming.
 It should execute code from remote sources securely.
 It should be easy to use.
 Oak was renamed Java in 1995.
Now Sun Microsystems is a subsidiary of Oracle
Corporation.

12.
Java Development Kit:
It contains one (or more) JRE's along with the various
development tools like the Java source compilers, bundling and
deployment tools, debuggers, development libraries, etc.
Java Virtual Machine:
An abstract machine architecture specified by the Java Virtual
Machine Specification.
It interprets the byte code into the machine code depending
upon the underlying OS and hardware combination. JVM is
platform dependent. (It uses the class libraries, and other
supporting files provided in JRE)
Java TerminologyJava Terminology

16.
The Architecture of the Java Virtual MachineThe Architecture of the Java Virtual Machine

17.
Java Virtual MachineJava Virtual Machine
Class loader subsystem: A mechanism for loading
types (classes and interfaces) given fully qualified
names.
The Java virtual machine organizes the memory it
needs to execute a program into several runtime data
areas.
Each Java virtual machine also has an execution
engine: a mechanism responsible for executing the
instructions contained in the methods of loaded
classes.

19.
When the virtual machine loads a class file, it parses information about
a type from the binary data contained in the class file.
It places this type information into the method area.
As the program runs, the virtual machine places all objects the
program instantiates onto the heap.
As each new thread comes into existence, it gets its own pc register
(program counter) and Java stack.

21.
What is the Difference b/w OO and OB Languages?
In Object based languages inheritance is not supported
so that dynamic polymorphism also not supported.
E.g. VB,VC++.
Is C++ partial OOP?
Yes, C++ is a partial OOP because without using class
also we can able to write the program.
Is Java total OOP or partial OOP?
Java is a total oop language because with out object
orientation we can’t able to write any program.

22.
Java is a pure oop or not ?
By default java is not pure object oriented language.
Java is called as Hybrid language.
Pure oop languages are “small talk”, ”ruby”, “Eiffel”.

27.
Java Features (2)Java Features (2)
Interpreted and High Performance
◦ java compiler generate byte-codes, not native machine
code
◦ the compiled byte-codes are platform-independent
◦ java bytecodes are translated on the fly to machine
readable instructions in runtime (Java Virtual Machine)
 easy to translate directly into native machine code
by using a just-in-time compiler.
Portable
◦ same application runs on all platforms
◦ the sizes of the primitive data types are always the same
◦ the libraries define portable interfaces

29.
Java Features (4)Java Features (4)
Multithreaded
◦ multiple concurrent threads of executions can run
simultaneously
◦ utilizes a sophisticated set of synchronization
primitives (based on monitors and condition
variables paradigm) to achieve this

30.
Java Features (5)Java Features (5)
Dynamic
java is designed to adapt to evolving environment
libraries can freely add new methods and instance
variables without any effect on their clients
interfaces promote flexibility and reusability in code
by specifying a set of methods an object can perform,
but leaves open how these methods should be
implemented
can check the class type in runtime

31.
Java Features (6)Java Features (6)
Distributed
Java is designed for the distributed environment of the
Internet, because it handles TCP/IP protocols.
 Allows objects on two different computers to execute
procedures remotely by using package called Remote
Method Invocation (RMI).
Architecture-Neutral
Goal of java designers is “write once; run anywhere, any
time, forever.”

32.
Byte CodeByte Code
Bytecode is a highly optimized set of instructions
designed to be executed by the Java run-time system,
which is called the Java Virtual Machine (JVM).
The JVM is an interpreter for bytecode.

36.
integer types
 Java does not support unsigned, positive-only integers.
 All are signed, positive and negative values.
Name Width Range
long 64 –9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
int 32 –2,147,483,648 to 2,147,483,647
short 16 –32,768 to 32,767
byte 8 –128 to 127

37.
 byte
The smallest integer type is byte.
Variables of type byte are especially useful while working with
a stream of data from a network or file.
Byte variables are declared by use of the byte keyword.
 Ex:-
byte b, c;

41.
Characters
 char in Java is not the same as char in C or C++.
 In C/C++, char is an integer type that is 8 bits wide.
 Java uses Unicode to represent characters.
 Unicode defines a fully international character set that can represent all
of the characters found in all human languages.
 It is a unification of dozens of character sets, such as Latin, Greek,
Arabic, Cyrillic, Hebrew, Katakana, Hangul, and many more.
 Hence it requires 16 bits.
 The range of a char in java is 0 to 65,536.
 There are no negative chars.

43.
Booleans
 It can have only one of two possible values, true or false.
 This is the type returned by all relational operators, such
as a < b.

44.
Variables
 The variable is the basic unit of storage in a Java program.
 A variable is defined by the combination of an identifier, a type, and an optional
initializer.
Declaring a Variable
 In Java, all variables must be declared before they can be used.
type identifier [ = value][, identifier [= value] ...] ;

45.
The Scope and Lifetime of Variables
Scope
The scope of a declared element is the portion of the program where the element is
visible.
Lifetime
The lifetime of a declared element is the period of time during which it is alive.
 Java allows variables to be declared within any block.
 A block begins with an opening curly brace and ends by a closing curly brace.
 A block defines a scope. Thus, each time you start a new block, you are creating a new scope.

46.
Contd..
 In Java, there are major scopes which are defined by a class and a method.
Scopes defined by a method
 The scope defined by a method begins with its opening curly brace and ends with closing curly brace.
 Scopes can be nested.
 Objects declared in the outer scope will be visible to code within the
inner scope. The reverse is not true.
 Objects declared within the inner scope will not be visible outside it.

48.
Arrays in JavaArrays in Java
An array is a data structure that defines an
ordered collection of a fixed number of
homogeneous data elements
The size of an array is fixed and cannot
increase to accommodate more elements

49.
Reference variables in Java (1/4)Reference variables in Java (1/4)
 Reference variables are used in Java to store the references of the
objects created by the operator new
 Any one of the following syntax can be used to create a reference
to an int array
int x[];
int [] x;
• The reference x can be used for referring to any int array
//Declare a reference to an int array
int [] x;
//Create a new int array and make x refer to it
x = new int[5];

51.
Reference Types in Java (4/4)Reference Types in Java (4/4)
 A reference type cannot be cast to primitive type
 A reference type can be assigned ‘null’ to show that it is not
referring to any object
◦ ‘null’ is a keyword in Java
int [] x = null;

52.
Initializing an array in JavaInitializing an array in Java
 An array can be initialized while it is created as follows
int [] x = {1, 2, 3, 4};
char [] c = {‘a’, ‘b’, ‘c’};

53.
The length of an arrayThe length of an array
 Unlike C, Java checks the boundary of an array while
accessing an element in it
 Java will not allow the programmer to exceed its boundary
 If x is a reference to an array, x.length will give you the
length of the array
 So setting up a for loop as follows is very common in Java
for(int i = 0; i < x.length; ++i){
x[i] = 5;
}

55.
Multidimensional ArraysMultidimensional Arrays
Multidimensional arrays are arrays of
arrays.
To declare a multidimensional array
variable, specify each additional index
using another set of square brackets.
int [][] x;
//x is a reference to an array of int arrays
x = new int[3][4];
//Create 3 new int arrays, each having 4 elements
//x[0] refers to the first int array, x[1] to the second and so on
//x[0][0] is the first element of the first array
//x.length will be 3
//x[0].length, x[1].length and x[2].length will be 4

60.
 The operands of the arithmetic operators must be of a numeric
type.
 You cannot use them on boolean types, but you can use them on
char types.
 No fractional component attached to the result, when the division
operator is applied to an integer type.
 The modulus operator, %, returns the remainder of a division
operation. It can be applied to floating-point types as well as
integer types.

◦ Ex:- int x = 42;
◦ double y = 42.25;
◦ x mod 10 = 2
◦ y mod 10 = 2.25


63.
Relational OperatorsRelational Operators
 The relational operators determine the relationship that one
operand has to the other.
 They determine equality and ordering.
 Operator Result
 == Equal to
 != Not equal to
 > Greater than
 < Less than
 >= Greater than or equal to
 <= Less than or equal to

64.
 Note :-
 int done;
 // ...
 if(!done) ... // Valid in C/C++
 if(done) ... // but not in Java.
 In Java, these statements must be written like this:

 if(done == 0)) ... // This is Java-style.
 if(done != 0) ...
 In C/C++, true is any nonzero value and false is zero.
 In Java, true and false are nonnumeric values which do not relate to zero or
nonzero.

72.
Control StatementsControl Statements Java’s control statements can be put into the following
categories:
 selection
 iteration
jump

73.
JavaJava’’s Selection Statementss Selection Statements
 Java supports two selection statements:
 if ( condition )
 Same as in c/c++.
 The condition is any expression that returns a boolean value.
 Switch( expression )
 Same as in c/c++.
 The expression must be of type byte, short, int, or char;
 Each of the values specified in the case statements must be of a type
compatible with the expression.



78.
 2. continue
 Similar to c/c++
 3. return
 The return statement is used to explicitly return from a method.
 It transfers control back to the caller of the method.

79.
Type Conversion and CastingType Conversion and Castingtype conversion, typecasting, refers to
different ways of, implicitly or explicitly,
changing an entity of one data type into
another
Types of Conversions:
 1.Widening conversion
 2.Narrowing conversion

80.
 The widening conversion is permitted in
the following cases:
 byte to short, int, long, float, or double
 Short to int, long, float, or double
 char to int, long, float, or double
 int to long, float, or double
 long to float or double
 float to double

81.
 When one type of data is assigned to another type of
variable, an
 automatic type conversion will take place if the following two
 conditions are met:
 The two types are compatible.
 The destination type is larger than the source type.
 When these two conditions are met, a widening conversion takes place.
For widening conversions, the numeric types, including integer,
floating-point, and double types, are compatible with each other.
 Ex:
 int a=10;
 double b=a;
However, there are no automatic conversion from the numeric types
to char or boolean
Also, char and boolean are not compatible with each other.

83.
 The narrowing conversion occurs from a type to a different
type that has a smaller size, such as from a long (64 bits) to
an int (32 bits).
 In general, the narrowing primitive conversion can occur in
these cases:
 short to byte or char
 char to byte or short
 int to byte, short, or char
 long to byte, short, or char
 float to byte, short, char, int, or long
 double to byte, short, char, int, long, or float
 The narrowing primitive conversion must be explicit.
 You need to specify the target type in parentheses.

84.
it is still possible to obtain a conversion between incompatible types.
Cast keyword / operator will be used to perform conversion between
incompatible types.
 General form:
 ( target-type ) value;

86.
java program structurejava program structure
• In Java :In Java :
 A program is made up of one or moreA program is made up of one or more classes.classes.
 One class is marked as the special “starting” class.
 Name of the file should coincide with the name of starting
class.
 When a Java program is launched by the interpreter ( JVM ), it
invokes a static method named “main” in the start class.

87.
Sample Program
public class HelloWorld {
public static void main (String args []) {
System.out.println (“Welcome to Java Programming…..”);
}
}
public allows the program to control the visibility of class
members. When a class member is preceded by public, then that
member may be accessed by code outside the class in which it is
declared. In this case, main ( ) must be declared as public, since it
must be called by code outside of its class when the program is
started.

88.
static allows main( ) to be called without having to instantiate a
particular instance of the class. This is necessary since main ( ) is
called by the Java interpreter before any objects are made.
void states that the main method will not return any value.
main() is called when a Java application begins. In order to run
a class, the class must have a main method.
string args[] declares a parameter named args, which is an array of
String. In this case, args receives any command-line arguments
present when the program is executed.

89.
System is a class which is present in java.lang package.
out is a static field present in System class which returns a
PrintStream object. As out is a static field it can call directly with
classname.
println() is a method which present in PrintStream class which
can call through the PrintStream object return by static field out
present in System class to print a line to console.

92.
 The data, or variables, defined within a class are called instance
variables.
 Functions defined within a class are called methods.
 The methods and variables defined within a class are called
members of the class.
• Note:-
• The class declaration and the implementation of the
 methods are stored in the same place and not defined separately.

94.
Constructors and Methods
 A constructor is a special member function whose task is to initialize the objects of its
class.
 A constructor has the same name as the class in which it resides and is syntactically
similar to a method.
 A constructor initializes an object immediately upon creation.
 The constructor is automatically called immediately after the object is created.
 If no constructor in program .System provides its own constructor called as default
constructor.
 Constructors doesn’t have any return type.
 A constructor which accepts parameters is called as parameterized constructor.

96.
MethodsMethods
General Form:
type name(parameter-list) {
// body of method
}
 The type of data returned by a method must be compatible with the return type
specified by the method.
 The variable receiving the value returned by a method must also be compatible
with the return type specified for the method.
return value;
Here, value is the value returned.
Ex:
double volume() {
return w*h*d;
}

100.
private:
 A private member is accessible only to the class in which it is defined.
 Use private keyword to create private members.
protected:
 Allows the class itself, subclasses, and all classes in the same package to access the
members.
 To declare a protected member, use the keyword protected.
Access ControlAccess Control

101.
public:
 Any class, in any package ,has access to a class's public members.
 To declare a public member, use the keyword public.
default :
 When no access specifier is used, then by default the member of a class is public
within its own package, but cannot be accessed outside of its package.

104.
Instance Variable HidingInstance Variable Hiding
 In java, it is illegal to declare two local variables with the same name inside
the same or enclosing scopes.
 But , you can have local variables, including formal parameters to methods,
which overlap with the names of the class’ instance variables.
 When a local variable has the same name as an instance variable, the local
variable hides the instance variable.

109.
Garbage CollectionGarbage Collection
 objects are dynamically allocated by using the new operator,
 In C++, dynamically allocated objects must be manually released by use of a delete
operator.
 Java handles deallocation automatically. This technique is called garbage collection.
Technique
 when no references to an object exist, that object is assumed to be no longer
needed, and the memory occupied by the object can be reclaimed.
 Garbage collection only occurs at regular intervals during the execution of your
program.

111.
The finalize( ) Method
 Sometimes an object will need to perform some action when it is destroyed
Ex:
if an object is holding some non-Java resource such as a file handle or
window character font, then you might want to make sure these resources are freed before
an object is destroyed
 To handle such situations, Java provides a mechanism called finalization.
 The finalize( ) method has this general form:
protected void finalize( )
{
// finalization code here
}
 Here, the keyword protected is a specifier that prevents access to finalize( ) by code
defined outside its class.

117.
Parameter PassingParameter Passing
Call-by-value
The call-by-value copies the value of a actual parameter into the formal parameter of the
method.
In this method, changes made to the formal parameter of the method have no effect on
the actual parameter
Simple types, int, float, char, double, are passed by value

119.
Call-by-reference
 In call-by-reference, a reference to an actual parameter (not the value of the
argument) is passed to the formal parameter.
 In this method, changes made to the formal parameter of the method will effect on the
actual parameter
 Objects are passed by reference

123.
Understanding staticUnderstanding static
 Normally a class member must be accessed only through an object of its
class.
 However, it is possible to create a member that can be accessed by using a
class name.
 static keyword will be used to create such a member
 You can declare both methods and variables to be static.
 The most common example of a static member is main( ).
 main( ) is declared as static because it must be called before any objects
exist.
 When objects of its class are declared, no copy of a static variable is
made.
 Instance variables declared as static are global variables.
 All instances of the class share the same static variable.

124.
Methods declared as static have several
restrictions:
 They can only call other static methods.
 They must only access static data.
 They cannot refer to this or super in any way.
static block
 If you need to do computation in order to initialize your static variables,
you can declare a static block which gets executed when the class is
first loaded.
static block Syntax :
static {
statement1;
statement2;
…….
…….
}

127.
String HandlingString Handling
 in Java a string is a sequence of characters.
 But, unlike many other languages that implement strings as character arrays, Java implements
strings as objects of type String.
 when you create a String object, you are creating a string that cannot be changed.
• That is, once a String object has been created, you cannot change the
characters that comprise that string.
• You can still perform all types of string operations.
• The difference is that each time you need an altered version of an existing
string, a new String object is created that contains the modifications.
• The original string is left unchanged.
• There are 3 types of String handling classes
• String
• StringBuffer
• StringTokenizer
•
•

128.
String Class
 The String class supports several constructors.
 String is an object not an array of characters in java.
 once a String object is created, you cannot change the characters that
are in string.
 Strings created by a StringBuffer class can be modified after they are
created.
 Both the String and StringBuffer classes are defined in java.lang.

136.
Character ExtractionCharacter Extraction
 charAt():
to extract a single character from a string
char charAt(int where)
Ex:
char ch;
ch=“abc”.charAt(1);
 getChars():
 to extract more than one character at a time,
void getChars(int sourceStart,int SourceEnd,char target[],int targetStart)

145.
boolean startsWith(String str) //to determine whether a given String begins
with a specified string.
boolean endsWith(String str)// to determine whether the String in question
ends with a specified string.
Ex: "Football".endsWith("ball") and "Football".startsWith("Foot") are both true.
boolean startsWith(String str, int startIndex) //to specifies the index into the invoking
string at which point the search will begin.
Ex: "Football".startsWith("ball", 4) returns true.
equals( ) Versus ==
// It compares the characters inside a String object
//To compare two object references to see whether they refer to the same instance.
String ComparisonString Comparison

147.
int compareTo(String str)
Value Meaning
Less than zero The invoking string is less than str.
Greater than zero The invoking string is greater than str.
Zero The two strings are equal.
int compareToIgnoreCase(String str)
String ComparisonString Comparison

153.
String objects are immutable,
whenever you want to modify a String, you must either copy it into a StringBuffer or use one of
the following String methods,
String substring(int startIndex)
 Here, startIndex specifies the index at which the substring will begin.
 it returns a copy of the substring that begins at startIndex and runs to the end of the
invoking string.
String substring(int startIndex, int endIndex)
 Here, startIndex specifies the beginning index, and endIndex specifies the stopping
point
 The string returned contains all the characters from the beginning index, up to, but
not including, the ending index.

163.
 int length( )
 //The current length of a StringBuffer can be found via the length( ) method
 int capacity( )
//The total allocated capacity can be found through the capacity( ) method.
 void setLength(int len)
//To set the length of the buffer within a StringBuffer object.

166.
append():
 // concatenates the string representation of any other type of data to the end of the invoking
StringBuffer object
 StringBuffer append(String str)
 StringBuffer append(int num)
 StringBuffer append(Object obj)
 String.valueOf( ) is called for each parameter to obtain its string representation. The result is
appended to the current StringBuffer object.

173.
StringTokenizer
 The StringTokenizer class provides the first step in this parsing process,
often called the lexer (lexical analyzer) or scanner
 Parsing is the division of text into a set of discrete parts, or tokens
 To use StringTokenizer
 you specify an input string and a string that contains delimiters
 Delimiters are characters that separate tokens
 Each character in the delimiters string is considered a valid delimiter
 ex: “ ,;:”
 The default set of delimiters consists of the whitespace characters:
 space, tab, newline,carriage return
token <= identifier | keyword | separator | operator
| literal | comment