Sunteți pe pagina 1din 3

7/17/2016

JavaPrimitiveDataTypes.Size,RangeandDefaultValueofBasicDataTypes

csFundamentals.com
ProgrammingTutorialsandInterviewQuestions

Home
CProgramming
JavaProgramming
DataStructures
WebDevelopment
TechInterview

JavaPrimitiveDataTypes.Size,RangeandDefault
ValueofBasicDataTypes
JavaEightPrimitiveData
Types
DefaultValuesofJava's
PrimitiveTypes
References

JavaEightPrimitive
DataTypes
Java primitive data types are the
basic data types that are builtin
toJavalanguage.Adatatypeisa
classificationmechanismwhereby
itcanbeidentifiedthatwhatkind
of data is stored inside the
variable,andwhatoperationsitsupports.
Java provides a richer set of primitive or basic or builtin data types than other
languages like C and C++. There are eight builtin types supported by Java to
supportinteger,floatingpoint,character,andbooleanvalues.Allprimitiveorbasic
datatypesholdnumericdatathatisdirectlyunderstoodbysystem.
ThefollowingtablelistsallJavaprimitivedatatypes,theirstoragerequirementsin
bytesandthenumericrangetheysupport.
Table1:ListofJava'sprimitivedatatypes
Type

SizeinBytes

Range

http://csfundamentals.com/javaprogramming/javaprimitivedatatypes.php

1/7

7/17/2016

JavaPrimitiveDataTypes.Size,RangeandDefaultValueofBasicDataTypes

byte

1byte

128to127

short

2bytes

32,768to32,767

int

4bytes

2,147,483,648to2,147,483,647

long

8bytes

9,223,372,036,854,775,808to
9,223,372,036,854,775,807

float

4bytes

approximately3.40282347E+38F
(67significantdecimaldigits)
JavaimplementsIEEE754standard

double

8bytes

approximately 1.79769313486231570E+308
(15significantdecimaldigits)

char

2byte

0to65,536(unsigned)

boolean

not
precisely trueorfalse
defined*

* boolean represents one bit of information, but its "size" isn't something that's
preciselydefined.[Source:Sun'sdatatypetutorial]
It is important to note that Java does not support

Adsby Google

unsignedtypes.All int variantsaresigned.But char is InJavaProgramming


an exception it is unsigned and consumes 2 bytes in IntegerJava
memory. It stores 16bit Unicode UTF16 character. JavaByte
Type char to be unsigned seems logical because there
arenonegativecharacters.

DefaultValuesofJava'sPrimitiveTypes
Java primitive data types are initialized to some default values when they are
declared as class members. While programming in Java you declare and use
variablesattwoplaces.First,insideafunctionthosearelocaltothatfunction.And
second,asaclassmember.Whenavariableisdeclaredlocaltoafunctionitmustbe
initialized or assigned before its first use otherwise compiler reports an error
"variable<variablename>mightnothavebeeninitialized".Butwhenavariableis
declaredasaclassmemberorfield,itisnotalwaysessentialtoassignavaluetothe
member. In that case class members are initialized to some default values by the
compiler. However, leaving class members uninitialized is not considered a good
practice.
ThefollowingtableliststhedefaultvaluesforJava'sprimitivedatatypesshownin
Table1.
Table2:DefaultvaluesofprimitivedatatypesinJava
http://csfundamentals.com/javaprogramming/javaprimitivedatatypes.php

2/7

7/17/2016

JavaPrimitiveDataTypes.Size,RangeandDefaultValueofBasicDataTypes

Type

DefaultValue

byte

short

int

long

float

0.0f

double

0.0d

char

'\u0000'

boolean

false

Stringorotherobject

null

LastWord
InthistutorialwediscussedJava'sprimitiveorbasicdatatypestheirdefaultvalues
and range. Hope you have enjoyed reading this tutorial. Please do write us if you
have any suggestion/comment or come across any error on this page. Thanks for
reading!

References
1.CoreJavaVolumeIFundamentals
2.Java:TheCompleteReference,SeventhEdition
3.PrimitiveDataTypes
4.StaticallyTypedLanguages

4Comments

http://csfundamentals.com/javaprogramming/javaprimitivedatatypes.php

Sortby Oldest

3/7

S-ar putea să vă placă și