As you have learned, the javac compiler examines your source-code files and produces the bytecode .class files. As your Java applets become more complex, it may become difficult for you to locate errors (bugs) within your code. To help you locate such errors, most Java development environments provide a special debugger program. In the case of the Sun's Java Developer's Kit, the debugger program is named jdb (for Java debugger). To provide jdb with more information to work with, the Sun's JDK provides special version of the compiler named javac__g. Sun designed the javac _g compiler for use with debuggers. Essentially, javacjg is a non-optimized version of the javac compiler which place tables of information within the bytecode that the debugger can use to track down errors. To compile a Java applet using the javac__g compiler, you simply specify the applet source-file named within the javac^g command line, as shown here
C:JAVACODE> javac „ g SomeFile.Java <Enter>
How To Optimize The Javac Output?
When you compare the performance of a Java program against that of a C/C++ program, you will find that the current generation of Java programs can be as much as twenty times slower than their C/C++ counterparts. This performance loss is mostly due to the fact that the browser musf interpret the Java bytecode and convert it into the computer's native code (such as a Pentium or Motorola-specific code) before the code can run. In C/C++, the code is in the processor's native format to begin with, so this time-consuming translation step is not required. Remember, however, that Java's generic bytecode allows the same Java code to run on multiple platforms.
The Java designers are working on various solutions to speed up Java. In the meantime, you can use the -O compiler switch with javac, which may increase the applet's performance. The -0 switch directs javac to optimize its bytecode by "inlining" static, final and private methods. For now, don't worry what "inlining" such code means other than it may improve your applet performance. Unfortunately, when you use inlining, you may increase the size of your bytecode file, which will increase the applet's download time. The following Javac command illustrates how you use the -0 switch
C:JAVACODE> javac -O MyFirst.java <Enter>
SYNNEX Most Frequently Asked Core Java Interview Questions Answers |
What Is The Difference Between Java Applets And Applications?
With Java, you can create two types of programs: an applet or an application. As you have learned, a Java applet is a program that executes from within a Web browser. A Java application, on the other hand, is a program that is independent of the browser and can run as a standalone program.
Because an applet is run from within a Web browser, it has the advantage of having an existing vindow and the ability to respond to user interface events provided though the browser. In addition, because applets are designed for network use Java is much restrictive in the types of access that applets can have to your file system than it is with non-network applications.
As you will, when you write a Java application, you must specify a main method (much like the C/ C++ main), which the program executes when it begins. Within the main method, you specify the functionality that your application performs. With an applet, on the other hand, you need to write additional methods that respond to events which are an important part of the applet's life cycle. The methods include init, start, stop, destroy and paint. Each of these events has a corresponding method and, when the event occurs, Java will call the appropriate method to handle it.
When you write your first Java programs, you can write them as if .they were applets and use the appletviewer to execute them. As it turns out, you can later convert your applet to an application by replacing your init method with a main method.
Can You Explain The Cs Option Of Java Interpreter?
When you develop Java using Sun's JDK, you normally compile your source by using the javac compiler. If no compile errors exist, you then run your application using the java interpreter. As a shortcut, you can specify the -cs command-line switch when you invoke the java interpreter. The java command, in turn, will automatically compile out-of-date (modified) source-code files for you.
By using the -cs switch, you can make changes to your source and immediately execute the java interpreter without having to manually run the java compiler yourself. The java interpreter knows which files it needs to recompile by comparing each file's modification dates against the corresponding class modification date. Normally, programmers use the -cs option when they have made a minor change to the source files and know that the files contain no compilation errors. The following command illustrates the use of the -cs switch
C: JAVACODE> Java -cs MyProgram <Enter>
What Is The Statements?
A Java program consists of instructions that you want the computer to perform. Within a Java applet, you use statements to express these instructions in a format the Java compiler understands. If you are already familiar with C/C++, you will discover that Java statements are very similar. For example, the following statements produce a programs which prints the words "Hello, Java!" in applet window
import java.applet.*; import java.awt.Graphics;
public class hello_java extends Applet
{
public void paint(Graphics g)
{
g.drawstring("Hello, Java!", 20, 20);
}
}
What Is Style And Indentation?
As you write Java programs, you will discover that you have considerable flexibility in how you line your statements and indent lines. Some editor programs help you line up indented lines and indent new block. If you have already programmed in another language, you probably have your own style of indentation. Java does not impose any specific style, However, you may want to be consistent with your own style and always be conscious that a well-formatted program is easier to read and maintain. For example, the following two programs function equivalently. However, one is much easier to read than the other
import java.applet. * ;
public class am_i_readable extends
Applet{public void init()
{
System.out.println("Can you guess whatI do?");
}
}
import java.applet.*;
public class am__i_readable extends Applet
{
public void init()
{
System.out.println("Can you guess what I do?");
}
}
What Is The Program Compilation Process?
When you create programs, you will normally follow the same steps. To begin, you will use an editor to create your source file. Next, you will compile the program using a Java compiler. If the program contains syntax errors, you must edit the source file, correct the errors, and re-compile.
After the program successfully compiles, the compiler generates a new file, known as bytecode. By using a Java interpreter, or appletviewer, you can execute the bytecode to test if it runs successfully. If the program does not work as you expected, you must review the source code to locate the error. After you correct the error, you must compile the source code to create a new byte code file. You can then test the new program to ensure that it performs the desired task. This illustrates the program development process.
What Is Java Literals?
Literals correspond to a specific value in your Java program. For example, if you type the number 7 (the literal number 7) in a Java program, Java will treat the value as an int type. If you use the character JC within single quotes (V), Java will treat it as a char type. Likewise, if you place the literal x within double quotes Ox'), Java will treat it as a String. Depending on the literal you are using, Java provides special rules for hexadecimal, octal, characters, strings and boolean values. As you will learn, you can force a literal to be a certain type. For example, Java will treat the number 1 as an int. But you can force Java to treat the value as the type long by appending the L character to the literal number: 1L.
What Is The Primitive Type Byte?
A byte is a primitive Java data type that uses eight bites to represent a number ranging from -128 to 127. The following statements declare two byte variables. The first variable, flag_bits, can store one value. The second byte variable, data_table, is an array, capable of holding four values. In this case, the Java compiler will preassign the array elements using the values specified between the left and right braces
byte flag__bits;
byte data__table = { 32, 16, 8, 4 }; // Creates an array.
What Is The Primitive Type Short?
The type short is a primitive Java data type that uses two bytes to represent a number in the range -32768 to 32767. The Java type short is identical to the two-byte hit in many C/C++ compilers.
The following statements declare two variables of type short
short age;
short height, width;
Why Call By Value Prevents Parameter Value Change?
When you pass primitive types such as the types float, boolean, hit and char to a method, Java passes the variables by value. In other words, Java makes a copy of the original variable which the method can access and the original remains unchanged. Within a method, the code can change the values as much as it needs because Java created these values as copies of the originals.
Wherever you pass a primitive type as a parameter to a method, Java copies this parameter to a special memory location known as the stack. The stack maintains information about variables used by the method while the method executes. When the method is complete, Java discards the stack's contents and the copies of the variables you passed into the method are gone forever.
Because Java copies your original primitive type parameters, there is never any danger of a method altering your original values. Remember, this only applies to primitive types, which are automatically passed by value. Objects and arrays are not passed by value (instead they are passed by reference) and they are in danger of being changed.
Post a Comment