Java Debugger Tutorial|Java Debugger Techniques

Java Debugger Tutorial|Java Debugger Techniques
The basic Java debugger is very similar to gdb and the commands it under-
stands.3 The command to launch is jdb StartClass and it launches a second
JVM to debug the program that waits for continued execution. Breakpoints can
be set by using stop at AClass:aLineNumber or stop in aCompleteMethod-
Name, or stop in AClass. for stopping at the constructors, or stop in
AClass. to stop at the initialization of the class.
next advances to the next line.
step advances to the next line within current scope.
run starts execution.
cont continues execution after a breakpoint has been found.
print prints values of the subsequent fields/method calls.
dump is more complete with all values of fields dumped.
threads lists the number of threads.
thread selects the thread with number indicated afterwards.
where dumps the stack.
where all dumps all the stacks.
catch can catch an exception on-the-fly.

Read more...

Java Virtual Machine Architecture|Java Virtual Machine Tutorial|Java Virtual Machine Launcher|Java Virtual Machine Specification

Java Virtual Machine
Table 2: Java Virtual Machine main options
java
Adds the path to the CLASSPATH value
-classpath path,
for this execution
-cp path
-verbose [:class—gc—jni] Output messages on classes/garbage
collection/java native interface to
show the effected operations
Gives the current version and exits
-version
Print help
-help, -?
Print non-standard options
-X
e.g. -Xmssize e.g. Set the initial heap size.
e.g. -Xsssize e.g. Set the thread stack size.
The Java Virtual Machines (JVM) are the execution environments for Java
programs (see Table 2). A JVM is intended to act as an interpreter for the byte
code. Thus the byte code is itself portable from one platform to another. Java
virtual machines have to be ported and installed on the respective platforms
prior to program execution.
By default, the argument class name will be loaded and the main method
within this class will be called as a starting point. The JVM takes care of
memory allocation and deallocation. In particular, the JVM manages automatic
garbage collection of instances and code when not used any more .
While interpretation is slow in general, a JVM has lots of facilities to speed
up execution. The most important one is Just-In-Time (JIT) compiling which
compiles the byte-code to native code when it is loaded, thus giving near native
performance on the use of a class once it has been executed at least once. Of
course this initial translation yields a performance penalty at first execution of
given method.
If MyMain is the class containing the bootstrap code, the program can be
launched as follows:
java MyMain

Read more...

BlueJ Installation on Linux|BlueJ Installation on Unix|BlueJ Installation on Ubuntu

BlueJ Installation on Linux|BlueJ Installation on Unix|BlueJ Installation on Ubuntu

The general distribution file for is an executable jar file. It is called bluej-xxx.jar, where xxx is a version number. For example, the BlueJ version 2.0.0 distribution is named bluej-200.jar. You might get this file on disk, or you can download it from the
BlueJ web site at http://www.bluej.org.

Run the installer by executing the following command. NOTE: For this example, I use the distribution file bluej-200.jar – you need to use the file name of the file you’ve got (with the correct version number).
/bin/java -jar bluej-200.jar
is the directory, where J2SE SDK was installed. A window pops up, letting you choose the BlueJ installation directory and the Java version to be used to run BlueJ. Click Install. After finishing, BlueJ should be installed.

Read more...

BlueJ Installation on Windows|BlueJ Installation Problems|BlueJ Installation Requirements

BlueJ Installation

BlueJ is distributed in three different formats: one for Windows systems, one for MacOS, and one for all other systems. Installing it is quite straightforward.

Prerequisites

You must have J2SE v1.4 (a.k.a. JDK 1.4) or later installed on your system to use BlueJ. Generally, updating to the latest stable (non-beta) Java release is recommended. If you do not have JDK installed you can download it from Sun’s web site at http://java.sun.com/j2se /. On MacOS X, a recent J2SE version is preinstalled - you do not need to install it yourself. If you find a download page that offers “JRE” (Java Runtime Environment) and “SDK” (Software Development Kit), you must download “SDK” – the JRE is not sufficient.

BlueJ Installation on Windows

The distribution file for Windows systems is called bluejsetup-xxx.exe, where xxx is a version number. For example, the BlueJ version 2.0.0 distribution is named bluejsetup-200.exe. You might get this file on disk, or you can download it from the BlueJ web site at http://www.bluej.org. Execute this installer. The installer lets you select a directory to install to. It will also offer the option of installing a shortcut in the start menu and on the desktop. After installation is finished, you will find the program bluej.exe in BlueJ’s installation directory.

The first time you launch BlueJ, it will search for a Java system (JDK). If it finds more than one suitable Java system (e.g. you have JDK 1.4.2 and JDK 1.5.0 installed), a dialog will let you select which one to use. If it does not find one, you will be asked to locate it yourself (this can happen when a JDK system has been installed, but the corresponding registry entries have been removed). The BlueJ installer also installs a program called vmselect.exe. Using this program, you can later change which Java version BlueJ uses. Execute vmselect to start BlueJ with a different Java version.

