Java Reference
In-Depth Information
Youmayalsodeclareseveralvariablesofthesametypebyseparating
thevariablenameswithcommas,forexample:
intage2,age3,age4;
Atthetimethatavariableisdeclared,youcanalsoassigntoitavalue.
Thisiscalledthevariable's initialization .Thevalueassignedtothevari-
ableisprecededbytheequalsign,forexample:
floatradius=1.22;
InthisstatementwehavecreatedanumericvariableofJava'sfloat
typeandnameditradius.Thevariablewasinitiallysettoavalueof1.22.
Youmayalsodeclareseveralvariableswhileinitializingsomeandnot
others,forexample:
intvalA=1,val2,val3,val4=77;
Inthiscasethecommasseparatetheindividualvariables.However,
thesecomplicatedvariabledeclarationsarenotconsideredgoodpro-
grammingstyle.
Java strings
Programsfrequentlyusegroupsofcharacterscalled strings .AJavastring
isaclass,notaprimitivetype.TheJavaStringclassispartofthejava.lang
librarythatisdirectlyaccessibletoanyJavaprogram.Forthisreasonyou
cancreateobjectsoftheJavaStringclassasiftheywerevariablesofaprim-
itivedatatype.NotethatbecauseStringisaclassitbeginswithanup-
per-caseletter,whilealltheotherprimitivedatatypesin Table4-1 have
names that start with a lower-case letter.
Programmers note:
The Java String class is so often used in programming that we tend to
think of it as another primitive data type. The fact that “String” starts
with a capital “S” reminds us that it is a class.
A String object is used to represent a sequence of characters. In con-
trast with the primitive data types, once a string is created it cannot be
changed. A String object is declared and initialized much like any of the
primitive types. Double quotation marks are used to delimit the string; for
example:
String uName = “Minnesota State University”;
String ssn = “263-98-2233";
Search WWH ::




Custom Search