Computer Notes
Computer Notes
Switch Case
Control Variable: The variable passed as on argument to the switch statement that decides which case is to be
executed is known as a control variable in switch case.
Break Statement: This is the statement used at the end of each case which acts as the case terminator. As soon as
break is encountered the control is forced to moved out of the switch block.
Default case: If no case is matched in the switch block for a given value of control variable default case is executed it
is like the else block of if else.
Fall Through: Suppose a break statement is not used at the end of a case statement the control enters the next case
for execution. This condition is said to be Fall Through.
i) It is used when complex logic handling is i) It is used to offer a more concise, clean,
necessary. and compact code.
ii) It can have multiple statements multiple ii) It produces an expression and hence a
assignments and expressions (more than single value can be assigned or
one statement) in its body. incorporated in a larger expression.
iii) Nested if statement is easy and simple to iii) Ternay operator in its nested form
understand. becomes complex and difficult to
understand.
Method Overloading
Functions/Method overloading takes part when there is more than one method in a class with the same name then
the parameter list (function signature) should be different.
i) Number of parameter
ii) Data Type of the Parameter
iii) Number of parameters are same but the data type sequence is different
Arrays
Definition: An array is a homogenous collection of data referred by a name and occupying contiguous memory
spaces.
Arrays are collection of similar type of objects collected as a single unit and occurring at contiguous locations in the
memory, referred by a single variable with index to refer to positions of each element.
Since arrays is simply a list of data, composed of primitive data types or objects it is also called composite data type.
Dimensional Array:
A dimensional array is a concept of using a few variables declared with the same data type.
Single Dimensional array is a structure that is represented along with x axis by using a name of a variable with
multiple values having different subscripts (number of locations).
The index or subscript of an array in java always begin from zero and s represented using an integer.
Array Sorting
Sorting refers to the rearranging the data of an array either in ascending or descending order. To rearrange the data,
we are having various techniques in Java, out of those the very common techniques are –
The automatic conversion of primitive data type into an object of its equivalent wrapper class is known as
Autoboxing.
Syntax:
Unboxing:
e.g.
Constructors
Constructor is a special type of method which must have the identical name as per the class and it must not return
any value. Normally we use it to initialise the instance or member variables. It cannot be called explicitly. It is used to
create an object of the specific class .
Without creating an object, we cannot run any class program, that is if we want to access (call) the class
method or class variable, then we need the object of the class .
When we have not defined any constructor with a class then Java compiler creates an empty constructor on
its own and then create the object.
Without the constructor we cannot create an object.
Method Constructor
1) Method name must be different from the class 1) Constructor name must be identical to class
name. name.
2) Method must have a return data type. 2) It must not have any return data type.
3) Method must be called explicitly. 3) It cannot be called explicitly. Only at the time of
declaration of the object it will be called implicitly.
4) Normally we write the input or processed or 4) Normally we initialize the instance or member
output or any combinations statements inside it. variables inside it.
i) Default Constructor : Here we initialize the instance or member variable with its default value.
ii) Parameterised Constructor : Here we can initialize the global section variables with the parameterised
values. At the time of creating the objects, the values are assigned to the variable.
iii) Copy Constructor : Not in our Syllabus
Empty Constructor : When there is no constructor defined in a class then compiler will generate a
constructor on its own without any statement in the body of it.
e.g.
class ABC
{
int x,y;
ABC() //default constructor
{
x=y=0;
}
e.g.
import java.util.*;
public class Bonus
{
int Sal[];
String Name[];
int n;
double bon[];
Bonus(int n) //parameterised constructor
{
this.n=n;
Sal=new int[n];
Name=new String [n];
bon=new double [n];
}
....
....
....
.... //End of main method
} //End of code
Column
Declaration:
Syntax:
a[0][0] = 5;
a[2][1] = 7;
1 2 3 4
5 6 7 8
9 10 11 12
1 2 3
4 5 6
7 8 9
//String is a sequence of character. In jva this sequence or group of characters is enclosed with double quotes(“ “)
E.g. – “COMPUTER” is a valid string constant/literal and ‘C’ is a character literal, whereas “C” is a string literal.
The string data type is a class defined in the java.lang package and every variable that we create of String data type is
object of the String class.
A String containing digits with double quotes is created as a symbol and hence cannot be used for arithmetic
calculations.
E.g. – System.out.rintln(“12”+”15”);
Output = 1215
System.out.rintln(“COMP”+”APPL”);
Output = COMPAPPL
System.out.rintln(“1*2+3”+”3*5”);
Output = 1*2+33*5
System.out.rintln(“5+3+2”+5+3+2);
Output = 5+3+2532
System.out.rintln(“5+3+2”+(5+3+2));
Output = 5+3+210
String variable :
String variable is a named memory location that contains the String constant or String literal. The value of the
variable may change in the program. A variable declared with the data type of String gains the capability of storing a
set of characters.
String Function :
String manipulation can be done by the inbuilt functions related to the String variable or object. All the functions are
used with the String variable name followed by a .(dot) operator and the function name; the parameter value (s)
is/are required as per the functionality of the specific function.
Syntax :
1) int.length()
2) char.charAt()
3) String.toLowerCase()
4) String.toUpperCase()
5) int.indexOf(char/String)
6) int.lastIndexOf(char/String)
7) String.trim()
8) String.substring(int begin_index)
9) String.substring(int begin_index, int end_index)
10) boolean.equals(String)
11) boolean.equalsIgnoreCase(String)
12) int.compareTo(String)
13) int.compareToIgnoreCase(String)
14) String.concat(String)
15) String.replace(char/string old val, char/string new val)
16) boolean.startsWith(String)
17) boolean.endsWith(String)
18) String.valueOf(<any data type>)