The choice of JDK is stored for each BlueJ version. If you have different versions of BlueJ installed, you can use one version of BlueJ with JDK 1.4.2 and another BlueJ version with JDK 1.5. Changing the Java version for BlueJ will make this change for all BlueJ installations of the same version for the same user.

Read more...

java Program for person class

person class (with get and set methods)
Note: Highlighted code added in this lesson.
package org.totalbeginner.tutorial;
public class Person {
// fields
private String name; // name of the person
private int maximumBooks; // most books the person can check out
// constructors
public Person() {
name = "unknown name";
maximumBooks = 3;
}
//methods
public String getName() {
return name;
}
public void setName(String anyName) {
name = anyName;
}
public int getMaximumBooks() {
return maximumBooks;
}
public void setMaximumBooks(int maximumBooks) {
this.maximumBooks = maximumBooks;
}
}

Read more...

Java Objects Basics

Java Objects
A simple C++ object or C struct definition such as "Button b;" allocates memory on the stack for a Button object and makes b refer to it. By contrast, you must specifically instantiate Java objects with the new operator. For example,
// Java code
void foo() {
// define a reference to a Button; init to null
Button b;
// allocate space for a Button, b points to it
b = new Button("OK");
int i = 2;
}
As the accompanying figure shows, this code places a reference b to the Button object on the stack and allocates memory for the new object on the heap. The equivalent C++ and C statements that would allocate memory on the heap
would be:
// C++ code
Button *b = NULL; // declare a new Button pointer
b = new Button("OK"); // point it to a new Button
/* C code */
Button *b = NULL; /* declare a new Button pointer */
b = calloc(1, sizeof(Button)); /* allocate space for a Button */
init(b, "OK"); /* something like this to init b */
All Java objects reside on the heap; there are no objects stored on the stack. Storing objects on the heap does not cause potential memory leakage problems because of garbage collection.

Each Java primitive type has an equivalent object type, e.g., Integer, Byte, Float, Double. These primitive types are provided in addition to object types purely for efficiency. An int is much more efficient than an Integer.

Read more...

Java HTML/Applet Interface

HTML/Applet Interface
The HTML applet tag is similar to the HTML img tag, and has the form:

[parameters]

where the optional parameters are a list of parameter definitions of the form:

An example tag with parameter definitions is:




where p1 and p2 are user-defined parameters.
The code, width, and height parameters are mandatory. The parameters codebase, alt, archives, align, vspace, and hspace are optional within the tag itself. Your applet can access any of these parameters by calling:
Applet.getParameter("p")
which returns the String value of the parameter. For example, the applet:
import java.applet.Applet;
public class ParamTest extends Applet {
public void init() {
System.out.println("width is " + getParameter("width"));
System.out.println("p1 is " + getParameter("p1"));
System.out.println("p2 is " + getParameter("p2"));
}
}
prints the following to standard output:
width is 300
p1 is 34
p2 is test

Read more...

Java string literals

Strings
Java string literals look the same as those in C/C++, but Java strings are real objects, not pointers to memory. Java strings may or may not be null-terminated. Every string literal such as "a string literal" is interpreted by the Java compiler as new String("a string literal")
Java strings are constant in length and content.

For variable-length strings, use StringBuffer objects. Strings may be concatenated by using the plus operator:

String s = "one" + "two"; // s == "onetwo"

You may concatenate any object to a string. You use the toString() method to convert objects to a String, and primitive types are converted by the compiler.

For example,
String s = "1+1=" + 2; // s == "1+1=2"
The length of a string may be obtained with String method length(); e.g., "abc".length() has the value 3.
To convert an int to a String, use:

String s = String.valueOf(4);

To convert a String to an int, use:

int a = Integer.parseInt("4");

Read more...

Java garbage collection|Java garbage collection tutorial|Java garbage collection algorithm|Java garbage collection techniques|Java garbage collection

Java garbage collection|Java garbage collection tutorial|Java garbage collection algorithm|Java garbage collection techniques|Java garbage collection example|Java garbage collection tuning|Java garbage collection basics|Java garbage collection finalize

Garbage Collection

An automatic garbage collector deallocates memory for objects that are no longer needed by your program, thereby relieving you from the tedious and error-prone task of deallocating your own memory. As a consequence of automatic garbage collection and lack of pointers, a Java
object is either null or valid--there is no way to refer to an invalid or stale object
(one that has been deallocated).

To illustrate the effect of a garbage collector, consider the following C++ function that allocates 1000 objects on the heap via the new operator (a similar C function would allocate memory using calloc/malloc):

// C++ code
void f() {
T *t;
for (int i = 1; i <= 1000; i++) {
t = new T; // ack!!!!!
}
}

Every time the loop body is executed, a new instance of class T is instantiated, and t is pointed to it. But what happens to the instance that t used to point to? It's still allocated, but nothing points to it and therefore it's inaccessible. Memory in this state is referred to as "leaked" memory.

In the Java language, memory leaks are not an issue. The following Java method causes no ill effects:
// Java code
void f() {
T t;
for (int i = 1; i <= 1000; i++) {
t = new T();
}
}

