Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Java.lang.String
06-25-2008, 03:51 PM
Post: #1
Question Java.lang.String
Could anyone please tell me why we have to explicitly import
java.lang.String even if we have already given the import statement
import java.lang.*. What is the significance of doing so?
Find all posts by this user
Quote this message in a reply
06-25-2008, 04:03 PM
Post: #2
RE: Java.lang.String
All classes in java.lang packages will be imported by default by the compiler at compile time. So we don't need to import java.lang.String explicitly. In fact we don't need to import java.lang.*; itself.

Importing java.lang.* or java.lang.String, does not make any difference in runtime and may be for readability you can import java.lang.String in your program.
Visit this user's website Find all posts by this user
Quote this message in a reply
06-25-2008, 04:09 PM
Post: #3
RE: Java.lang.String
Hi thanks a lot for the reply. But I was told that there is some difference between importing java.lang.* and java.lang.String and that importing java.lang.* does not import the package String. Could u pls check again and let me know. Thanks again in advance.
Find all posts by this user
Quote this message in a reply
06-25-2008, 04:15 PM
Post: #4
RE: Java.lang.String
Importing or not importing java.lang package has no difference as all the classes of java.lang will be imported at compile time. Just try the following program. It does not import the java.lang.* or java.lang.String. Still it compiles and runs

[java]
public class StringTest {
public static void main(String args[]){
String str = "Hello World";
System.out.println(str);
}
}
[/java]
Visit this user's website Find all posts by this user
Quote this message in a reply
06-25-2008, 04:39 PM
Post: #5
RE: Java.lang.String
Ok Thanks a lot Albin. That was very kind of you. I tried the example. It is working indeed.
Find all posts by this user
Quote this message in a reply
06-25-2008, 04:44 PM
Post: #6
RE: Java.lang.String
You are welcome
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump: