Java Notes B.Sc-1-1 PDF
Java Notes B.Sc-1-1 PDF
Java Introduction
1.1 Java History
It was created in 1991 by James Gosling et al. of Sun Microsystems.
Initially called Oak, in honor of the tree outside Gosling's window, its name was changed
to Java because there was already a language called Oak.
The original motivation for Java
- The need for platform independent language that could be embedded in various
consumer electronic products like toasters and refrigerators.
- One of the first projects developed using Java.
- A personal hand-held remote control named Star 7.
- At the same time, the World Wide Web and the Internet were gaining popularity.
Gosling et. al. realized that Java could be used for internet programming.
Class File
The Java class file helps Java suitable for networks mainly in the areas of platform-
independence and network-mobility.
Its role in platform independence is serving as a binary form for java programs that is
expected by the Java virtual machine but independent of underlying host platforms.
The Java class file plays a critical role in Java's architectural support for network-
mobility. First, class files were designed to be compact, so they can more quickly move
across a network. Also, because Java programs are dynamically linked and dynamically
extensible, class files can be downloaded as needed.
Java API
The Java API is set of runtime libraries that give you a standard way to access the system
resources of a host computer.
2. Java Basics
2.1 Java Features
Simple
Object Oriented
Distributed
Interpreted
Robust
Secure
Architectural Neutral
Portable
High Performance
Multithreaded and Dynamic
2.2.1 JDK
Java Development kit(JDK) : JDK is a superset of the JRE, and contains everything that is in the
JRE, plus tools such as the compilers and debuggers necessary for developing applets and
applications. The above illustrates all the component technologies in Java SE platform and how
they fit together.
– applet viewer : Allows you run applet without browser.
– Javac (Compiler): The Java Compiler that use to compile programs written in java
to byte codes.
– Java (interpreter ) : it interprets the byte code
– Javadoc (Doc. Generator) : Generates documentation in HTML Format from java
source code.
– Javap (Disaasembler): Disassembles the byte code generates java code in text
format.
– Jdb (debugger): The debugger helps to fix the bugs in java program.
2.2.2 JRE
Java Runtime Environment (JRE) : The Java Runtime Environment (JRE) provides the libraries,
the Java Virtual Machine, and other components to run applets and applications written in the
Java programming language. In addition, two key deployment technologies are part of the JRE:
Java Plug-in, which enables applets to run in popular browsers; and Java Web Start, which
deploys standalone applications over a network. It is also the foundation for the technologies in
the Java 2 Platform, Enterprise Edition (J2EE) for enterprise software development and
deployment. The JRE does not contain tools and utilities such as compilers or debuggers for
developing applets and applications.
2.3.2 Identifiers
Identifiers can contain any combination of upper case, lower case, numbers and ‘$’, ‘_’.
An identifier must not begin with a number.
2.3.6 Constants
Declaring Array:
– To declare an array, write the data type, followed by a set of square brackets [],
followed by the identifier name.
– e.g.: int ages[] or int []ages;
– Int v[]; //simple array of ints
v = new int[10]; //fixed length of array
Some points of Arrays
To access an array element, or a part of the array, you use a number called an index or a
subscript.
Elements inside your array are from 0 to (sizeOfArray-1).
In order to get the number of elements in an array, you can use the length field of an
array. The length field of an array returns the size of the array.
It can be used by writing, i.e., arrayName.length
Multidimensional arrays are implemented as arrays of arrays. Multidimensional arrays
are declared by appending the appropriate number of bracket pairs after the array name.
// integer array 512 x 128 elements
int [][] twoD = new int[512][128];
// character array 8 x 16 x 24
char [][][] threeD = new char[8][16][24];
1. Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and
double.
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.
The Boolean data type specifies one bit of information, but its "size" can't be
defined precisely.
The byte data type is used to save memory in large arrays where the memory
savings is most required. It saves space because a byte is 4 times smaller than an
integer. It can also be used in place of "int" data type.
The short data type can also be used to save memory just like byte data type. A
short data type is 2 times smaller than an integer.
The int data type is generally used as a default data type for integral values unless
if there is no problem about memory.
Example
byte a = 68;
char a = 'A'
Type Conversion:
For example, we can assign variable of integer data type into a variable of long data type.
Now, this is just a simple example of type conversion. There are other types of
conversion as well in Java.
int i = 10;
long l = i;
}
Output :
The result of a * b exceeds the range of byte. To handle this kind of problem, Java
automatically promotes each byte or short operand to int. a * b is performed using
integers.
3. Operators
Operator in java is a symbol that is used to perform operations. For
example: +, -, *, / etc.
There are many types of operators in java which are given below:
o Unary Operator,
o Arithmetic Operator,
o Shift Operator,
o Relational Operator,
o Bitwise Operator,
o Logical Operator,
o Ternary Operator and
o Assignment Operator.
//Bitwise AND
System.out.println("num1 & num2 = " + (num1 & num2));
//Bitwise OR
System.out.println("num1 | num2 = " + (num1 | num2) );
//Bitwise XOR
System.out.println("num1 ^ num2 = " + (num1 ^ num2) );
}
}
Java has six relational operators that compare two numbers and return a
boolean value. The relational operators are < , > , <= , >= , == , and != .
System.out.println("a == b = " + (a == b) );
System.out.println("a != b = " + (a != b) );
System.out.println("a > b = " + (a > b) );
System.out.println("a < b = " + (a < b) );
System.out.println("b >= a = " + (b >= a) );
System.out.println("b <= a = " + (b <= a) );
}
}
Operator Precedence