0% found this document useful (0 votes)
479 views

Nirmal Jha Notes

The document provides an overview of important concepts in object-oriented programming like abstraction, encapsulation, polymorphism, and inheritance. It defines key terms like class and object. It also discusses primitive and non-primitive data types in Java, including their default values. The document covers other Java concepts such as variables, operators, statements, expressions, comments, and loops.

Uploaded by

Satyam Dev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
479 views

Nirmal Jha Notes

The document provides an overview of important concepts in object-oriented programming like abstraction, encapsulation, polymorphism, and inheritance. It defines key terms like class and object. It also discusses primitive and non-primitive data types in Java, including their default values. The document covers other Java concepts such as variables, operators, statements, expressions, comments, and loops.

Uploaded by

Satyam Dev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Notes on Bluej CARMEL SCHOOL,GIRIDIH

Important features of OOPs :


 Abstraction : is a concept of hiding unnecessary details and representing only the very
essential features.
 Encapsulation : wrapping up of data and methods in a single unit called class known as
encapsulation.
 Polymorphism : a function behaving differently upon different objects known as
polymorphism . polymorphism Implements function overloading.
 Inheritance : is the process by which an object acquires the properties of another object.
Class : is the collection of objects of the same kind i.e. that share the common properties and
relationship.
Objects : it is an instance of a class having some characteristic and behaviors.
Characteristics of an object : identity , classification , inheritance and polymorphism.
Data types : identify what type of value can be stored in a particular variable.
Types of data type :
Primitive data types : are not composed of other data types . they are as follows :
byte :1 byte : -27 to 27 -1
short : 2 bytes : -215 to 215-1
int :4 bytes : -231 to 231-1
long : 8 bytes : -263 to 263-1
float : 4 bytes
double : 8 bytes
char : 2 bytes ( it has a minimum value of \u0000 (or 0) and a maximum
value of \uffff (or 65,535 inclusive)
boolean : 1 byte

In addition to 8 primitive data types Java provide special support for character String
through the Java.lang.String class.

String objects once created , their value can not be changed.

Default values for each data types :

Data types Default values


byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0
char ‘\u0000’
boolean false
String null
Non primitive data types : are composed of other data types
 Token : is the smallest unit of program. Various features of token are as follows :
 Keywords : are the reserved words used within the program to specify a particular task.
 Identifiers : represent variables , objects, class, method and arrays.

Created By : NIRMAL JHA Page 1 of 17 8-Apr-20


Notes on Bluej CARMEL SCHOOL,GIRIDIH

 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.

 Separators : include special characters such as , ; [ ] { } ( ) etc.


 Operators : are special symbols that perform specific operations , according to a
precedence on one or more operand to return result. The Types of operators are as
follows :
o Arithmetic operators : contain arithmetical symbols such as +,-,*, /, %
o Assignment operators : are used to assign values to a variable .=, -=,+=,*=,
/=,%= are the assignment operators.
o Unary operators : contain only one operand . they are ++, --
o Binary operators : contain two operands .
o Ternary operators : contain three operands. They are ?, :
o Relational operators : these operators determine one operand is less than, greater
than, equal or not . they are >,<,>=,<=,!=,= =
o Logical operators : are used to join more than one condition. They are &&,||, !

Block : is a group of one or more statements between a pair of curly braces { }.


Statements : are similar to sentences in human languages. A statement forms a complete unit of
execution.
Expression : is a construct made up of variables , operators and method invocations that
evaluates to single value.
Mixed mode expression : depend on the types of largest operand in the expression.
Comment : is a remark from the programmer to explain some aspect of the code top someone
who is reading the source code. Comment lines are ignored by the compiler.
Types of comment :
Single line comment ( //) : only one line of text can be written.
Multi line comment : ( /* ……..*/) : multiple lines of comment can be written in this.
Documentation comment (/**……………..*/) : they are used to produce HTML file.
Unicode : defines a complete international character set that can represent all the characters
found in all human languages. Java character is 16 bit type.
Type conversion : The process of converting one primitive type to another is called type
conversion or casting.Following are the types of conversion :
i. Implicit conversion or automatic conversion or widening or coercion : in this
conversion compiler checks in the whole expression , whichever data type is higher,
in that type only result is being given.

Created By : NIRMAL JHA Page 2 of 17 8-Apr-20


Notes on Bluej CARMEL SCHOOL,GIRIDIH

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

( type ) Unary type cast


* Multiplication
/ Division
% Modulus
+ Addition
- Subtraction
< Relational less than
<= Relational less than or equal
> Relational greater than
>= Relational greater than or equal

== 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.

Created By : NIRMAL JHA Page 3 of 17 8-Apr-20


Notes on Bluej CARMEL SCHOOL,GIRIDIH

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.

Created By : NIRMAL JHA Page 4 of 17 8-Apr-20


Notes on Bluej CARMEL SCHOOL,GIRIDIH

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.

Created By : NIRMAL JHA Page 5 of 17 8-Apr-20


Notes on Bluej CARMEL SCHOOL,GIRIDIH

What happens in absence of break in switch case?


In absence of break in switch case all statements after the matching case label are executed in
sequence, regardless of the expression of subsequent case labels, until a break statement is
encountered. The same situation is also known as fall through.
Format of defining a function or method :
Access specifier type function name (parameter list)
{
// function body
}
Access specifier : Determines the type of access to the function. It can be either public or
private. The default access specifier is friendly. Which is not a keyword.
Type : specifies the type of value returned by the method.
Function name : is the name of the function which can be any valid identifiers.
Parameter list : are the variables that receive the value of the argument passed to the function
when it is called. This can be empty if there are no parameters.
Function prototype (function signature) : is the first line of the function definition . it specifies
the return type ,function name and the number of parameters and type of parameters or
arguments.
Function header : this basically refers to the number and types of arguments. It is a part of
function prototype. But sometime it also refers to a function prototype.
Functions are divided into two categories :
1. Pure function or Accessor methods- the method which return values and do not change
state are called pure functions. This function may also return values or may not.
2. Impure functions or Mutators methods.- may return values but also change state.
Returnable function : a function that returns value known a returnable function.
Non returnable function : a function that never returns value , known as non returnable
function. Such type of function has always the type as void.
Recursive function : a function that calls itself repeatedly known as recursive function.
There are two ways to call the function :

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 :

Created By : NIRMAL JHA Page 6 of 17 8-Apr-20


Notes on Bluej CARMEL SCHOOL,GIRIDIH

Math class contains following function :


1. Math.ceil ( ) : this function returns the smallest integer number which is greater than or
equal to the number. The return type of this function is double.
2. Math.floor ( ) : the return type of this function is double. This function returns the largest
integer number which is less than or equal to the number.
3. Math.rint ( ) : the return type of this function is double. This function returns the
truncated value of a number. If number appears in the integer part is odd and in fractional
part is more than or equal to 5 , then 1 is being added to the integer part , otherwise it will
return the integer part number. If in the integer part there is even number and in the
fractional part it is more than 5 then 1 is being added to the integer part , otherwise it will
return the integer part number only.
4. Math.round( ) : return type of this function is int. Rounds off a double value by first
adding 0.5 to it and then returning the largest integer that is less than or equal to this new
value.
5. Math.abs( ) : return type of this function is double. Returns the absolute value of a
number.
6. Math.max (a,b ) : Return type of this function is double.Takes two double values, a and
b, and returns the greater of the two.
7. Math.min( a,b) : Return type of this function is double. Takes two double values, a and
b, and returns the smaller of the two.
8. Math.sqrt ( ) : Return type of this function is double. Returns the square root of a
number.
9. Math.pow( a,b) : Return type of this function is double. Returns the value of a raised to
the power b.
10. Math.random ( ) : Return type of this function is double. Generates a random number
between 0.0 and 1.0.
11. Math.exp (a ) : Return type of this function is double. Returns the exponential number
e(2.718...) raised to the power of a
12. Math.sin( ) : Return type of this function is double. Returns the trigonometric sine of an
angle in radian. Where radian = π/ 180*degree
13. Math.cos( ) : Return type of this function is double. Returns cosine value of an angle in
radian.
14. Math.tan ( ) : Return type of this function is double. Returns tangent value of an angle in
radian.
15. Math.asin( ) : Return type of this function is double. Returns the angle whose sin is a.
16. Math. acos( ) : Return type of this function is double. Returns the angle whose cos is a.
17. Math.atan ( ) : Return type of this function is double. Returns the angle whose tan is a.
18. Math.log ( ) : Return type of this function is double Returns the natural logarithm of a.

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.

Created By : NIRMAL JHA Page 7 of 17 8-Apr-20


Notes on Bluej CARMEL SCHOOL,GIRIDIH

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.

Created By : NIRMAL JHA Page 8 of 17 8-Apr-20


Notes on Bluej CARMEL SCHOOL,GIRIDIH

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

Types of java constructors


There are two types of constructors:
1. Default constructor (no-argumentr): A constructor that have no parameter is known as
default constructor.
2. Parameterized constructor : A constructor that have parameters is known as
parameterized constructor.

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.

Constructor Overloading in Java


Constructor overloading is a technique in Java in which a class can have any number of
constructors that differ in parameter lists.The compiler differentiates these constructors by
taking into account the number of parameters in the list and their type.

Can constructor perform other tasks instead of initialization?


Yes, like object creation, starting a thread, calling method etc. You can perform any
operation in the constructor as you perform in the method.

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.

Difference between constructor and method in java


There are many differences between constructors and methods. They are given below.
Java Constructor Java Method

Created By : NIRMAL JHA Page 9 of 17 8-Apr-20


Notes on Bluej CARMEL SCHOOL,GIRIDIH

1. Constructor is used to initialize the state of an 1. Method is used to expose


object. behavior of an object.
2. Method must have return
2. Constructor must not have return type.
type.
3. Method is invoked
3. Constructor is invoked implicitly.
explicitly.
4. The java compiler provides a default constructor if 4. Method is not provided by
you don't have any constructor. compiler in any case.
5. Method name must not be
5. Constructor name must be same as the class name.
same as class name.

String vs. StringBuffer :


String StringBuffer
String class is immutable. StringBuffer class is mutable
String is slow and consumes more memory StringBuffer is fast and consumes less
when you concat too many strings because memory when you cancat strings.
every time it creates new instance.

Switch-case : It is a multiple branching statement. It transfers control to different parts of the


code based on the value of an expression. Where expression can be either integer( byte,short,int)
or character(char).

In absence of break statement , execution will continue on into the next case
statement i.e. fall through.

Difference between , switch-case and if-else:


switch-case if-else
It can only test for equality. It can evaluate any type of boolean
expression.
Switch only looks for a match between the If statement uses a series of expression having
value of the expression and one of its case unrelated variables.
constant values.
It runs much faster than the if-else-if It is relatively slower than the switch-case.
statement
With switch statement floating point variables If-else statement can use floating point
and constants cannot be used. constant or variables.
Wrapper class : it is a collection of classes used to wrap variables of simple data type Boolean,
Character,Float,Integer and Long are wrapper classes.
Boxing :The object of the wrapper class contains or wraps its respective primitive data type.
converting primitive data types into object is called boxing.
Un boxing : the Wrapper object will be converted back to a primitive data type, and this
process is called un boxing.

Created By : NIRMAL JHA Page 10 of 17 8-Apr-20


Notes on Bluej CARMEL SCHOOL,GIRIDIH

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.

Primitive Type Wrapper class


boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double

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.

The finally Block


The finally block always executes when the try block exits. This ensures that the finally block is
executed even if an unexpected exception occurs. But finally is useful for more than just exception
handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a
return, continue, or break.

Created By : NIRMAL JHA Page 11 of 17 8-Apr-20


Notes on Bluej CARMEL SCHOOL,GIRIDIH

Difference between throw and throws in Java


There are many differences between throw and throws keywords. A list of differences between
throw and throws are given below:
throw throws
Java throw keyword is used to explicitly throw Java throws keyword is used to declare an
an exception. exception.
Checked exception cannot be propagated using Checked exception can be propagated with
throw only. throws.
Throw is followed by an instance. Throws is followed by class.
Throw is used within the method. Throws is used with the method signature.
You can declare multiple exceptions e.g.
You cannot throw multiple exceptions. public void method()throws
IOException,SQLException.
Stream
A stream can be defined as a sequence of data. there are two kinds of Streams
 InPutStream: The InputStream is used to read data from a source.
 OutPutStream: the OutputStream is used for writing data to a destination.
Java Byte streams : are used to perform input and output of 8-bit bytes.
Java Character streams :are used to perform input and output for 16-bit unicode.
 Standard Input: This is used to feed the data to user's program and usually a keyboard is
used as standard input stream and represented as System.in.
 Standard Output: This is used to output the data produced by the user's program and
usually a computer screen is used to standard output stream and represented as
System.out.
 Standard Error: This is used to output the error data produced by the user's program
and usually a computer screen is used to standard error stream and represented as
System.err.
Scanner and Printer classes
Input and Output using Scanner class

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 ?

Created By : NIRMAL JHA Page 12 of 17 8-Apr-20


Notes on Bluej CARMEL SCHOOL,GIRIDIH

 A class in java.util package to read input from the keyboard


 Data of various types like int, float, double, char etc are used
 This class further converts the data into binary form
7) What are the uses of scanner class?
 Allows the user to input the values of various types
 This allows the user to input the values from either the keyboard or from file without using
any conversion
8) How will you import a scanner package ?
 import java.util.Scanner;