In Java, each time t is assigned a new reference, the old reference is now available for garbage collection. Note that it isn't immediately freed; it remains allocated until the garbage collector thread is next executed and notices that it can be freed. Put simply, automatic garbage collection reduces programming effort, programming errors, and program complexity.

Read more...

Java Pointers

No Pointers!

The Java language does not have pointer types nor address arithmetic. Java
variables are either primitive types or references to objects. To illustrate the
difference between C/C++ and Java semantics, consider the following equivalent
code fragments.
// C++ code (C code would be similar)
Stack *s = new Stack; // point to a new Stack
s->push(...);
// dereference and access method push()
The equivalent Java code is:
// Java code
// internally, consider s to be a (Stack *)
Stack s = new Stack();
// dereference s automatically
s.push(...);

Read more...

Java Method Parameters and Return Values

Method Parameters and Return Values
Arguments and return values for primitive types are passed by value to and from
all Java methods because they are implied assignments, as in C/C++. However, all
Java objects are passed by reference. For example, the C/C++ code:
// C++ code
int foo(int j) { return j + 34;}
Button *bfoo(Button *b) {
if ( b != NULL ) return b;
else return new Button();
}
or, in C
/* C code */
int foo(int j) { return j + 34;}
Button *bfoo(Button *b) {
if ( b != NULL ) return b;
else return calloc(sizeof(Button));
}
would be written in the Java language:
// Java code
int foo(int j) { return j + 34;}
Button bfoo(Button b) {
if ( b != null ) return b;
else return new Button("OK");
}

Read more...

Applet Execution

Applet Execution
An applet is a Java program that runs within a Java-compatible WWW browser or
in an appletviewer. To execute your applet, the browser:

• Creates an instance of your applet
• Sends messages to your applet to automatically invoke predefined lifecycle
methods
The predefined methods automatically invoked by the runtime system are:
• init(). This method takes the place of the Applet constructor and is only called
once during applet creation. Instance variables should be initialized in this method.
GUI components such as buttons and scrollbars should be added to the GUI in
this method.
• start(). This method is called once after init() and whenever your applet is
revisited by your browser, or when you deiconify your browser. This method
should be used to start animations and other threads.
• paint(Graphics g). This method is called when the applet drawing area needs
to be redrawn. Anything not drawn by contained components must be drawn in
this method. Bitmaps, for example, are drawn here, but buttons are not because
they handle their own painting.
• stop(). This method is called when you leave an applet or when you iconify
your browser. The method should be used to suspend animations and other threads so they do not burden system resources unnecessarily. It is guaranteed to
be called before destroy().
• destroy(). This method is called when an applet terminates, for example, when
quitting the browser. Final clean-up operations such as freeing up system
resources with dispose() should be done here. The dispose() method of Frame
removes the menu bar. Therefore, do not forget to call super.dispose() if you
override the default behavior.
The basic structure of an applet that uses each of these predefined methods is:
import java.applet.Applet;
// include all AWT class definitions
import java.awt.*;
public class AppletTemplate extends Applet {
public void init() {
// create GUI, initialize applet
}
public void start() {
// start threads, animations etc...
}
public void paint(Graphics g) {
// draw things in g
}
public void stop() {
// suspend threads, stop animations etc...
}
public void destroy() {
// free up system resources, stop threads
}
}
All you have to do is fill in the appropriate methods to bring your applet to life. If
you don't need to use one or more of these predefined methods, simply leave them
out of your applet. The applet will ignore messages from the browser attempting
to invoke any of these methods that you don't use.

Read more...

Java Program Execution

Java Program Execution
The Java byte-code compiler translates a Java source file into machineindependent
byte code. The byte code for each publicly visible class is placed in a
separate file, so that the Java runtime system can easily find it. If your program
instantiates an object of class A, for example, the class loader searches the
directories listed in your CLASSPATH environment variable for a file called A.class
that contains the class definition and byte code for class A.

There is no link phase for Java programs; all linking is done dynamically at The following diagram shows an example of the Java compilation and execution
sequence for a source file named A.java containing public class A and non-public
class B:

Java programs are, in effect, distributed applications. You may think of them as a
collection of DLLs (dynamically loadable libraries) that are linked on demand at
runtime. When you write your own Java applications, you will often integrate
your program with already-existing portions of code that reside on other
machines.

Read more...

Java Program Structure

Java Program Structure
A file containing Java source code is considered a compilation unit. Such a
compilation unit contains a set of classes and, optionally, a package definition to
group related classes together. Classes contain data and method members that
specify the state and behavior of the objects in your program.

Java programs come in two flavors:

• Standalone applications that have no initial context such as a pre-existing main
window

• Applets for WWW programming

The major differences between applications and applets are:

• Applets are not allowed to use file I/O and sockets (other than to the host
platform). Applications do not have these restrictions.

• An applet must be a subclass of the Java Applet class. Aplications do not need to
subclass any particular class.

• Unlike applets, applications can have menus.

• Unlike applications, applets need to respond to predefined lifecycle messages
from the WWW browser in which they're running.

Read more...

  © Free Blogger Templates Modic Template by For more Information : click Here

Back to TOP