Lei Wu

Greenhorn
+ Follow
since Sep 22, 2003
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Lei Wu

I think the usage java.util.* will cost theoretically a little more time in compilation. For example, if you write code like this:

A compiler must take additional actions to make sure that only one package contains class MyClass. If class with the same name 'MyClass' appears in both packages, it will produce a compile-time error. So it may check out all the packages to ensure the unique appearance of class MyClass.
If it is written like:

No such extra actions will be required.
Of course this time cost is indiscernible.
Another, but more important, difference is that they have different precedence.
20 years ago


"A Java file without any class or interface definitions can also be compiled."


From JLS 7.3:
CompilationUnit:
PackageDeclaration(opt) ImportDeclarations(opt) TypeDeclarations(opt)

As above, since all the parts of a CompilationUnit in Java is optional, I think it is right to say so. Actually, JDK1.4.1 does treat an empty file as a valid compilation unit. You can even write like this:

It's also a valid compilation unit according to the definition.


"If an import statement is present, it must appear before any class or interface definitions."


Yes. you can see from the above definition that ImportDeclarations must appear before TypeDeclarations.

Originally posted by K. Ravisekhar Naidu:
HI, I have the follwoing code:
String s1="java";
String s2="java";
String s3=new String("java");
System.out.println(s1==s2);
System.out.println(s1==s3);
This prints 'true' and 'false' respectively. Any explanation for this??
Thanks
-Ravi


My answer:
Every string literal is an instance of class String, and only one is created if they are the same. So s1 == s2 is true, because they are both refering to the same instance of class String.
The line:

is actually calling class String's construct String(String s), so a new instance will be created. It is like:
String s = "java";
String s3 = new String(s);

Originally posted by Krishna Srinivasan:
can anyone explain me about System.out.println(); in detail

Advance Thanx.



I don't know what you really need, but you can read the java sources for more details. It should be obtained if you select to include java source when installing jdk.
I try to give some information here.
System.out is an instance of class PrintStream - you can find this in class java.lang.System. And class PrintStream has overloaded method println(), such as println(boolean x), println(char x)... Here is one of them:

You can see that it calls its own method print(char x). Here is the code:

It first convert character 'c to a string, and eventually write it out by calling method write(String s). Method write(..) are also overloaded, one version is:

textOut and charOut are the fundamental streams that do the real work.
You can see a chain of method calling from println() to print(), then to write(). All of these methods are overloaded, so System.out.println() can take different types of parameters.
Is this what you want?
To Jose Botella:

The cast "(Object[]) new int[][] {{}};" conflicts --to my knowledge-- with JLS 5.5


No, it doesn't conflicts with JLS 5.5.
Now, let's first look at what JLS 5.5 says:
<1> If S is an array type SC[], that is, an array of components of type SC :
<1.1> If T is an array type TC[], that is, an array of components of type TC, then a compile-time error occurs unless one of the following is true:
<1.1.1> TC and SC are reference types and type SC can be cast to TC by a recursive application of these compile-time rules for casting.
So, here SC[] is int[][], which is an array of int[], SC is hence int[]. TC[] is Object[], and TC is of type Object. Since int[] can be cast to Object at compile-time, it is correct to cast int[][] to Object[], according to the above rule. Actually, it's a narrowing reference conversion from int[][] to Object[].
It sure won't cause any compile-time error in line 4. You know, a.clone() returns an object, while an object can be cast to anything at least during compile time. No exception will occur at run time, too . As Steve said, a.clone is actually an array of int[], it can be assigned to an array of object. In fact, you can even write line 4 like this:
Object[] obj = (int[][]) a.clone();
It still works correctly.
Hope this help.