Beruflich Dokumente
Kultur Dokumente
A:
Key words are reserved words. but not vice versa. See Java Language Keywords for
details.
• You cannot put your hands on an Object without through its reference in Java. In
C/C++, you can.
• In C/C++, you can do arithmetic on pointers, but you cannot do arithmetic on
references in Java.
• In C/C++, you can dereference a pointer, you cannot dereference a reference in
Java.
• In Java, all object are put on the heap only. In C/C++, you can put an
Objecty/struct onto the stack.
• In C/C++, you can cast pointer to an totally defferent type without compiler
errors. In Java, you cannot, Java is more strong typed language.
• In C/C++, pointer can point to primitive types too. In Java, reference cannot
reference a primitive type, unlees the wrapper class is used.
• In C/C++, pointer can point to another pointer, in Java, reference cannot
reference another reference.
Q. What modifiers should be used for main method? Why could I violate the rule,
and it still works?
A:
The official modifiers and signature for main method is
You should remember it, and use this for your SCJP test.
However, if you miss public or even use private instead, some JVM might let you launch
your Java application without complaining or error messages. Why?
Because some JVMs just make themselves more tolerant then they should be. This kind
of practice is very common in Microsoft world. I guess Sun learned something from there.
Take a look at stock price movements of both co. on 2001, you will probably understand
why.
What will happen if I omit static modifier for main method? Should I do that?
Q. What will happen if I omit static modifier for main method? Should I do that?
A: The code will compile, but not functional as you wanted (if you want
something normal, of course).
The main() method is supposed to be the entrance of the entire application.
However, if you omit static, it is just another instance method, happen to be called main.
It will not work as you expected. It will make yourself and your coworker confused. also
make yourself confused. It will become maintenance hazard.
Something is legal does not mean it is recommanded!!!!!! Don't trouble troubles until
trouble troubles you!!!!
Q. How does Java compiler resolve the ambiguity to decide which methods to
call?
A:
In the following example, four test() methods, if we pass ambiguous \b{null} to the test,
which one should (will) be called? The 3 on top has super/subclass/sub-subclass
relationship. The most specific one (down hierarchy) will be called. The 4th with String as
a parameter, but it is a sibling of Tester. Ambiguity compile time error results.
class Tester {
void test(Object s) { System.out.println ("Object version"); }
void test(Tester s) { System.out.println ("Tester version"); }
void test(SubTester s) { System.out.println ("SubTester version"); }
lblc:
if (o instanceof Object) {
while (true) {
n++;
if (n > 5)
break lblc;
System.out.println(n);
}
}
System.out.println("Finished n");
lblx:
for (int i = 3; i < 7; i++){
// skip number 5
if (i==5)
continue lblx;
System.out.println(i);
}
break lbly;
// compilable Error: statement not reached
//System.out.println("This will never be printed out.");
}
}
}
Constructors look a little strange because they have no return type, not even void. This
is because the implicit return type of a class' constructor is the class type itself
It makes sense. Just think of this:
MyObj o = new MyObj();
Q. Is null the same as void in Java? Can I return null in a void return type
method?
A: No! Another No!
void means return nothing. null in Java means it is a reference type, but refers nothing.
null and void are very different things. For further interest, read here.
Q. What is the difference of null in Java and NULL in C++?
But if you insist, figure it out! I guess it can be done, and here is something which
might help:
Q. How to create my own package, how to call my class in another package?
Q. What are the rules to name a Java file without public class defined in it?
A:
Everyone knows one Java file only can contain one public class, and the file must be
named as the name of the public class name. But how about Java file with no public
class?
No restrictions on name of Java file without public class defined. If you have a class with
a main, you'd better name the file with that class name for your own convenience.
However, this is not required. Test this out!
// D.java
class A {}
class B {}
class C {}
class E {
public static void main(String[] args) {
System.out.println("Strange thing in E.");
}
}
class F {
public static void main(String[] args) {
System.out.println("Strange thing in F.");
}
}
It is called D.java. You can run it in 2 ways:
java E //output: Strange thing in E.
java F //output: Strange thing in F.
If you think carefully, there are good reasons for this. Testing is one of them.
Sample code:
import java.io.*;
class Redirection {
public static void main(String args[]) throws IOException {
PrintStream pos = new PrintStream(new FileOutputStream("applic.log"));
PrintStream oldstream=System.out;
System.out.println("Message 1 appears on console");
System.setOut(pos);
System.out.println("Message 2 appears on file");
System.out.println("Message 3 appears on file");
System.out.println("Message 4 appears on file");
System.setOut(oldstream);
System.out.println("Message 5 appears on console");
System.out.println("Message 6 appears on console");
}
}
In java, only char is unsigned, others like byte, short, int, long are all signed. In other
languages like c/c++, all other types have a unsigned brother, char too. All are default to
signed except char type is implementation dependent.
For the first one, use your scientific calculator to do a little math. You can use the
following to understand it too.
1111 1111 1111 1111 + 1 = 1 0000 0000 0000 0000 = 216
Q. The size of long is 64bits and that of float is 32 bits. then how it is possible
long to float without type casting it?
eg.
long l=12345678;
float f = l; // ok to compiler
A: Interesting question!!!
The internal representation of integer types (byte, short, char, long) and floating point
number (float, double) are totally different. Converting from long to float is not a
narrowing conversion. Even it may lose some significant digits. See the following table for
details:
default value
# of
type range when it is a class
bits
member
8, 1 bit
Boolean true false false
used
+
double 64 -1.79769313486231570E+308 0.0
1.79769313486231570E+308
are assignments to floating numbers. The difference between 1.3d and 1.3f is the
precision, not the range. 1.3 is default to double precision number, assigning it to a float
will lose precision. It will need a cast!
• Member variables (both static and instance) are initialized implicitly by default:
1. Most primitives except boolean are default initialized to zero, not null!
2. boolean variables are default initialized to false, not zero, not null!
3. Only object references are default initialized to null!
4. Final varibles must be initialized explicitly in declaration or constructors
(instance final variable.)
• Local varibles are not initialized
1. They are not default intialized to anything, unless programmer initialize
them explicitly!
2. It is not compilable if you use them before assign them a value!
Q. Why byte to char, or float to long are narrow conversion defined by JLS?
A:
class My{
static long l;
static float f;
static char c;
static byte b;
public static void main(String[] ss) {
b=c; //not compilable
c=b; //not compilable either
The float to long should be more obvious, since its decimal part will be lost.
To prove this theory, run the following program TestBoolSerialize.java. The output file
Bool.tmp and Byte.tmp will be the same size, but Short.tmp will be double the size. You
may need to run 1 section a time if you run out your memory.
Don't need to worry too much about internal representation is one beautiful thing of Java.
The programmer don't need to actively allocate/return memory like in c/c++. Don't you
remember the pain of using sizeof() in c/c++, because of the pointer arithmetic, etc?
There is no sizeof() in Java. My above program TestBoolSerialize.java actually does the
trick of sizeof() to reveal the secret of Java for you.
class A {}
class B extends A {}
1. Upper cast from subclass to supercalss, no need to cast, since the ISA
relationship. OakTree is a Tree.
2. Down cast from superclass to subclass, an explicit cast is needed to avoid compile
time error. If you cast Tree to OakTree, it would possibly be right, but not
necessarily, since this particular Tree actually might be an AppleTree. In that
case, a ClassCastException will be thrown out at the run time
3. If you cast between siblings or even irrelevant classes, a compile time error
results, since compiler has the type information of them. for example, if you cast
a OakTree to an AppleTree, or even a Furniture, compile will error out.
4. However, there is a catch, if you cast to an interface, even they are irrelevant,
and compiler might let you go without an error.
When you cast super to sub, compiler assume that you know what you were doing, since
it is possible. However, runtime will find out b actually is not a Sub.
Since Cat is an Animal, but Animal is not necessary a Cat, but it is possible a Cat.
That is why downcast is allowed on compile time, since compiler assumes you know what
you are doing. However, Runtime will find out this Animal is actually not a Cat.
Q. What are the differences between char and byte? Why text file can be read
back as byte streams?
A:
1. Everything in computer are represented by 0's and 1's, every eight 0's and 1's
can form a byte, that is why everything can be read in as byte streams.
2. byte is the smallest memory unit addressable.
3. How to interpret those bytes is the job of operation system, software, etc.
4. For Java primitive types, char is 16 bits unsigned integer, byte is 8 bits signed
integer, short is 16 bits signed integer.
5. The difference between char and short is the interpretation of the first bit.
// output: 3
// think why?
A:
Remember the following will help: