0 ratings0% found this document useful (0 votes) 10 views6 pagesData Type in Java
Java is a programming language.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Data Types in Java
Data types specify. the different sizes and values that can be stored in the variable, There are two
types of dota types in Tava
1. Primitive data types: The primitive data typey inchide lotewer, Chatacter, Booleinn, and
Floating Point
2. Nonsprimitive data typest Te mdeepHimtive data lypes inchite Chases, tnterte
Arrays.
san
Java Printitive Daw
Ten Java Lani
Types
primitive daa type
frndst Base days types avi
ane the Building Docks of dita Manipulation ‘Teve ire the
iy Java Language
isa simionlly-typed programming language. Lt means, alt variables
ese, That bs why We nee! to. deelare vigriable’s tye utd hamne
There are 8 types of primitive data types:
+ boolean datatype
# bytediata type
chur data type
mist be declared heft
short data type
int daratype
© long ’data type
© float datatype
double data type
Dati Type | Detail’ Walue | Default size
Boolean | take
char *woou"
bie [0
short 7
int. o
Tong ob
Tioat BOF
oui 0d
Boolean Data Type
The Boolean data type is-used to store only two posstisle values: true and filse This dats type ix
ised for simple flags that track true/false conditiuns,
The Boolean datw type specities one bit of information, but its "size cant be defined precisely
Example: Boolean one
Byte Data Type
‘The byte dita type is an exaunple of primitive dasa type, Itisan 8-bit signed two's complement
integer Its valte-range lies between -128 to 127 (inélusive) Hs minim value i128 aid
masiotan value is 127; Nts defnult valu Ts 0
‘The byte data type ix used to save memniory in large umay's where the themory savinas Es must
tequired. Insaves spwee because y byte is 4 thnes smaller thai an integer. It can also be used in
pice af "int data type
Example: bytea=10, byte b= 20# Variables
SA varioble iy w avmmainiee which hokdx the wate Wille the va peolgtam ts executed, A yariahile ie
assigned! with » datanype.
Vatinble is a name ofremory Iocalldn! There are thie yes of viifiblés tn java: losals instance
ate mtatic
There are/oxo types of dati type ih java’ pritnitlve and niin-phimnitive
Variable,
Variable is nti of'reseridd ured allocated in imeinory tw olher words, it itu naner af memery
focarion, Iisa combihatiirs of "slury + able that mate ite value ean be changed.
1. int datas 0,//Mete data is vartable
‘Types of Variables
here are thee types of wartabe in px
© local variable
© instance variable
© static variable
1) Local Variable
‘A vakiable declared inside the body af the method is called!local yartable. You ean use this variable
nly within that method and the other methods in the ¢lass aren't even aware that the vanable
exists,
A local variable cannot be defined with © Mate" keyword,
2) Instance Variable
‘A variable declared inside the class bur outside the body of Use inethod. iv called inmatice variable
It is not declared as static.
Itis called instance: variable because its value Is inwtunce specific. and is:not shined among
instances.
3) Stutic variable
Avvarioble which is declared as static ts called suite varlable. It Gantiot be lool You can ending a
single copy of static variable and share amor all the instances af the class. Memory alloeitivn lor
static variable happens only ance when the claas iv fouled! the memory
Example to understand the types of variables in jay
elasa ‘Al
drt date=S0;//inst
Static Ent mel00s//;
void method (0 (
int n=$0://ioaed variable
'
\ftend/ot class
Java Variable Example: Add Two Numbers
lass Stmpley
pblic static vole main (ételngt{ args! |
tnt 10;
ant b=207
aint c=sebrutp
=
Java Variable Example: Widening,
id main (Striogt
Quipor
100
Java Variable Example: Narrowing (Typecasting)
ste lind
Output:
tos
wv
Java Varinble
mt
imple: Owvertor
class
be (byte}as
-eut.
Lem sOUE «Fi
Ouynit
iyo
“134
Java Narkible Example: Adding Lower Type
class Simple
Pubic static void =ainistring!} ange
Jibyte cease //
bre. colbyce) faspr
tie Tie Tres
Because atmByaten,est.pelitinicl
'
‘
Ouueputt:
oH
Unicode System,
Ufiicodle is a universal Interhationdl standard chanatter encoding thar is cupable of representing
most of the world’s written Ianguages.
Why java uses Unicode System?
Before Unicode, there were many lange standards:
© ASCH (American Standard Code for taformation Titershange) forthe Linited States:
¢ ISO 8859-1 for Western European Language.
* KROES lor Russinn.
+ GB18030 and BIG-S for chinese, anid/so on!
Problem
“This chused two problems:
1. A particular code value corresponds to different eters in io vais laslunge standards
2: The envodtings for tanguages with large churwoter 4éts halve variuble leagth, Some somman
chiracters are encoded as single bytes, other require (Wo or more byte
Solution
‘To volve these problems, anew Jangiuaye suandard wats developed be. Uitieadie Systerh,
In Unieodle, character holds 2 byte, s6 jovi ilo tases. 2 byte fon characters:
owest value: 0000
highest valueruEFPE
| Unicode iy.am internat ital efter te
rove a sine character set then everyone sm ans: cite
supports the characters needed foe Fnylial, Arabic Tinie Cynitle: Greek Devanauant (ity ain
many otliers, Java is, ond wf the first peodramading Matipiingits to explicitly addreny tbe peed fir nan
Epelish text, Kedoes this by. avoptinie Uinieasle ws (6 anne characte set. Ut Janu shiorw anal nferes
ard aiven In Unicode: At the time of Java's ereatiot. Linivindi tssjuireil 140 t Fives. intywvy chan ve
awI6sbitiwe-byte) type. The range ol a clsar ih Oh O5-S36> Whthe ane no meyn
i designed 10 allow programs fo be written Ube worklintde Use, it mlhcy seme thtit asi se
“inieinde to represent chaneters. The ups al Khboode 4s somewhat bie Mbcline fine lathes set 8
“Bigllah, Germsei Spanish. or Erenchy, whone chivrachees ei balls se cunitainent Synies bite Lut