Java Reference
In-Depth Information
< Day Day Up >
Puzzle 20: What's My Class?
This program was designed to print the name of its class file. In case you aren't familiar with class
literals, Me.class.getName() returns the fully qualified name of the class Me , or
"com.javapuzzlers.Me" . What does the program print?
package com.javapuzzlers;
public class Me {
public static void main(String[] args) {
System.out.println(
Me.class.getName().replaceAll(".", "/") + ".class");
}
}
Solution 20: What's My Class?
The program appears to obtain its class name ( "com.javapuzzlers.Me" ), replace all occurrences of
the string "." with "/" , and append the string ".class" . You might think that the program would
print com/javapuzzlers/Me.class , which is the class file from which it was loaded. If you ran the
program, you found that it actually prints ///////////////////.class . What's going on here? Are
we a victim of the slasher?
The problem is that String.replaceAll takes a regular expression as its first parameter , not a
literal sequence of characters. (Regular expressions were added to the Java platform in release 1.4.)
The regular expression "." matches any single character, and so every character of the class name
is replaced by a slash, producing the output we saw.
To match only the period character, the period in the regular expression must be escaped by
preceding it with a backslash ( \ ). Because the backslash character has special meaning in a string
literal— it begins an escape sequence— the backslash itself must be escaped with a second
backslash. This produces an escape sequence that generates a backslash in the string literal. Putting
it all together, the following program prints com/javapuzzlers/Me.class as expected:
package com.javapuzzlers;
 
 
Search WWH ::




Custom Search