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

Java Notes

java notes

Uploaded by

Watch King
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Java Notes

java notes

Uploaded by

Watch King
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Data (2")

int
Tes.
32 bits
byte No integers stofed- 28
Shor t 16
bts
\ona 64 bjts

Range to
(Unsigned)
-126tot 127
inti= I0
byle 130

- 126

Valve-12-6 willbe storedi


Primitive DatTypes
i)There ase total& data tyfes
int,short, byfe, long
(loat double char, boolean
(32) (6u) C1,bghe).

Aprecision D7 16 plecisionDT
CABSnT-30o1
Flonting /
Point Red Numbers Daa Types
the
Numters. haring pot
Rxressedwith
rationa
hele a decimal points
primitive data, types Float Dovble
) Float
Single(iecision data tyee (S2e
= 32 bits)
Decimal point prcisions q diglts
) Doule-
Double precision doto tyge( e1ze 6u bits)
Decimalpoint prectšton =I6 digits

Char
t an store onlyaSngle chatactef

Boolen
trve Lalse
It cn haye 2, valves

rins
•.H is
a seayuence o
chargters. non-pimitive
data type to trmg
stoíe character. tings.
S.

cdass
actuallya
Operators -
special
Opesatos aresecial enbols that Rro1m
operateions on 4,2, 9 3 0perOnd
Arithmetc Operators -

publie cass App_


public stohe vold main
CShing. &1
System.out frintln s) 2
Helo Wórld)

System ovt
prittnCNomei Aean "
Sout ( Addíess:
Sout ( Mob:
so0tCEM ail?mo{on@gail
usu2305")
com

pubi' ststie Noid main (sting] arg)


Sysiemout- pritn " Sum
sout ( (xy));
Prodvct=
SoUt (
Dierene K-y))}
Sout ( Diyision
Sot ( (K/y)
Aodotts= Remainder
ABST-300l
TShtement

o
codebased on whethey a|ven ccndihn
expiession Is tye ofilsSe
CCondition ) cond ition elher evaly
1/ Code to ttue oY

alse
blok

Ese Bcck
evalvate
Not neCessary
to lalse
Executes icondition

Re laional Operotors

Q- Given tae ad calculatfe afea 2 Perimeter

public class Aee


g
public stohe soin main(Shing
II
double 23,
doublear, = Math. PI Math pow (I, 2);
Moth.PI*i
9)
Sout Are>
sout ( Pesimeter per),
Scanner Class -
impor
ava.il. Scanner
publie class
ble stutik
void main (Shing
Scanney
LI Sg
new Segnner (Sy:ten.in);
int o

double dbl =
SC.
String woTd
netOouble C);
Sc.next)
Siring lonstin=

- Input 3
num,cslulote
import
avauHl.Scanner;
main )
s new scanner (Sysiem.in)
Jouble
sout C
x,Y2
Enter numb js :

Z=Sc. next Double


double av tY+2)/3.o
SoutAverage
a2 b:3

ba-b

main ()
Scanner. SC new Scqnner (Sy shem.n
Sout Enter 2 number s ")
O

int a SC. next Int


int b= SC, nextIntC)

Sout (Orignal walue \n a=


b=

b= a-b

Sout New Valves \n

Compaíe
main()
Scanner (Sy steen.in)
Scanner Sc_ne
Sout Entet
int
SC
netntC))
int
b se. nest Int C)
Sout(s
A\s. bigger")

3
elie
Sout( ßoh ae eaval

I| Check whether an integer


maino

Soutl Even")
else

ogital Opesatozs
tapes NOT
190
6 and
I Check'l a is
mber iS divisble by
NOT diwistble by S
=>mt. O)

(humo 6=
Sout(Yes")

else
Sout ( No ");

/ Check i an yeot
Heor leae

Yes

-tse
SouttNO "}

int 4
=O
Sou (" Ye");
3
else
Sout C
NO))
ind hether it is tve,e or

no. ")

else i Cumo){
out (Rositive
3 no,)
else

3
Sout Negaiye no);

the day no, print


he roff esonding d
SA1

Int. day Si
iCday )
c6

else
Sot Invalid input)
Given the
marksint its erade

80-89 At
A

else
int

else
Sout A+ ");
3

else
Sout (A);
(morK)=6o marks= 69)
else
Sout &")
marks<= S9)
Sout ( );}:
else S oUt (
int num= 30
Jo 6 = 0)
sou
Yes"),
else
)

Alternale way>
nom
/6=O)
o
Yes")
else
2

