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

Computer Notes

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Computer Notes

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Escape Sequences

Escape Sequence Description

\t Inserts a tab in the text at this point.

\b Inserts a backspace in the text at this point.

\n Inserts a newline in the text at this point.

\r Inserts a carriage return in the text at this point.

\f Inserts a form feed in the text at this point.

\' Inserts a single quote character in the text at this point.

\" Inserts a double quote character in the text at this point.

\\ Inserts a backslash character in the text at this point.

Switch Case

Terms related to switch statement:

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.

Rules while using the switch statement:

i) A case statement cannot exit by itself, outside of a switch block.


ii) The break statement which appears as the last statement in a case block is encountered in the case block,
program execution jumps to the line code following the switch block, i.e. outside of the body of switch
statement.
iii) Braces should be used to denote the start and end of the switch block (not on case block).
iv) Only character and integer constants (byte,short,int and char) are use in the case evaluation and allow only
for equality comparison.
Difference between if-else and Ternary Operator

If-else Ternary Operator

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.

Parameter list may differ in:

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.

Dimensional Array basically of two types:

i) Single Dimensional Array


ii) Double/Multi-Dimensional Array

Single Dimensional Array:

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 –

i) Bubble Sort Technique


ii) Selection Sort Technique
CODE
Selection Sort
CODE
Autoboxing

The automatic conversion of primitive data type into an object of its equivalent wrapper class is known as
Autoboxing.

Syntax:

<class name> <object name> + new <class name> (parameter);

Unboxing:

It is a system of converting object wrapper class into primitive data type.

e.g.

Integer x =new Integer(25); //Autoboxing

Int y=x; //Unboxing

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.

There are mainly three types of constructors:

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;
}

ABC(int p, int q) //parameterised constructor


{
x=p;
y=q;
}
....
....
....
....
void display()
{
System.out.println(x+"\t"+y);
}
public static void main()
{
//creating object of ABC class
ABC obj1 = new ABC();
obj1.display(); //output = 0 0
//Creating an object of ABC class-Parameterised
ABC obj2 = new ABC(10,50;
obj2.display(); //output = 10 5
....
....
....
....
}
}
 this. : Refers to the class own property

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

Double Dimensional Arrays

Column

0,0 0,1 0,2 0,3 0,4


1,0 1,1 1,2 1,3 1,4 Rows
2,0 2,1 2,2 2,3 2,4
3,0 3,1 3,2 3,3 3,4

Declaration:
Syntax:

<datatype> <arr.name>[][] = new <datatype> [<no of rows>][<no of columns>];

Inserting values into DDA:

a[0][0] = 5;

a[2][1] = 7;

Inserting constant values as well as creating the DDA:

Int x[][] = {{1,2,3,4},{5,,6,7,8},{9,10,11,12}};

1 2 3 4

5 6 7 8

9 10 11 12

Inserting values from the user:

int ar[][] = new int [3][3];


for(r=0; r<3; r++)
{
for(c=0; c<3; c++)
{
System.out.println(“Ener a no:”);
ar[r][c] = sc.nextInt90; //9
}
}
//Printing values of an DDA in DDA format :

1 2 3

4 5 6

7 8 9

for(r=0; r<3; r++)


{
for(c=0; c<3; c++)
{
System.out.println(ar[r][c]+”\t”);
}
System.out.println();
}

String Manipulation in JAVA

//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

Int A=10, B=5;


System.out.rintln(“A+B = ” + A+B);
Output = A+B = 105
System.out.rintln(“A+B = ” + (A+B));
Output = A+B = 15

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.

Syntax to store a String constant or a literal :

String <variable> = “<value>”;

E.g – String s = “COMPUTER”;

String t = “ ”; // “ “ means null that is nothing.

The default initial value of a string variable represented as “ ”(means null).

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 :

<String variable name>.<function name>([Parameter 1, Parameter 1, ….]);


String Methods List:

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

You might also like