You are on page 1of 28

1.

class& objects:

Data-student,course & dept


e.g.:
Id, name, gender
Behaviour- whereas registering &
updating comes under behaviour

Student. ———>class
Id,name,gender———
>state(variables)
updateprofile()———
>behaviour(methods)

Note
Data types
Int-integers,string-name

Class objects

Class student test{


psvm(Sting[] args){
//1.creating a new student object
Student s= new student();
//keyword new followed by student
followed by parenthesis to create
student object assigned to
variable s on left whose datatype
is student
I.e)student s- indicates that s
word hold student object

//2.setting students state


S.id=1000;
S.name=“JOAN”;
S.gender=‘male”;

//3.updating profile with correct


name (via methods)

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

*Variables are containers that


hold data
1.variable type
Int id=1000;
String name=“john”;
Student s=new student();
Note :variable s can be used to
invoke the method within student
class
**statically typed language—
>static type checking**(i.e. int id,
string name once decl data type
can’t be changed)
**dynamically typed language—
>dynamic type checking(*type of
variable is not
constant,*performed at runtime)

1.2 variable declaration:


<type> <name>[=literal or
expression];
*literal—>rawdata
e.g. int count=25;
Expression—>evaluated to a
single value
Int count=x;
int count=getfun();

1.3. reintializing variable:


Variable can be reinitialised by
using assignment statement
I.e <variabe name>=literal or
expression

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

vid30. Variables type casting


1.Typecasting:
Assigning variable or
literal of one type to variable of
another type.
e.g. int<—long, int<-byte
*only numeric to numeric casting
is possible
I.e cannot cast to boolean or vice
versa

1.1 Types of type casting:


2 types
1)implicit casting
2)explicit casting
—>implicit casting:
*variable or literal of smaller
type to variable or literal of larger
type(I.e widening conversion)
e.g. int x=65;
long y=x;(implicit casting by
compiler)
—>explicit casting:
*larger to smaller(narrowing
conversion)
Note:
Casting to char is always explicit
e.g) explicit casting
long y=42;
Int x=(int)y;
—>information loss in explicit
casting:
*out of range assignment
*Truncation:
Floating point to
integer/character will always
truncate.
eg
Int x=(int)3.14f; // x=3
Int y=int(int)0.9;//y=0
Char c=(char)65.5;//c=‘A’
i.e. first converted into int65
after truncation then
corresponding utf value A
1.2)casting use cases
—>implicit casting:
/*
go(double d1, double d2 ){
…………….
}
*/

float f1=3.133f;
float f2=4.135f;
go(f1,f2);
1.3)explicit casting
double avg=(double)(2+3)/2;

vid31. Variable object


references
Student s = new student();
Here
Student-allocate space for
reference variable
New student()-allocate space for
new student object
S- students object reference
Note :
All objects live on heap

1.2) bit depth& default


*Default value of an object
reference is null until explicitly
initialized
Student s;//s is null until initialized
*
s.updateprofile();//NullpointerExce
ption

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){
………….
}

Note :expression statements&


control flow statements can’t be
declared @class level

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

1.1 creating an array:

int[] myarray=new int[7] //each


element gets default 0
7 here denotes no of elements to
be created
myarray[0]=1
myarray[1]=1

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

*accesing outside array boundary


generates runtime error
int item=myarray[7]; //runtime
error bcoz I’m trying to access
the 7 th element which doesn’t
exist

1.3 )array of object references


student[] students= new
student[2]
/*here student[] —-> object types
students——>array name
New student[2]—>2 student
array elements
I.e) students[0] &students[1] // by
default have null values

—>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”;

Vid34 creating a 2d array

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

1.1)2d array creation another way

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

I.e results in symmetric matrix

——>array operations
length
myarray.length—> 4 // 4rows & 2
column return

int[]row=myarray[2];// variable row


of int type is assigned to 3rd row
——————————————————
————
Vid35 3dArrays
1.array creation:
int[][][] myarray=new int[4][4][4];

——————————————————
———
Vid36 methods

*Behaviours defined by methods


*methods are self contained logic
that can be used many times
*. methods can receive input &
generate output
Note: caller passes the input to
method
—->syntax
returntype methodname(type
param1,type param2){
….
Return somevalue;
}
Note:if no parameters the method
does not take any input
*return type is mandatory or else
it generates compile time error
*method name and method
parameter list together
arereffered to as method
signature
/* returntype—>defines the return
type
Method name —>defines the
method name
Paramater - is a variable it can be
either primitive or an object
reference
*the variable returned can be
either primitive or an object
reference

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

Returntype in the method


declaration
Returning value in the method
declaration should also match

Type variable in the callers end


should match
I.e (int pointer any other smaller
datatypes )
*/

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

You might also like