9) How will you create a scanner object ?
 Scanner sc = new Scanner(System.in);
10) What is the use of nextInt()?
 Receives the next token from scanner object
 Can be expressed as an integer and stored in integer type
11) What is the use of nextFloat()?
 Receives the next token from scanner object
 Can be expressed as an floating and stored in float type
12) What is the use of nextLong()?
 Receives the next token from scanner object
 Can be expressed as an long and stored in long type
13) What is the use of nextDouble()?
 Receives the next token from scanner object
 Can be expressed as an double and stored in double type
14) What is the use of next()?
 Receives the next token from scanner object as a string
15) What is the use of nextline()?
 Receives the entire line of the string

16) Name the class that is used to read formatted input ?\


 Scanner
17) How does a scanner read the data ?
 Scanner reads the input and breaks down the input into tokens
 A token is a part of input that is separated from other tokens by separations (White spaces)
18) What are the rules to be followed while using scanner classes ?
 Determine if a specific type of the input is available or not(Using has Next methods)
 If input is available then read it by calling next method
 Repeat the above process until all tokens of input are read
19) What do you mean by input mismatch Exception ?
 Scanner reads the numeric data throws an InputMismatchException, if the next token is not
the value as expected by the method.
20) What is the use of the function Boolean hasNextInt() ?
 Returns true if the next token in the scanner object can be interrupted as an int value
