Java Reference
In-Depth Information
the two strings are identical or not, use the method equals(str) with a string
argument str . Considers for example, the following sequence of instructions:
String s1="java";
String s2="JAVA";
System.out.println(s1.equals(s2));
We get false on the console output since Java is case-sensitive : That is, upper
cases are considered different from lower cases in Java.
We can access the character of the string at position k
1 by calling the method
charAt(k) . Again, indexes of characters range from 0 to the string length minus
one. For example, consider the following code:
String s= "3.14159265";
System.out.println(s.charAt(1));
Then the executed program writes the separation “.” on the console. We can
assign a character variable with that string character as follows:
char c=s.charAt(1);
5.6.4 Comparing strings: Lexicographic order
Before comparing strings of various lengths, we first need to define what is
meant by the comparison of two elementary characters. The “distance” between
two characters is defined as the span in the ASCII 3 code table. Since Java is
case-sensitive, lower cases are different from upper cases (they have different
integer codes), and the same character appears twice in the ASCII table: once
for the upper case and once for the lower case. The following code demonstrates
the lower/upper case-sensitivity of Java:
Program 5.8 Lower/upper cases and ASCII codes of characters
char c1 , c2 ;
c1= 'a' ;
c2= 'z' ;
// Compare character code
if (c1 < c2)
{ System . out . println ( c1+ " is before " +c2 ) ; }
else
{ System . out . println ( c1+ " is after or equal to " +c2 ) ; }
int codec1=c1 ; // type casting/conversion
int codec2=c2 ; // type casting conversion
System . out . println ( "Code ASCII for " +c1+ ":" +codec1 ) ;
System . out . println ( "Code ASCII for " +c2+ ":" +codec2 ) ;
3 American Standard Code for Information Interchange (ASCII). See
AmericanStandardCodeforInformationInterchange(ASCII) . nf , a
encodes characters using two bytes for encoding many different language character
sets and kanjis. See UNICODE at http://en.wikipedia.org/wiki/Unicode
 
Search WWH ::




Custom Search