|
How can I program linked lists if Java doesn't have pointers? |
Ans:) Of all the misconceptions about Java, this is the most
egregious. Far from not having pointers, in Java, object-oriented
programming is conducted exclusively with pointers. In other
words, objects are only ever accessed through pointers,
never directly. The pointers are termed "references" and they
are automatically dereferenced for you.
Java does not have pointer arithmetic or untyped casting.
By removing the ability for programmers to create and modify
pointers in arbitrary ways, Java makes memory management
more reliable, while still allowing dynamic data structures.
Also note that Java has NullPointerException, not
NullReferenceException.
A linked list class in Java might start like this:
public class LinkedList
{
public LinkedList head;
public LinkedList next;
public Object data;
public LinkedList advanceToNext(LinkedList current) { ...
}
}
|
| |
| what is the other way to create linked lists in java? |
Ans:) Another choice for a linked list structure is to use the
built-in class java.util.Vector which accepts and stores
arbitrary amounts of Object data (as a linked list does)
and retrieves it by index number on demand (as an array does).
It grows automatically as needed to accommodate more elements.
Insertion at the front of a Vector is a slow operation compared
with insertion in a linked list, but retrieval is fast. Which is
more important in the application you have!.
Note: java.util.Vector is thread safety.. so it is slow.
If you are not worried about thead safety, it is better to user
java.util.List.
|
| |
| Can I compile group of java files once? |
Ans:) The first way is
javac *.java
Another way is
javac -depend tip.java
where "tip.java" is a class "at the tip of the iceberg",
i.e. that depends on (uses) all the other classes.
Typically, this may be your main class. However, "-depend" is
known to be buggy and cannot be relied upon. It also doesn't
issue compile commands in parallel to make use of multi-processor
systems.
|
| |
| What language is the Java compiler and JVM written in? |
Ans:)Sun's javac Java compiler is written in Java. Sun's java JVM
interpreter is written in Java with some C, C++ for
platform-specific and low level routines.Sun's Java Web Server
is written in Java.
|
| |
| Is there any limit to the length of an identifier? |
Ans:) Yes; 65,535 characters is the maximum possible identifier
length, because identifier names must be representable in
Java class files.
The Java language in principle does not limit the length of
identifiers. According to The Java Language Specification (p. 6)"An identifier is an unlimited-length sequence of Unicode letters
and digits, the first of which must be a letter." However,
Java source code is compiled into Java class files, and the
specification for class files does, in effect, place an
upper bound on the size of identifiers. This limit is well
beyond the practical needs of human codes, but it
is interesting to understand where it comes from.
|
| |
| How do I compare two Strings? |
Ans:) The comparison using "==" on objects, such as Strings,is asking,
"Do these two objects have the same reference?"
Do they have the same address, and hence are the same object?
What you really want to do isask, "Do these two Strings have
the same *contents*?"
Compare String contents with any of the following:
if (s1.equals(s2) )
if (s1.equalsIgnoreCase(s2) )
if (s1.startsWith(s2) )
if (s1.endsWith(s2) )
if (s1.regionMatches(s1_offset, s2, s2_offset, length) )
if (s1.compareTo(s2) < 0)
(There are other ways, too.)
Note that you can do this with literals:
if ("apple".equals(s2) ) ...
If you compare these the other way round, like this:
if ( s2.equals("apple") ) ...
and s2 is null, you will get a null pointer exception.
|
| |
| How do I convert strings to numbers? |
Ans:) You can convert strings into numbers using the Integer,
Float, Double and Long type wrapper classes as indicated
by the following code snippet:
class ConvertTest
{
public static void main (String args[])
{
String str;
str = "25";
int i = Integer.valueOf(str).intValue();
System.out.println(i);
long l = Long.valueOf(str).longValue();
System.out.println(l);
str = "25.6";
float f = Float.valueOf(str).floatValue();
System.out.println(f);
double d = Double.valueOf(str).doubleValue();
System.out.println(d);
}
}
There are no equivalent Short and Byte classes in Java 1.0.
There are in Java 1.1. For shorts and bytes use the
Integer class but use the byteValue() or shortValue()
methods instead.
|
| |
|