Friday, October 12, 2007

Java Faqs - Will Java Allow Viruses and Destructive Programs to be Transmitted Via the Net?

As we mentioned before, Java applets are built on AWT. AWT provides two levels of security. On the first level, AWT prevents applets from having free access to your computer’s file system. So, for example, it would be extremely difficult for someone to write a Java applet which did any damage to your computer’s files. The second level of security involves a byte code verifier in the Java virtual machine. The verifier checks to see if any of AWT’s security classes have been overridden or if anything in the applet will make the browser crash. If both conditions are met, it allows the non-offending applets to run in your browser. Otherwise, it rejects them. While a good concept, the verifier can be fooled; be careful.
By
Shantan Nethikar
9949040106

Java Faqs - How Fast is Java?

The real question is, “How fast is Java in relation to other programming languages?” The answer is, Java falls somewhere in between the speed of static languages such as C/C++ and dynamic languages such as SmallTalk. Preliminary benchmarks show that Java byte codes typically execute at about 50% of the speed of well written C code for processor intensive tasks. The bottom line is that Java is significantly faster than dynamic languages such as SmallTalk and interpreted languages such as Visual Basic. The secret to Java’s speed is the design of the virtual machine instruction set. The instruction set is close enough to most native CPU instruction sets that there is very little overhead in translating from Java byte codes to native instructions. Future versions of the Java byte code interpreter will be able to translate from Java byte codes to native machine code on the fly (also known as “just-in-time compilation”), which will result in even greater execution speed.
By
Shantan Nethikar
9949040106

Java Faqs - What are the naming conventions in Java?

The naming conventions are straightforward:
1 . Package names are guaranteed uniqueness by using the Internet domain name in reverse order: com.javasoft.jag - the "com" or "edu" (etc.) part used to be in upper case, but now lower case is the recommendation.2 . Class and interface names are descriptive nouns, with the first letter of each word capitalized: PolarCoords. Interfaces are often called "something-able", e.g. "Observable", "Runnable", "Sortable".3 . Object and data (field) names are nouns/noun phrases, with the first letter lowercase, and the first letter of subsequent words capitalized: currentLimit.4 . Method names are verbs/verb phrases, with the first letter lowercase, and the first letter of subsequent words capitalized: calculateCurrentLimit.5 . Constant (final) names are in caps: UPPER_LIMIT. Other sites:6 . Check out the section "Naming Conventions" in the language specification: 7. Also take a look at Doug Lea's draft coding standards:

Java Faqs - How can I store the errors from the javac compiler in a DOS file?

javac foo.java > errorfile doesn't work.

javac writes errors to stderr, The problem is that DOS doesn't allow stderr to be redirected (as command.com is very poor software). So you have to use a special error redirection mechanism in the compiler:

javac -J-D javac.pipe.output=true myfile.java > errors.txt

In JDK 1.2, you can use: javac -Xstdout You typically use this when a compilation produces a lot of errormessages, and they scroll off the DOS window before you can read them.

Alternatively, you can get a scollbar to appear on a DOS window by changing the properties with the "Layout" tab. Change the Screen Buffer Size Height: to some multiple > 1 of the Window Size Height. E.g. use a buffer height of 100 and screen height of 25 (the default). This will give you three buffers of scroll "history."

Java Faqs - How do I turn off the JIT in the JDK?

In JDK 1.1.x you use the commandline option "-Dnojit". In JDK 1.2/2 you use "-Djava.compiler=none"

One reason for turning off the JIT is to get more information about any exception that is thrown in your code.

By
Shantan Nethikar
9949040106

Java Faqs - What language is the Java compiler and JVM written in?

Sun's javac Java compiler is written in Java. Sun's java JVM interpreter is written in Java with some C, C++ forplatform-specific and low level routines.Sun's Java Web Server is written in Java.Other companies have chosen other approaches. IBM's Jikes compiler (which is praised for its speed) is written in C++, but of course each version only runs on one platform. Jikes versions have been prepared for IBM AIX, Linux Intel (glibc), Win95/NT and Solaris Sparc at http://www.alphaworks.ibm.com/formula/jikes.

Java Faqs - Can I compile group of java files once?

The first way isjavac *.java

Another way isjavac -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.Without the "-depend" option, the standard "javac files" doesn't look beyond the immediately adjacent dependencies to find classes lower down the hierarchy where the source has changed.The -depend options searches recursively for depending classes and recompiles it. This option doesn't help when you have dynamically loaded classes whose names cannot be determined by the compiler from the dependency graph. E.g. you use something likeClass.forName(argv[0]);The author of the code using those classes should make sure that those classes are mentioned in a Makefile.

By Shantan Nethikar

Java Faqs - What is the difference between jre and java?

They are functionally equivalent, with minor differences in the handling of default classpath and options supported. To reduce confusion, the jre command was removed in JDK 1.2. Instead there is a "java" command in both bin and jre/bin.jre.exe is the java launcher that comes with the Java Runtime Environment. It ignores the CLASSPATH environment setting in favor of its own internally generated default and whatever is supplied on the cmd line using -cp or -classpath. It's intended to be a bit simpler for those who are only ever running Java programs, not developing them.java.exe is the java launcher that comes with the JDK. It uses the CLASSPATH environment setting as a starting point and then tacks on its own internally generated entries.They both serve the same purpose and that's to start a Java VM, have it run a Java application, then terminate. The source for jre.exe is provided in the JDK. The source to java.exe is provided only in the JDK Source distribution.
By
Shantan Nethikar