21) What is the use of the function Boolean hasNextLong() ?
 Returns true if the next token in the scanner object can be interrupted as a long value
22) What is the use of the function Boolean hasNextDouble() ?
 Returns true if the next token in the scanner object can be interrupted as a double value

Created By : NIRMAL JHA Page 13 of 17 8-Apr-20


Notes on Bluej CARMEL SCHOOL,GIRIDIH

23) What is the use of the function Boolean hasNextFloat() ?


 Returns true if the next token in the scanner object can be interrupted as a float value
24) What is the use of the function Boolean hasNext() ?
 Returns true if the scanner object has another token in its input otherwise false
25) What is the use of the function Boolean hasNextLine() ?
 Returns true if the scanner object has another line in its input otherwise false
26) What are printer writer classes ?
 One of the character based class
 It makes our output more productive and standard
27) How will you create printer writer object ?
PrintWriter prn = new PrintWriter(System.out,true);
28) What is the use of flushIfNewline ?
Controls the java system to flush the output stream every time a newline character(\n) is output
29) What are the methods used for printing in printer classes ?
Print() and println()
Public:
public classes, methods, and fields can be accessed from everywhere.
Protected:
protected methods and fields can only be accessed within the same class to which the
methods and fields belong, within its subclasses, and within classes of the same package,
but not from anywhere else.
Private : if a member of a class is specified private then the member can be accessed by
other members of the same class only.
default (no specifier) :
If you do not set access to specific level, then such a class, method, or field will be
accessible from inside the same package to which the class, method, or field belongs, but
not from outside this package.

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>

