Java Notes
Java Notes
Java was been developed by James Gosling and his colleagues in 1991, initially was called as
oak , in 1995 it was renamed as Java.
Few shortcuts related to java.
JDK - Java Development Kit.
JVM - Java Virtual Machine.
J2SE - Java 2 Standard Edition.
J2EE - Java 2 Enterprise Edition.
Java API – Java Application Programming Interface.
WORA - Write Once Run Anywhere.
IDE- Integrated Development Environment.
Features of java:
Object oriented programming: This model organizes the program around its data, hence
securing data. Everything in Java is in the form of classes and objects.
Platform independent: Platform for any program is mainly the Operating System and the
Processor used on the system where it has to be processed. Program once compiled on one
platform may not work on another platform.
But java makes use of both compiler and interpreter, hence it is platform independent.
Java compiler instead of converting the source code(program) to object code(machine lang)
produces an intermediate code called as byte code.
Once byte code is formed it can be executed on any platform with the help of JVM.
Byte Object
Source code
Java Compiler (javac) code JVM
code
Portable: Since java is platform independent once compiled it can be executed on any machine
hence also called as WORA (write once run anywhere)
Multithreaded: One program can be divided into multi threads(tasks) and can be run
simultaneously.
Robust: Java is Robust(reliable) language. As Java has the feature to handle both runtime error
and compile time error, the programs become more efficient and reliable.
Architectural Neutral: Java is architecture neutral because there are no implementation
dependent features.
Distributed: Java is distributed because it facilitates users to create distributed applications
(Parts of the application are shared among systems connected to the network) in Java. RMI
(Remote Method Invocation)is used for creating distributed applications. This feature of Java
makes us able to access files by calling the methods from any machine on the internet.
Garbage collection: In Java, garbage collection is the process of managing memory,
automatically. It finds the unused objects (that are no longer used by the program) and delete or
remove them to free up the memory.
JVM: Java Virtual Machine is an interface(translator) to convert byte code to object code. JVM
and java API together make the platform for java program. JVM on any machine acts as a virtual
processor hence the name.
Java API:API consists of packages which in turn consists of built in classes and functions that
programmers use in their program.
Java is often referred to as platform as they run on a virtual machine(JVM) provided by java,
instead of interacting with platform of system directly.
What is IDE ?
IDE stands for Integrated Development Environment which includes
* Text editor--- to type the program.
* A debugger—to debug the errors
* A viewer--- to view the output (terminal window)
Examples of IDE: BlueJ, Jcreator etc…
What is JDK?
Java Development Kit is a collection of tools that allow us to compile run and debug programs.
BlueJ is an example for IDE it provides an easy- to –use teaching environment for the java
language.
Object: It is a software bundle which consists of its own state(data members) and behaviour
( member function).
Class: Class is a prototype or blueprint which defines what an object consists of i.e., what data
members and methods an object or set of objects of similar type should consists of.
Class is also called as object factory as once a class is defined any number of objects can be
created.
Ex: doll prepared using POP (Plaster Of Paris) is an example for an object. Here the size, shape,
colour makes the state of the object. It moves when it is moved, it breaks into pieces when
thrown down, are some of its behaviour. But to prepare such doll we require a moulder which
specify the size and shape for the doll. Now the moulder is an example for class. Moulder itself
is not an object but can be used to create many objects.
State of object: All the data members of an object together make the state.
Behaviour: All the methods of an object make the behaviour of an object.
State: Data members → attribute → values
Behavior → set of functions → methods
Object is an instance of a class, as once a class is defined you can create any number of object
within instance using new keyword.
Message: Objects interact each other through messages. Methods of one object make a call of
another method which may present in the same object or another object, this is called as
messaging. Some times message can contain parameters or values such messages are called as
data messages.
Class variables: data members of a class if defined as static then they are called as class
variables these variables will be common to all objects of the class.
Class methods: methods defined as static are called as class methods. Common to all objects,
none of the objects of this class contains this method. To call this function creation of object is
not required.
Instance variable: non static data members are called as instance variable. Even though they
are declared in a class, they are created(stored in computer memory) when an object is created.
They can be accessed using object name.
Instance method: non static methods can be called only when an object is created. To call these
functions we have to mark use of the object name and the dot separator.
Abstraction: showing of only what user needs and hiding of all the complexity of working of an
object is known as abstraction.
Encapsulation: wrapping up of code and data together is known as encapsulation. By doing so,
some data is secured as the data can be accessed only through the code of the object. This leads
to information hiding. The source code for an object can be written and maintained
independently from other objects hence making it modular.
Inheritance: process of accessing the properties of one object into another object is called as
inheritance. In java property of one class can be inherited by other object using “extends”
keyword.
class A
{
int a;
}
class B extends A
{
int B;
}
Now objects of class B can have both A and B variables.
Polymorphism: When two or more objects responds to the same message differently with
respect to the values given, this process is known as polymorphism. Function overloading is one
form of polymorphism.
• Should not be a keyword, but keyword can be a part of it.eg: else(invalid), someone
else(valid).
• Case sensitive: once used in some case should be used in the same case when ever used.
• No special characters are used except for ‘$’ and ‘_‘ characters.
• Numbers can be a part of variable name but should always start with an alphabet or ‘$’ or
‘_‘.
• No space should be left in between.
Constants or literals:
Are the data values which does not change during the execution of a program.
Integer constant: ‘,’ , ’*’ , ‘a’ , ‘1’ , ‘\n’.
String constant: “this” , “ hello”, “***” ,” ???”.
Character set: Java uses the Unicode character set. Unicode stands for Universal Code, is a 2
byte character set which consists of most human written characters including English, Arabic,
Chinese, etc. The first 128 characters are similar to ASCII (American Standard Code for
Information Interchange) character set.
Escape Sequence: Non graphic characters that cannot be typed directly from keyboard such as
new line characters, backspace can be represented by using escape sequences. An escape
sequence is always preceded by ‘\’ followed by one or more characters.
Eg: ‘\b’ – backspace , ‘\n’ - new line, ‘\t’ – tab space, ‘\\’ – backslash, ‘ \’ ’ – single quote, ‘ \” ‘ –
double quotes.
Variables: Variables are the memory location reserved to store some values. These values can
be changed during the execution of the program.
Separators: Separators are also called as Punctuators. They are used to separate two tokens. -
, --- used to separate two variables while declaration.
. --- used to separate object or class name from its members.
; --- used to separate two statements.
( ) --- used to separate function name from its parameters.
[ ] --- used to separate array name from its subscript (index).
{ } --- used to separate block of statements, group array elements.
Operators: are the symbols used to perform some specified action on the operands. Depending
on number of operands used, operators can be classified as:
1. Unary : ++ --- increment, - - --- decrement, - --- negates the value.
2. Binary: +, -, *, /,>,&& are few examples of binary.
3. Ternary: ? : --- is a ternary operator.
Unary --- ++ used to increment the operand by one. Can be used as prefix (++a) or postfix (a++)
notation.
-- used to decrement the operand by one.
Prefix notation: When an operator is placed before the operand.
== it is equal to
!= not equal to
&& ---- returns true only when both the operands are true.
|| ---- logical or ----returns true when any one of the operand is true.
! ---- logical not--- returns true when operand is false.
& ---- similar to logical AND but checks or valuates both the conditions.
| ---- similar to logical OR but evaluates both the condition.
^(XOR) ---- exclusive OR returns true when any one of the operand is true but returns
false when both the operands are true or when both are false.
Assignment operator:
= is an assignment operator.
Mantissa and exponent part of a double value. The base part is called mantissa and exponent part
is called exponent.
Eg: 3.4*1038 3.4 is mantissa and 38 is exponent part.
Type conversion: the process of converting a value from one data type to another data type is
called type conversion.
Precedence of Operators: The order in which the operators are evaluated in an expression is
called as precedence of operators.
Associativity: When 2 or more operators have the same precedence in an expression, then
associativity specify the direction (left to right or right to left) in which the operators are
evaluated.
Operator Associtivity
() Parenthesis Left to right
[]
++, -- Unary operators Left to right
*, /, % Multiplication, division, modulus Left to right
+, - Addition, subtraction Left to right
<, <= Less than, less than equals Left to right
>, >= Greater than, greater than equals Left to right
==, != Equals, not equals Left to right
&& Logical And Left to right
|| Logical Or Left to right
?: Ternary Operator Right to left
= Assignment Right to left
+=, -=, *=, /+, %= Compound Assignment or Right to Left
Shorthand assignment
Types of expression:
Pure expression: where all the operands of the expression are of same type.
Ex: int a=5, b=10;
System. out.println(a/b);
Here as both the operands of integer type, the return type will also be integer, hence the result is
0.
Mixed expression( Impure Expression): where the operands are of different dat type. Return
value will be of greater data type. Ex: int a=5, float b=100;
System.out.println(a/b); the result will be 0.5.
2. Unicode : is 16 bit character set can store upto 65,536 characters including ascii values and
other characters like Arabic, Chinese, hindi etc…
Data type : before declaring any variables we have to specify what type of value we are going
to store in it. This is called as data type.
Primitive data type: basic data type provided by java are called primitive data type.
Reference data type: the user defined types or composite type are called as reference data
type. These type are constructed from primitive dat types. A reference datatype is used to store
the memory addresses of a class or an array. The variables storing memory addresses are known
as reference data type.
Types of Errors:
Syntax Errors: The errors that take place due to the violation of the syntax is called as syntax
errors. These errors can be rectified during the compilation of the program.
Ex: Semicolon missing, Unclosed String literal, etc.
Run Time Errors: Run time errors are also called as Exceptions. These errors occur during the
execution of the program.
Ex: ArrayIndexOutofBoundException, IndexOutofBoundException, NumberFormatException,
ArithmeticException, InputMismatchException, etc.
Logical Errors: The errors that cannot be rectified either during the compilation time or during
the execution are called as logical errors. Even though Java doesn’t display any error, but still
the program produces the wrong output due to mistake in the logic.
Ex: Area_Triangle=1/2.0*b*b; in this statement, instead of using base * height, we have used
base*base, so even though there is no error in this statement, still the output is wrong.
switch block:
Syntax:
switch(variable/expression)
{
case constant1: Statements; break;
case constant2: Statements; break;
:
:
case constantn: Statements; break;
default: Statements; break;
}
break: break keyword is used to terminate the switch block.
default: when none of the case matches default statement is executed.
case: case keyword checks whether the control variable matches with the constant value given
after the case, if it matches the statements after the case are executed.
Iterations
Group of statements executed for number of times is called as iteration.
3 Iteration statements supported by Java are : while(), do..while() and for loop.
Difference between while() and do while()
while do while
While is an entry controlled loop Do while is an exit controlled loop
Condition is checked before the body of the Condition is checked at the end of the body of
loop the loop
Loop is not executed even once if the condition Loop is executed atleast once even if the
is false for the first time condition is false for the first time.
infinite loop: The loop which executes for infinite times is called infinite loop.
Empty loop : The loop which repeats for certain number of times but doesn’t contain any
statement in the loop. It is also called as delay loop.