Java Faqs - How can I program linked lists if Java doesn't have pointers?

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) { ...}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.

By

Shantan Nethikar

99490401096

Java Faqs - Does Java have pointers?

No, no, a thousand times no. Java does not have pointers, no way. Java does have references. A reference is an abstract identifier for an object. It is not a pointer. A reference tags a particular object with a name in the Java virtual machine so that the programmer may refer to it. How exactly the virtual machine implements references at the level of machine code is VM-dependent and completely hidden from the programmer in any case. Most VMs including Sun's use handles, not pointers. A handle is a pointer to a pointer. At the level of machine code in the CPU a reference is an address in memory where the address of the object is stored. This way the objects can be moved around in memory and only the master pointer needs to be updated rather than all references to the object. This is completely hidden from the Java programmer, though. Only the implementer of the virtual machine needs to worry about it. Indeed, this is not the only way references can be implemented. Microsoft's VM actually does use pointers rather than handles. Other schemes are possible.
By
Shantan Nethikar
9949040106

Java Faqs - What is the latest version of JDK? Is Java "Year 2000"-compliant?

Latest version is J2SE™ v1.3, still u can find updated at http://www.javasoft.com/
Java is Y2K compliant in release JDK 1.1.6 and later. See, Prior to this release there were certain corner case bugs that had to be fixed. The Date class, as you can see from the discussion, contains more than enough resolution to represent dates in this century and the next and the last. The SimpleDateFormat when parsing a 2 digit year could cause problems.

By Shantan Nethikar
Java Faqs

Java Faqs - What are the ways to learn Java?

Following steps makes easy to learn java.

1. Start with a java tutorial provided by sun.

2. And get basic book that covers all language fundamental, U can get several java book online too.

3. You can also get a compile from sun site to write and test sample programs.

Thursday, October 11, 2007

Java Faqs - What is Externalizable Interface ?

Answer : Externalizable interface is a subclass of Serializable. Java provides Externalizable interface that gives you more control over what is being serialized and it can produce smaller object footprint. ( You can serialize whatever field values you want to serialize)This interface defines 2 methods: readExternal() and writeExternal() and you have to implement these methods in the class that will be serialized. In these methods you'll have to write code that reads/writes only the values of the attributes you are interested in. Programs that perform serialization and deserialization have to write and read these attributes in the same Reference:

http://www.geocities.com/srcsinc/java/Interview_Questions/

Java Faqs - How do I deserilaize an Object?

Answer : To deserialize an object, perform the following steps:
- Open an input stream -
- Chain it with the ObjectInputStream
- Call the method readObject() and cast the returned object to the class that is being deserialized.
- Close the streams
Java Code
try{
fIn= new FileInputStream("c:\\emp.ser");
in = new ObjectInputStream(fIn);
//de-serializing employee
Employee emp = (Employee) in.readObject();
System.out.println("Deserialized " + emp.fName + " " + emp.lName + " from emp.ser ");

}catch(IOException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace(); }

Java Faqs - How do I serialize an object to a file ?

Answer : To serialize an object into a stream perform the following actions:

- Open one of the output streams, for exxample FileOutputStream

- Chain it with the ObjectOutputStream <- Call the method writeObject() providinng the instance of a Serializable object as an argument.

- Close the streams

Java Code ---------

try{

fOut= new FileOutputStream("c:\\emp.ser");

out = new ObjectOutputStream(fOut);

out.writeObject(employee); //serializing

System.out.println("An employee is serialized into c:\\emp.ser");

} catch(IOException e){ e.printStackTrace();

}

Java Faqs - What does the Serializable interface do ?

Answer : Serializable is a tagging interface; it prescribes no methods. It serves to assign the Serializable data type to the tagged class and to identify the class as one which the developer has designed for persistence. ObjectOutputStream serializes only those objects which implement this interface.
Posted By
Shantan Nethikar
9949040106

Java Faqs - What is serialization ?

Answer : Serialization is the process of writing complete state of java object into output stream, that stream can be file or byte array or stream associated with TCP/IP socket.

Posted By
Shantan Nethikar
9949040106

Wednesday, October 10, 2007

Java Faqs - Know About Java......& Java Faqs

Everything in Java is an object. An object is a collection of data and actions that make up a programming entity. A 'car' object, for example, might have some data (i.e. 'speed,' 'direction,' 'lights on,' 'current fuel,' etc.) and some actions it can take ('turn right,' 'turn lights on,' 'accelerate,' etc.). Objects provide a number of advantages when programming; the include the ability to hide what's going on 'inside' the object from other programmers, which is useful for writing code that others can work with but not easily mess up. However, the biggest advantage to programming with objects is that objects simply 'make sense'; they form the nouns and verbs that you use to write the 'story' of your program.

Our 'car' example would be (pseudo-) coded as follows:

public class Car { double speed; double direction; boolean lightsOn; double currentFuel;
public void turnRight(){ - code here to turn right - }
public void turnLightsOn(){ - code here to turn lights on - }
public void accelerate(double rate){ - some code here to accelerate - }
}