Professional Documents
Culture Documents
class& objects:
Student. ———>class
Id,name,gender———
>state(variables)
updateprofile()———
>behaviour(methods)
Note
Data types
Int-integers,string-name
Class objects
S.updateprofile(“John”);
Vid22
1.namingrules:
*First character should be
letter,underscore,$
*keywords can’t be used
*java is case sensitive
I.e int id, int ID ,int Id all are
different
2.newline:
\n\n—>2 newline sequence
ln——>new line
3.comments:
//
/*block quotes*/
——————————————————
————
Vid23. Variable
2.variable kinds
I)instance variables
ii)static variables
III)local variables
I)instance variables
*Declared directly btw the
class(@class level)
*represent object state
*gets default value
*cannot be reinitialized directly
within class
ii)static variables
*declared directly within class
with keyword static
*class variables-one copy per
class
Shared by all the objects of that
class
*gets default value
*cannot be reinitialized directly
within class
iii)local variables
*declared in methods
*includes method parameters
*not accessible outside method
*dont get default values
e.g.
Class student{
//instance variable
Int id;
id=22;// illegal we cannot reinit
inside class it can be reinit inside
method
Static int count=25;//static
variable
Boolean updateprofile (string
newName){
id=22;//ok
Boolean result=true;// newName &
result are local variables
return result;
}
}
Variable types
Primitive
Object references(non primitive)
1.boolean values- false in default
float f1=3.133f;
float f2=4.135f;
go(f1,f2);
1.3)explicit casting
double avg=(double)(2+3)/2;
Vid32 statements
Statements are what constitute
a program
Every statement end with ;
*command to be executed
Declare a variable, change
variable value,invoke a method
*involves one or more expressions
* it involve
literals,variables,operators,metho
ds
eg
Int count=x.getcount();
/*
x
getcount()
x*getcount() //compound
expression
count=x*getcount() //compound
expression
*/
2. Statement types
3 types
I)declaration statements
e.g. int count=25;
ii)expression statements
count=25;// assignment
statement
getcount();// method
invocation statement
count++; //increment
statement
Iii)control flow statements
If (count<100){
………….
}
Vid33 arrays
Array is a container object(i.e
object reference) that holds fixed
(defined @the time of array
creation itself)values of single
type(e.g. int ,double)
*each item In array is called
element & is accesed by index
number(numerical position)
*index no starts with 0
Note
To access 4 th element
Array name[array index number ]
e.g.
Array name[3 ] //I.e array index
no starts with 0
myarray[2]=1
myarray[3]=1
myarray[4]=1
myarray[5]=1
myarray[6]=1
Notes
*since array is an object if only
the type and array name it gets
null value
other ways
int[] myarray=new int[]
{1,2,3,4,5,6,,7};
int[] myarray={1,2,3,4,5,6,7};
int myarray[]; // is also correct
1.2)length
myarray.length—>7
—>students[0]=new student();
students[1]=new student();
since each element is an object
reference we can initialize each
element with student object
—>students[0].name= “john”;
—>students[1].name=“raj”;
int[][]myarray=new int[4][2];
/*
Here the 1st row indicates no of
rows
Here the 2nd row indicates no of
rows
Note
type[]myarray—>array of type
int[]myarray—>array of int
int[][]myarray—>array of array of
int
Refer img
eg
int[][]myarray=new int[4][2]
4 array with 2 elements each
int[][]myarray=new int[][]{
{9,11},
{2,5},
{4,4},
{6,13}
};
1.2)—->array with irregular rows
int[][]myarray=new int[2][]; //2d
arrays with 2columns
myarray[0]=new int[5];// 1st row
with 5elements
myarray[1]=new int[2];//2nd row
with 2 elements
——>array operations
length
myarray.length—> 4 // 4rows & 2
column return
——————————————————
———
Vid36 methods
——>method invoking :
*once the method is defined it
needs to be invoked
Methods logic will never be
executed if it is not invoked
type
var=methodname(arg1,arg2)//arg1
arg2 input data that needs to be
passed
Note:
i)The argument passed must
have one to onematch (int-int or
byte or short)with
method(typeparam1,typeparam2)
parameters & the no of
parameters should be same
ii)Method definition-
parameters/formal parameters
Method invocation-
arguments/actual parameters
1.2)return type
void:
Nothing to return
Optional return as last statement
e.g.
voidprint(){ /*return type is
mandatory in method
declaration(it can be primitive
type or array or class name or
interface or void)*/
system.out.println(“hello
world”);
}
Note
The return type should be just
before the method name or else it
generates CTE
e.g.
Static double sum(double x,
double y){
double Static sum(double x,
double y){ //it generates CTE
EXAMPLE PROGRAM
Static double sum(double x,
double y){
return x+y;
}
psvm(String[]args){
double d =
sum(3.0,2.0);//arguments are
matching
S.o.pln(d);
}
}
eg2.
Static double sum(double x,
double y){
return x+y;
}
psvm(String[]args){
double d = sum(3.0f,2.0);//caller
float to double accceptable ,but
(caller)double to float generates
error
S.o.pln(d);
}
}
Eg3:
Static double sum(float x,float y){
return x+y;
}
psvm(String[]args){
double d = sum(3.0f,(float)2.0);//
double to float generate error we
can overcome this by using
typecasting
S.o.pln(d);
}
}
eg4
Static float sum(double x, double
y){// we are adding float but return
type is double to overcome this
we have to use typecasting
@return//
return (float) (x+y);
}
psvm(String[]args){
double d = sum(3.0f,
(float)2.0);//arguments are
matching
S.o.pln(d);
}
}
Notes:
Float —->to double ok
Double —> float generates error
—>method benefits:
Avoid code duplication
Software reuse