Non -Static Variable:


It doesn’t retain/update the value .It doesn’t have the global scope because it’s usually act
as local variable.

Created By : NIRMAL JHA Page 14 of 17 8-Apr-20


Notes on Bluej CARMEL SCHOOL,GIRIDIH

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.

Disadvantage of Java Array


 Size Limit: We can store only fixed size of elements in the array. It doesn't grow its size
at runtime. To solve this problem, collection framework is used in java.

There are two types of array :


 Single Dimensional Array : an array having one row and multiple columns or one
column and multiple rows known as single dimension array.
 Multidimensional Array (Double dimension array) : an array having multiple rows
and multiple columns known as double dimension array.
Copying a java array
We can copy an array to another by the arraycopy method of System class.
Syntax of arraycopy method
arraycopy( bject src, int srcPos,Object dest, int destPos, int length )
Example : char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e', 'i', 'n', 'a', 't', 'e', 'd' };
char[] copyTo = new char[7];
System.arraycopy(copyFrom, 2, copyTo, 0, 7);

What is Bubble Sort?


Bubble sort is a sorting algorithm that operates by going through the list to be sorted repeatedly
while comparing pairs of elements that are adjacent. If a pair of elements is in the wrong order
they are swapped to place them in the correct order. This traversal is repeated until no further
swaps are required (which means that the list is sorted).

