Notes of Bluej For STD 10
Notes of Bluej For STD 10
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 true.it executes at least once even if the condition
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 iteration This loop is used when the number of iteration
is known beforehand 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 whole This lasts as long as
variable(fields) within a class but of the class definition their object lasts.
outside all the blocks i.e. it can be accessed
are called instance from anywhere in the
variable same class
Actual variable The variable given in Limited to the method Once the uses of the
function call known only in which it is variable is over , this
as actual variable. defined. also gets destroyed.
Formal variable Variable given in Limited to the method Once the uses of the
function declaration only in which it is variable is over , this
or prototype known as defined. also gets destroyed.
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
with if else code 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.
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 :
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.
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 ?
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
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>
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.