(aiven s
DUbers,hek vhish isigest

Sout X
is the bigAes) ?

else
7 is the
bih est)
else
Sout Y
is the
ese Sout * Z
iges)
;
3
is the bigest ")
feql distinct
4
ools are
Veol but Sone
imagina

int
do int D

Rools ofe (eal and Same");


SOut
3
elseioo)
Sout ( Real aind dishnt )

else
Roots ose
sout

4
int doy
switch Cday)

Sout SUNOAY "));

break
I/ hivenno a
month, print coresfnd ing month nane
Sconner sc =
new Sconner
Sysiemin)
SoutEnter a
monh numberi
nl mon

Suith mon)

bíeaky
ase1 Sout ( Feh")
bfeak',
Cse 3:Sot ( narch );
bienk
bíegky

bresk
Cose 6

case' 7

case'
Suteh keywod
condiiong
l test some vaiable lorese of
oY
Cases) and per lorm. tasks
test ondihons, we ve Swlteh keyword
A
is cimilar to, Iaadder i stetemenis
is
The aiesene b/w suitch Amdi
while Swikh vSeshe
VSe dielent vatiabler
Some yatigble. bYeak'
Another dill erence Switch reuirer
is
Statement

Loopsin aa
i Do wile Ioop
i) for each loop(prim orly use d for obj ets)

x 1 (2*1)
Sout (2
Sout ( *
2x2 (2*2));
(1*3))
Sout 2 x (2r4))
Sout 2xS (2*6));
31
32
64
int ch

il=Nit=1)
Soutnum)
3 pa

22
multiplicotion tolb le
Scomner Sc
mey_Scanner (System
Sout Enter in)
intnum sc. metInt);
1
Ror(int i= it=1){
SOutnum

J| Series SUn
int
lorCint. i 1
it )
SUm

=
Ses ies Sym):

{orcint i= mnum
i=1
goct
1

2) 2)S 2

lo

nt bin O.

$iing
for tint
While (n>o)
d
int
= +
bin (bin o) dj

3
=
sou\ CBinaiy umber
6
S

2.8
CALSnI-30o.
nt num =3us6
int
SUm = O

Ror Cint i nun

Sum

s out (" Sum o


od digts
Su m)

a
I/ Reverse nymber
int 3456.
Strina s-"
or
(int i nun0

mum fev)
Sout ( ReNeise
=
SoutCReverse

Prit odd
orin i=1
3
3) 2
S2.

it=|)

SoutCS;

Rrint nmbers
not s
and 7 oml to whose
Lsdi
os Cint
il ikiooi )
Sout ();

/hiven no. identh


int
or
nUm=12 al iis
int count=o
actors

Cint i 1
is=ny m

it=)
3 COUnttt,

soyt Facto S
COvnt)
6

3s.r 2

mumbeyis primeor not


Int. num =I7)
ott int atos 9j numA,it+)
or
int i2 iKs
i(num i,== 0)
fattors +*

==0)
{factosSout ( Prine" )

else
Sovt (NGt Pine"):
3

Alteinatve
fo Cint i2iSE Matha
sat (oum);iti)
fators

Metiodto find pime numbexg


Sieve
moíe
EVen
eiclent method
4nt nwm
RorGint ie
Aotetboo
leons Prime= trve

Cnm
i<smoth. Sat(num)it)
breok
3
forter icisRrine)
sout ( Prime

else
9
6
Sout NotRine"
IPint prime numbeis fom

Ror (int
nUm=2; nym lo0 numt)
boolen isrime
=
true
af (inti= 2iK= Malh. Soyrt(num) it=){

isPrime galse,
break'

Sout (i)

o
To 1ün a set. iohe hends sefestedly far ixed
ho:
Basic uchuse is

Cinitalizaion; terminaion vpdathion )


Por
Condiion
Looping
can he done Porwaid and hackwnrds

wbie loop
Used when
(ondiion is
we

trUQ
nont to loop s long
sn gyen
Tt
is usedwhen
we do not know heorehand
how man
hmes the looe will YUn.
-
.A checks
he condithsn irst bejofe exeeuhing
ts code
blok,
o-while loop
,Similo to
while loop.
- The
koop
\ exe quled at least once
aonditgn, is checked ond then he
- I4
is
Used or meny
based progiamming
( breok' Statemem
T
is vsed to jvmp ovt
ol the \oop
immedinely mext stalement onto he
ofier it
Continue stalement
Aike bYeak,
ontinue statement also
cuYrent o
indersupls the
low he proiQm T
skies he
semainins code al1er it ond jumps
Herotion
to next the
he IooR

tings in Iava

Stin S2 new
gCA")
Shrin
Sting o
class In Jayq,TH hos
inbult {un Hons.
SOut
S1.lensth))}
Sot (s1. chorfAiC)
characterot inae ti

Sout (SI t'S) 1 Concolen ote


Sout AMVBA
(S1, concat ($2))

Sout (s1, eavals (S2))

Sout (s1compofe To(s1))

SL.Compaie Tols2)

5Zeio both -ae eoguaL)