What is Selection Sort?


Selection sort is also another sorting algorithm that starts by finding the minimum element in the
list and swapping it with the first element. Then the minimum element is found from the remainder
of the list (from the second element until the last element in the list) and swapped with the second
element. This process is repeated for the remainder of the list by placing swapped elements in
order.

Created By : NIRMAL JHA Page 15 of 17 8-Apr-20


Notes on Bluej CARMEL SCHOOL,GIRIDIH

What is Linear Search?


Linear search is the simplest searching method, which checks each element in a list sequentially
until it finds a specified element. The input to the linear search method is a sequence (such as an
array, collection or a string) and the item that needs to be searched.

What is Binary Search?


Binary search is also a method used to locate a specified item in a sorted list. This method starts
by comparing the searched element to the elements in the middle of the list. If the comparison
determines that the two elements are equal the method stops and returns the position of the element.
If the searched element is greater than the middle element, it starts the method again using only
the bottom half of the sorted list. If the searched element is less than the middle element, it starts
the method again using only the top half of the sorted list. If the searched element is not within the
list, the method will return a unique value indicating that.

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).

Extends: is the keyword used to inherit the properties of a 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.

The super keyword


The super keyword is similar to this keyword following are the scenarios where the super
keyword is used.
 It is used to differentiate the members of superclass from the members of subclass, if
they have same names.
 It is used to invoke the superclass constructor from subclass.

Interface : An interface is a reference type in Java, it is similar to class, it is a collection of abstract


methods. A class implements an interface, thereby inheriting the abstr act methods of the interface.
An interface is similar to a class in the following ways:
 An interface can contain any number of methods.
 An interface is written in a file with a .java extension, with the name of the interface
matching the name of the file.
 The byte code of an interface appears in a .class file.
 Interfaces appear in packages, and their corresponding bytecode file must be in a
directory structure that matches the package name.

Created By : NIRMAL JHA Page 16 of 17 8-Apr-20


Notes on Bluej CARMEL SCHOOL,GIRIDIH

However, an interface is different from a class in several ways, including:


 You cannot instantiate an interface.
 An interface does not contain any constructors.
 All of the methods in an interface are abstract.
 An interface cannot contain instance fields. The only fields that can appear in an interface
must be declared both static and final.
 An interface is not extended by a class; it is implemented by a class.
 An interface can extend multiple interfaces.

Interface : The interface keyword is used to declare an interface.


implements : A class uses the implements keyword to implement an interface.
Abstract class : A Java abstract class is a class which cannot be instantiated, meaning you
cannot create new instances of an abstract class.

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.

Created By : NIRMAL JHA Page 17 of 17 8-Apr-20

You might also like