Nirmal Jha Notes
Nirmal Jha Notes
In addition to 8 primitive data types Java provide special support for character String
through the Java.lang.String class.
Literals : are the constants which remains constant within the program. Types of literals
are as follows :
o Integer literals : contain integer numbers , which can be either positive or
negative.
o Floating literals : contain real numbers , which can be either positive or negative.
o Character literals : contain only one character and must be enclosed within
single quote.
o String literals : contain one or more than one characters and must be enclosed
within double quotes.
o Boolean literal : contains only true or false.
ii. Explicit conversion or type casting or Narrowing : conversion from one type to
another type using type cast operator ( ) in a a java program is done using
programmer’s interventions.
Java Operator Precedence Table( Higher to lower)
Operator Type
() Parentheses
[] Array subscript
· Member selection
++ Unary post-increment
-- Unary post-decrement
! Unary logical negation
== Relational is equal to
!= Relational is not equal to
&& Logical AND
|| Logical OR
?: Ternary conditional
Assignment
=
Addition assignment
+=
Subtraction assignment
-=
*=
Multiplication assignment
/=
Division assignment
%=
Modulus assignment
Variable : is the name of the memory location that stores some value.
Java byte code : a compiler Javac , transform the java language source code to byte code that
runs in JVM.
Escape sequence characters : the characters interpreted with a backslash are called escape
sequence or escape characters. They are as follows :
\\ : backlash
\b : backspace
\r : carriage return
\n : new line
\” : double quote
\t : tabbing
Types of loop :
Entry control loop : in these loop constructs the test expression is evaluated before each
iteration. E.g. : for and while loop.
Exit control loop : in these loop construct the test expression is evaluated after each
iteration. E.g. : do-while loop.
Definite loop : a loop which performs up to given limit.
Indefinite loop : it is a loop which performs as long as user wants.
while loop : is the fundamental looping statement. It repeats a statement or block of statement
while the expression that controls it, is true.
for loop : it is a loop which has pre-determined number of iterations. It provides a step by step
way of performing repeated actions.
do-while loop : it is a loop that tests the condition at the end of the loop rather at the beginning.
while vs do-while loop :
while do-while
1. It checks for the condition first and then It executes block of statements once and then
executes the block of statement checks for the condition.
2. It is entry control loop. It is exit control loop.
3. It does not execute if condition is not it executes at least once even if the condition
true. is false.
Similarity between while and do-while loop : in the both the loop number of iterations are not
known in beforehand.
for Vs while :
for while
This loop is used when the number of This loop is used when the number of
iteration is known beforehand iteration is not known beforehand.
Similarity between for and while loop :Both are the entry control loop
Significance of (,) operator in for loop : using (,) operator wecan include more than one
statement in the initialization and updation part of for loop.
Jumping statement : are used to transfer control to another part of the program. Following are
the jumping statements :
break : is used to terminate a loop or a statement sequence in switch statement .
continue : transfer the control back to the loop for the next iteration.
return : transfers the control back to the calling module.
break label : using break label, control is being transferred to the statement after the labeled
statement .
continue label : using continue label , control is transferred to the beginning of the loop
identified by the label.
If statement : tells the compiler to take one of two alternative courses of action depending on
whether the value of a given boolean valued expression is true or false. It is a branching or
decision making statement.
Scope of a variable : defines the section source code from where the variable can be accessed.
Lifetime of a variable : determines how long the variable continues to exist before it is
destroyed.
Types of variables :
Scope Lifetime
Local variable It is a variable It is limited to the Until the uses of the
declared and used method or block variable is over.
within single block or
method
Instance Variables are defined It’s scope is the This lasts as long as
variable(fields) within a class but whole of the class their object lasts.
outside all the blocks definition i.e. it can
are called instance be accessed from
variable anywhere in the same
class
Actual variable The variable given in Limited to the Once the uses of the
function call known method only in which variable is over , this
as actual variable. it is defined. also gets destroyed.
Formal variable Variable given in Limited to the Once the uses of the
function declaration method only in which variable is over , this
or prototype known it is defined. also gets destroyed.
as formal variable.
Class variable A variable having
static modifier known
as a class variable. It
is directly accessible
by the class.
Compound statement : multiple statements placed within the curly braces form a compound
statement.
Ternary operators Vs. if else statement :
Ternary operators If else
It is used for single expression It is used for single as well as compound
statement and expression.
Produces an expression and returns a value It is a statement. It can have multiple
statements, multiple assignments and
expression in its body.
In nested form it is more complex In nested form it is not complex.
Every conditional operators can be replaced But every if else code can not be replaced
with if else code with conditional operator as if else can have
multiple statements.
Function : functions are the modules from which java programs are built. In other words , it is a
sequence of some declaration and executable statements. In java it is also known as methods ,
which implements the behavior of object.
1. Call by value or pass by value : in call by value method the value of actual parameter is
being copied to formal parameter, so that if any changes are made to the formal
parameter does not effect the actual parameter.
2. Call by reference or pass by reference : in call by reference method the reference of
actual parameter is being passed to the formal parameter , so that any changes made to
the formal parameter reflects actual parameter.
Actual parameter : the parameters used in function call , known as actual parameter.
Formal parameter : the parameters used in function prototype known as formal parameter.
What is a message ?
Software objects interact and communicate with each other using message.
Class method : are invoked directly from the class where as instance methods are invoked on a
particular instance.
Control flow statement : are statements that break up the flow of execution by employing
decision making, looping and branching to enable a program to conditionally execute only
particular blocks of code.
Library function :
String functions :
1. String.toLowerCase ( ) : returns string in lowercse. Return type of this function is
String.
2. String.toUpperCase ( ) : returns string in uppercase. Return type of this function is
String.
3. String.replace( char1,char2): returns string by replacing all the occurrences of char1 by
char2 . Return type of this function is String.
4. String.trim( ) : returns string after removing whitespaces from beginning and end of the
string. Return type of this function is String.
5. String1. equals( string2) : returns true if string1 and string2 is equal otherwise returns
false. Return type of this function is boolean.
6. String1.equalsIgnoreCase( string2) : returns true if string1 and string2 is equal
otherwise returns false but it doesn’t consider the case while comparing strings. It does a
case insensitive comparison. Return type of this function is boolean.
7. String.length( ) : returns length of a string. Return type of this function is int.
8. String.charAt ( ) : returns a character at the specified index. Return type of this function
is char.
9. String1.concat( string2) : Concatenates the specified string 2 at the end of the string1.
Return type of this function is String.
10. String .substring(int beginIndex): It returns the substring of the string. The substring
starts with the character at the specified index. Return type of this function is String.
11. String .substring(int beginIndex, int endIndex): Returns the substring. The substring
starts with character at beginIndex and ends with the character at endIndex. Return type
of this function is String.
12. string . indexOf(char ch): Returns the index of first occurrence of the specified
character ch in the string. Return type of this function is int.
13. String. indexOf(char ch, int fromIndex): Same as indexOf method however it starts
searching in the string from the specified fromIndex. Return type of this function is int.
14. String. lastIndexOf(char ch): It returns the last occurrence of the character ch in the
string. Return type of this function is int.
15. string. lastIndexOf(char ch, int fromIndex): Same as lastIndexOf(int ch) method, it
starts search from fromIndex. Return type of this function is int.
16. String1. compareTo (string2) : compares the two strings and returns positive
value if string1 is greater than string2, returns negative value if string1 is smaller
than string2 and returns 0 if both the strings are equal. Return type of this function is
int.
17. String. startsWith( string prefix) : returns true if string is having specified
prefix otherwise returns false. Return type of this function is boolean.
18. String. startsWith( string prefix, int index) : returns true if the substring (starting from
the specified index) is having the specified prefix otherwise returns false. Return type of
this function is boolean.
19. String. endsWith(String suffix): Returns true if the string ends with the specified
suffix otherwise returns false. Return type of this function is boolean.
20. Character.isLetter( ) : returns true if the specified char value is a letter otherwise false.
Return type of this function is boolean.
21. Character.isDigit( ) : returns true if the specified char value is a digit otherwise false.
Return type of this function is boolean.
22. Character.isWhitespace( ) : returns true if the specified char value is whitespace
otherwise false. Return type of this function is boolean.
23. Character. isUpperCase( ) : returns true if the specified char is in upper case otherwise
false. Return type of this function is boolean.
24. Character.isLowerCase( ) : returns true if the specified char value is in lower case
otherwise false. Return type of this function is boolean.
25. Character.toUpperCase ( ) : returns the uppercase form of specified char value. Return
type of this function is char.
26. Character.toLowerCase ( ) : returns the lowercase form of specified char value. Return
type of this function is char.
Constructors : Constructor in java is a special type of method that is used to initialize the
object. It is invoked at the time of object creation. It constructs the values i.e. provides data for
the object that is why it is known as constructor.
Rules for creating java constructor
There are basically two rules defined for the constructor.
1. Constructor name must be same as its class name
2. Constructor must have no explicit return type
Purpose of default constructor : Default constructor provides the default values to the object
like 0, null etc. depending on the type.
Note: If there is no constructor in a class, compiler automatically creates a default
constructor.
Why do we use parameterized constructor?
Parameterized constructor is used to provide different values to the distinct objects.
this : In java, this is a reference variable that refers to the current object. If there is ambiguity
between the instance variable and parameter, this keyword resolves the problem of ambiguity.
In absence of break statement , execution will continue on into the next case
statement i.e. fall through.
All the wrapper classes (Integer, Long, Byte, Double, Float, Short) are subclasses of the
abstract class Number. The Number class is part of the java.lang package.
Wrapper methods : in each wrapper classes there are some methods known as wrapper
methods. They are used to convert a number type value to a string and string to a number type.
Integer.parseInt( ) : converts string to integer.
Double.parseDouble( ) : converts string to double.
Byte.parseByte ( ) : converts string to byte.
Integer.toString( ) : converts integer value to string.
Double.toString ( ) : converts double value to string.
valueOf( ) : The valueOf method returns the relevant Number Object holding the value of the
argument passed. The argument can be a primitive data type, String, etc.
try-catch : Java try block is used to enclose the code that might throw an exception. It must be
used within the method.
Java try block must be followed by either catch or finally block. Java catch block is used to
handle the Exception. It must be used after the try block only.
You can use multiple catch block with a single try.
1) What is a stream?
Stream is a sequence of data.
It supports different kids of data like byte, primitive data types and objects.
Some streams help in flow of data and others transform data
2) What is input stream ?
Data is read from the source using an input stream
Source of data can be file, a keyboard or computer memory
3) What is output stream ?
Data is written to the designation using output stream
The designation can be a file or the monitor
4) What is byte stream ?
Used to perform input or output of bytes and integers
5) What is character stream ?
When the input data contains characters, character streams are used
6) What is a Scanner class ?
final : by using final keyword , if any variable is assigned, its value can not be changed. If
we try to do so, computer will report error message.
Static Variable:
It is a variable which belongs to the class and not to object(instance).Static variables are
initialized only once,at the start of the execution. These variables will be initialized first,
before the initialization of any instance variables. A single copy to be shared by all instances
of the class
A static variable can be accessed directly by the class name and doesn’t need any object.
Syntax : <class-name>.<variable-name>
Static Function :
It is a method which belongs to the class and not to the object(instance).A static method can access
only static data. It can not access non-static data (instance variables). A static method can call only
other static methods and can not call a non-static method from it. A static method can be
accessed directly by the class name and doesn’t need any object.
Syntax : <class-name>.<method-name>
A static method cannot refer to “this” or “super” keywords in anyway
Non-Static Function:
It is a method which belongs to the class and it cannot be accessed without the object of the
class.
Array : array is a collection of similar type of elements that have contiguous memory location.
Advantage of Java Array
Code Optimization: It makes the code optimized, we can retrieve or sort the data easily.
Random access: We can get any data located at any index position.
Inheritance : Inheritance can be defined as the process where one class acquires the properties
(methods and fields) of another.
subclass (derived class, child class): The class which inherits the properties of other is known
as subclass (derived class, child class)
superclass (base class, parent class): the class whose properties are inherited is known as
superclass (base class, parent class).
Note − A subclass inherits all the members (fields, methods, and nested classes) from its
superclass. Constructors are not members, so they are not inherited by subclasses, but the
constructor of the superclass can be invoked from the subclass.
Types of inheritance
There are various types of inheritance as demonstrated below.
A very important fact to remember is that Java does not support multiple inheritance. This
means that a class cannot extend more than one class.