Si. compaie To Inore (ase(2)


Comearess.and but iS no 9se Sensitit
MOe oten used

a
Shring S
seauente choraters

class
s
Sych lengthh At(1
6 6-9!
32 A723S
48-570igit
13 o)

Java afe n nu tatsle


be changed once whh..means hey cont
hesxe cested
Shringst ANNU
String strn= BCA

StrinnesirStrlconcat (6t2)
sout(wSh)

Checkshings are
esjual.

Soutye");
else
SoutNO")

l Cont hsbets nd digis


Scanne sc new 3oner Sysen in
Sting Somple s Sc.nextK),
Int OUntha=O
orCint..0 (Somple
\eghs )
Samplehsc AtCISS)M(
Stugple
choAt (1)
som9744
else i C) )-u8
s. chor At
a4 s.charft i) (=
cOUntdigtt

Sout " ALphab es = Countalpha


sout( Digiis

/Aevese String
Gting
int ze =
tr. length)
= Str. to Char ArrayO
chor C
o1

Por(inti0 iK size /2)

Or (size -i-d= teme

thn Shina teveise ofr,totringO,


=
SoutAeveised string
cB

AFFAN
912e S

FFAA
NAFFA
nRevesse string
String
(orCint
K=f+ Str. hor AL)
Sout (e Rever se

AlternatiNe
er' -)
lorCint i0 Y
isie ) it )
St, Char AtCi)
public sontic boolean islar (Sthing str)
int sSize strlength0;
Sting
or Cint it size-I,i)o,i-)
14Str, chof Ati)

R(st. equalsr))
Neturn trve

Ietin alse ,

CisPal( Hello ")!


Sout ( Palindfome "")

else
Sout ( NOT Pa\indíome")}
AFFA
NEF AA
AFA

CABSM-300|L
palindTome
Or.not
I/Checkwheher_o gien stingis
boolean islal trves
String str AecOCBA")
=
int sìZe str leng thO
lorlint i -0; ika(lze /2) it)
beclean iPal>ne
char str. char At())
cha b sr charAt(Si2e -i-)

isPal=alse
bíeak

Gcta)
Saut ( Palindsone "):
Selse
indrome
poa

Sout ( Noto

u
Students S7,6 9, 15, 67
Calculate the average

int J_ marks= 57, 69,S,675;


int su = 0
it counto",
or Cint:
i: marks)?

dovble =Um/Cdoube)count
double av
Cathtc+d)4.0
Fnd the larset number

Soutla)
else

int C1 {1,2,3, u,sl


new int L51y
Delining the
size
netthods
ofr.length

Rendita en stnag
lof Cimt
i0ierr <alengh it)
CilF SC. net
Int();
bgfe &bt

double
Mew double l5j
Sconner SC new Scanner C Sy stem in)

for(int ieoika. lengthi=)


SotEnter element.
yr il=sc. nextDouble 2r

orldouble i ar)
Sout(i

arrqy index ih element >


base +
(dxi)
d size d detn tyRe

atray lements
I/ Sum r al
int new int Lio;
sc ( System n)
PortinScanner newScönner

or Cint
i0
Sovi ( Enter elem ent
+
i+)

3
to Cint eL: o)

Sout Su m =
sum

/ Check {ist and. lost element aie sane


int ll rr 10
0,30
int Size =
air.length ho0

Sout ( Same"):
else
sout (otdi eient),

la elements
int Size= aT.lenth

temp

Prist only odd elements


int ,
QiL 2, 3, 5, 5,1,8
int len ar- length
olinti:0,is len it)i
Car ciy 2
}o)

| Cavnt odd
and
=
even in an ar
int COUnte ,
intC] SIY =Li,23y, :3
Rorlint eli TY)

CoUntett

else
coUnt-O tt)

Sovt count~e)
Sout Ccount-,
MOximum element an aray
/Flnd
int max ar Co3;
Jor Cinte arr)?
3
int C] atr=S6 78 45, 343)
Ant min

lorCint ei arr)
e
min

You might also like