Professional Documents
Culture Documents
JavaBasicDatatypes
JavaBasicDatatypes
https://www.tutorialspoint.com/java/java_basic_datatypes.htm
Copyrighttutorialspoint.com
Variablesarenothingbutreservedmemorylocationstostorevalues.Thismeansthatwhenyoucreateavariableyou
reservesomespaceinthememory.
Basedonthedatatypeofavariable,theoperatingsystemallocatesmemoryanddecideswhatcanbestoredinthe
reservedmemory.Therefore,byassigningdifferentdatatypestovariables,youcanstoreintegers,decimals,or
charactersinthesevariables.
TherearetwodatatypesavailableinJava
PrimitiveDataTypes
Reference/ObjectDataTypes
PrimitiveDataTypes
ThereareeightprimitivedatatypessupportedbyJava.Primitivedatatypesarepredefinedbythelanguageandnamedby
akeyword.Letusnowlookintotheeightprimitivedatatypesindetail.
byte
Bytedatatypeisan8bitsignedtwo'scomplementinteger
Minimumvalueis128(2^7)
Maximumvalueis127(inclusive)(2^71)
Defaultvalueis0
Bytedatatypeisusedtosavespaceinlargearrays,mainlyinplaceofintegers,sinceabyteisfourtimessmaller
thananinteger.
Example:bytea=100,byteb=50
short
Shortdatatypeisa16bitsignedtwo'scomplementinteger
Minimumvalueis32,768(2^15)
Maximumvalueis32,767(inclusive)(2^151)
Shortdatatypecanalsobeusedtosavememoryasbytedatatype.Ashortis2timessmallerthananinteger
Defaultvalueis0.
https://www.tutorialspoint.com/cgibin/printpage.cgi
1/4
10/19/2016
JavaBasicDatatypes
Example:shorts=10000,shortr=20000
int
Intdatatypeisa32bitsignedtwo'scomplementinteger.
Minimumvalueis2,147,483,648(2^31)
Maximumvalueis2,147,483,647(inclusive)(2^311)
Integerisgenerallyusedasthedefaultdatatypeforintegralvaluesunlessthereisaconcernaboutmemory.
Thedefaultvalueis0
Example:inta=100000,intb=200000
long
Longdatatypeisa64bitsignedtwo'scomplementinteger
Minimumvalueis9,223,372,036,854,775,808(2^63)
Maximumvalueis9,223,372,036,854,775,807(inclusive)(2^631)
Thistypeisusedwhenawiderrangethanintisneeded
Defaultvalueis0L
Example:longa=100000L,longb=200000L
float
Floatdatatypeisasingleprecision32bitIEEE754floatingpoint
Floatismainlyusedtosavememoryinlargearraysoffloatingpointnumbers
Defaultvalueis0.0f
Floatdatatypeisneverusedforprecisevaluessuchascurrency
Example:floatf1=234.5f
double
doubledatatypeisadoubleprecision64bitIEEE754floatingpoint
Thisdatatypeisgenerallyusedasthedefaultdatatypefordecimalvalues,generallythedefaultchoice
Doubledatatypeshouldneverbeusedforprecisevaluessuchascurrency
Defaultvalueis0.0d
Example:doubled1=123.4
boolean
https://www.tutorialspoint.com/cgibin/printpage.cgi
2/4
10/19/2016
JavaBasicDatatypes
booleandatatyperepresentsonebitofinformation
Thereareonlytwopossiblevalues:trueandfalse
Thisdatatypeisusedforsimpleflagsthattracktrue/falseconditions
Defaultvalueisfalse
Example:booleanone=true
char
chardatatypeisasingle16bitUnicodecharacter
Minimumvalueis'\u0000'(or0)
Maximumvalueis'\uffff'(or65,535inclusive)
Chardatatypeisusedtostoreanycharacter
Example:charletterA='A'
ReferenceDatatypes
Referencevariablesarecreatedusingdefinedconstructorsoftheclasses.Theyareusedtoaccessobjects.These
variablesaredeclaredtobeofaspecifictypethatcannotbechanged.Forexample,Employee,Puppy,etc.
Classobjectsandvarioustypeofarrayvariablescomeunderreferencedatatype.
Defaultvalueofanyreferencevariableisnull.
Areferencevariablecanbeusedtoreferanyobjectofthedeclaredtypeoranycompatibletype.
Example:Animalanimal=newAnimal("giraffe")
JavaLiterals
Aliteralisasourcecoderepresentationofafixedvalue.Theyarerepresenteddirectlyinthecodewithoutany
computation.
Literalscanbeassignedtoanyprimitivetypevariable.Forexample
bytea=68;
chara='A'
byte,int,long,andshortcanbeexpressedindecimal(base10),hexadecimal(base16)oroctal(base8)numbersystems
aswell.
Prefix0isusedtoindicateoctal,andprefix0xindicateshexadecimalwhenusingthesenumbersystemsforliterals.For
example
intdecimal=100;
intoctal=0144;
inthexa=0x64;
StringliteralsinJavaarespecifiedliketheyareinmostotherlanguagesbyenclosingasequenceofcharactersbetweena
pairofdoublequotes.Examplesofstringliteralsare
Example
https://www.tutorialspoint.com/cgibin/printpage.cgi
3/4
10/19/2016
JavaBasicDatatypes
"HelloWorld"
"two\nlines"
"\"Thisisinquotes\""
StringandchartypesofliteralscancontainanyUnicodecharacters.Forexample
chara='\u0001';
Stringa="\u0001";
JavalanguagesupportsfewspecialescapesequencesforStringandcharliteralsaswell.Theyare
Notation
\n
Characterrepresented
Newline(0x0a)
\r
Carriagereturn(0x0d)
\f
Formfeed(0x0c)
\b
\s
Backspace(0x08)
Space(0x20)
\t
tab
\"
\'
Doublequote
Singlequote
\\
backslash
\ddd
Octalcharacter(ddd)
\uxxxx HexadecimalUNICODEcharacter(xxxx)
WhatisNext?
Thischapterexplainedthevariousdatatypes.Thenexttopicexplainsdifferentvariabletypesandtheirusage.Thiswill
giveyouagoodunderstandingonhowtheycanbeusedintheJavaclasses,interfaces,etc.
https://www.tutorialspoint.com/cgibin/printpage.cgi
4/4