PowerBuilder Authors: Chris Pollach, Yakov Fain, RealWire News Distribution, Al Soucy, Elizabeth White

Related Topics: PowerBuilder

PowerBuilder: Article

Java vs. PowerScript

Java vs. PowerScript

With the industry buzzwords being Java, Java, and more Java, I wrote this article to provide insight into how you can leverage your knowledge of PowerScript and PowerBuilder to quickly learn and build Java programs. I'm going to assume that you're familiar with PowerBuilder and the OOP religion. To exploit all of Java's features, you must design and build applications in accordance with OOP techniques. Where appropriate, this article presents a syntactical comparison of the Java language and PowerScript.

Essentially, both Java and PowerBuilder represent their programs as a collection of objects that communicate and interact with one another. PowerBuilder does an excellent job of hiding from you the low-level details required to assemble objects at runtime. Java on the other hand doesn't hide any details from you other than the implementation of the core libraries.

The Java Language
All programming languages contain fundamental building blocks such as variables, control statements, operators and methods. You use these building blocks to build dynamic applications. Both the Java and PowerScript programming languages contain these building blocks. In Java, all statements must end with the semicolon, ";". In PowerBuilder, statements can end with either the carriage return or the semicolon. In PowerBuilder, however, embedded SQL statements must end with the semicolon. You don't usually use a semicolon to end a single statement. You use the carriage return as a statement separator and reserve the use of the semicolon to end embedded SQL statements.


Java is also a case-sensitive language, and differentiates between the keywords "hello" and "Hello". PowerBuilder is not case sensitive except for embedded SQL statements. In Java, white space (spaces, tabs, carriage returns and line feeds) is ignored except for within strings and no line continuation characters are required.

In Java there's a choice of three comment styles to choose from: single-line, multiline or the documentation comment. The choice of comment style depends on the size and nature of the comment. PowerBuilder permits the use of the first two comment styles, while the third is new to Java.

The single-line comment is used to quickly comment out a line of executable code. It begins with "//" and continues until the end of the line. All text after the "//" is ignored by the compiler.

The multiline comment enables you to create comments that span multiple lines. The comment starts with "/*"and ends with "*/" and any text in between is ignored by the compiler.

Java, unlike PowerBuilder, doesn't permit embedded multiline comments. For example, this is permitted in PowerBuilder:

Start of main comment.
/* This is a submultiline comment. */
End of main comment.

Java parses out the multiline comment, leaving behind the following:

End of main comment.

You must be careful when using multiline comments to comment out large blocks of code as you may accidentally include other multiline comments. This inevitably leads to an error message in some strange location in your code.

Documentation comments enable you to embed documentation for classes, methods, constructors and fields within the Java source code. The documentation comment is another example that parallels Sun Microsystems' Java philosophy. As you write the logic and code for your program, you may as well document it at the same time while it is fresh in your mind. Once the source is written, all the documentation comments can be extracted using the javadoc utility, which is responsible for creating HTML documentation from documentation comments. It's also located in the JDK bin directory. Listing 1 illustrates the use of documentation comments to generate documentation for the simple class myMath.


To generate the documentation for myMath, run javadoc myMath.java. The javadoc utility will create the HTML-based documentation files directly from the source code. Javadoc is also responsible for creating an index with all public methods and fields; an index of the classes in the package and a hierarchy tree. Each of the aforementioned sections is placed in its respective HTML file. The HTML files generated for the sample myMath.java are:

  • AllNames.html, index of all public fields and methods
  • myMath.html, documentation for the class myMath
  • packages.html, list of all classes within the package
  • tree.html, an inheritance tree for the documented classes

All programming languages specify a set of valid identifiers that can be used when naming methods, fields, classes, constants and other tokens. In Java, identifiers must start with a valid Java letter followed by any number of Java letters or Java digits. Several keywords are also reserved by Java and can't be used as identifiers. You can find a list of the reserved keywords in the docs; however, a few of them are abstract, Boolean, break, byte, case, catch, char, class, const, etc.

To determine whether a character is a valid Java letter, use the standard Java method, java.lang.Character.isLetter(). To determine whether a character is a valid Java letter or Java digit, use the standard Java method java.lang.Character.isLetterOrDigit(). Both methods are documented in the JDK under the class java.lang.Character.


Primitive Data Types
In Java several data types are available for use in programs. All are classified into two datatype categories: primitive and object. The primitive data types represent the class "native," the fundamental types common among most programming languages (see Table 1).

Storage for these data types is automatically allocated by the Java VM. For example, when declaring a field of type int, it can be immediately assigned a value, as illustrated in the following:

int j; // Declaration and allocation of storage
j = 10; // Assignment

This is the same process PowerBuilder uses when declaring any of its standard data types available in PowerScript. In addition, Java assigns default values to all primitive data types. Table 1 also lists the primitive Java data types and their corresponding size, default value and range. Unicode is an extended character set in which each character is represented by 16 bits as opposed to the PC standard ASCII character set represented by 8 bits. In ASCII there are only 28-1 possible characters, whereas Unicode increases the possible characters to 216-1.

Primitive Datatype Wrappers
Each primitive data type has a sibling object data type known as its wrapper, which is a class that "wraps" around its respective primitive data type. For example, the Double wrapper class "wraps" the primitive data type double. These wrapper classes enable you to manipulate primitive types as objects and perform conversions between different primitive types. In the JDK documentation the wrapper classes are located in the java.lang package.


These classes are contained within the set of Java core libraries, which implies that they aren't part of the Java language specification but a set of convenience classes defined by Sun Microsystems. The wrapper classes include all classes listed in Table 2. The names of each wrapper class all begin with a capital letter and have the same name as their corresponding primitive data type.

Sun Microsystems defines the wrapper classes as immutable objects, meaning that the content(s) of each wrapper isn't changeable. This is clearly seen by the lack of any setDataType() methods in the wrapper classes. To change the value stored in the wrapper class, a new class must be created.

Unlike the primitive types, wrapper classes must be constructed before they can be used. Once constructed, they're accessed like any other Java object. Creating any object in Java is like creating a user object in PowerBuilder. In PowerBuilder objects are created with the "create" statement; in Java they're created with the "new" statement. For example:

Integer myInt = new Integer( 10 ); // #1
Double myDouble = new Double( 10.0 ); // #2

Statement 1 in the example above creates an instance of the Integer wrapper class and assigns it the int value of 10 by using a special method known as a constructor, which is discussed later. Methods of the wrapper class are used to perform datatype conversions. The following illustrates common conversions performed by the wrapper classes:

// Converting from the Wrapper Datatype to the Primitive Datatype.
int j = Integer.valueOf( myString ).intValue();
double d = Double.valueOf( myString ).doubleValue();

// Converting from the Primitive Datatype to the Wrapper Datatype.
String strValue = String.valueOf( 100 );
String strValue = new Float( 3.1415 ).toString();
String Datatype

Java doesn't define the primitive datatype string as part of its language specification. Instead, Java represents strings as objects of the datatype string, which is also located in the java.lang package. Because a string is an object, it contains methods and fields that are used to interact with the string. Like any other Java object, the string object is required to be instantiated before it can be used. The following creates a string field called myCarsName and sets its value to the string "Porshe 911 Targa":

String myCarsName = new String( "Porshe 911 Targa" );

Since strings are frequently used in programs, Sun Microsystems added a shortcut in the Java compiler to create anonymous strings. For example:

String filName = "Tom";
String milName = "Dale";

"Dale" and "Tom" are anonymous strings that are automatically instantiated by the Java compiler. Once the compiler creates the strings, it assigns them to the fields milName and filName, respectively.

Since strings are objects in Java, a few properties are worth mentioning:

  1. The text representing the string is contained within the string object and is not alterable.
  2. Each time a string is altered, Java creates a new string object to hold the new string.
  3. To compare the contents of a string, use the equals() method of the string object.
  4. Java reuses duplicate string literals as a performance technique.

Consider the following method in Java:

void compareFunction() {

String one = "Marie";
String two = "Marie";
String three = new String( " and Bob" );

if ( one == two ) {
// Objects one and two are equal.
System.out.println( "one == two, objects one and two are equal!" );

if ( one.equals(two) ) {
// Contents of one and two are equal.
System.out.println( "Contents of one equals contents of two." );

// The contents of three is concatenated to the contents of one and
// a new string object is created then assigned to one.
one = one + three;


For efficiency, Java will reuse duplicate string literal objects, as mentioned in point 4 above. In the example above, duplication is measured and verified by the expression "one == two". Technically, the statement should read: "The objects one and two are equal." The strings one and two are also equivalent as strings, which are measured and verified with the method called "one.equals(two)." Java's behavior is unlike PowerBuilder, as it will automatically compare the contents of two strings with the "=="operator.

PowerBuilder's Any Data Type
In PowerBuilder the "any" data type can hold any of the standard data types in PowerBuilder. In Java there's no equivalent object to PowerBuilder's any data type, but it can be emulated in Java with the use of the wrapper classes and some object-oriented programming. Take a look at the following class in Java:


class Truck {
void truckInfo() {
String make = new String( "Dodge Dakota R/T!" );
Integer size = new Integer( 350 );

displayInfo( make );
displayInfo( size );

void displayInfo( Object o ) {
System.out.println( o.toString() );

The truckInfo() method in the example above creates two different data types: a String and an Integer. The root of all Java classes, Object, is used to hold the two different data types from truckInfo(). Notice that truckInfo() invokes the displayInfo() method with each data type.

In PowerBuilder, before an "any" object can be assigned to a specific data type the underlying data type for the object should be verified with the typeof() operator. Java has an equivalent type verifier, the instanceof operator. The instanceof operator is a boolean operator that evaluates to true if the created instance of a class matches the class definition that it's being compared to. In the following example the expression "o instanceof Truck" compares the field "o" to the class definition "Truck". If they match, the expression evaluates to true.

Truck t = new Truck();
Object o = t;

if (o instanceof Truck) {
System.out.println( "o is an instance of a Truck." )

To assign the field "Truck" to the field "o", a runtime cast is required. In PowerBuilder a runtime cast isn't required since the PowerBuilder VM takes care of this for you. The following example modifies the displayInfo() method to check the underlying data type of the information it's about to display.

void displayInfo( Object o ) {
if ( o instanceof Integer ) {
Integer i = (Integer) o;
System.out.println( "Integer type, value = " + i.intValue() );
}else {
System.out.println( "Unchecked type, value = " + o.toString() );

In the example, if the method argument "o" is an instance of the class Integer, the argument is cast back to an Integer instance. This is done with the line:

Integer i = (Integer) o;

The general syntax of a cast is as follows:

DestinationClassName destFieldName = (DestinationClassName) sourceFieldName;

Relational Operators
Java's use of relational operators mimics their use in C and C++ and, to a degree, PowerBuilder. The Java language defines the standard relational operators in Table 3.

As a PowerBuilder developer, you have to be careful of using PowerBuilder's equal to operator, "=" in Java, as you'll get a compiler error. Examples 1 and 2 in Table 4 demonstrate proper use of the "equal to" and "not equal to" operators. Examples 3 and 4 demonstrate the difference between Java and PowerBuilder.


Example 3 demonstrates that, in Java, the assignment operator "=" can be chained to assign the same value to multiple variables of the same data type. The variables "a" and "b" will be simultaneously assigned the value of "c". This isn't a legal expression in PowerBuilder. Example 4 demonstrates that, in Java, the equal relational operator returns a boolean expression like PowerBuilder. Example 5 in the table demonstrates a common trick in Java. The example calls a method to getMyObject(), which returns a reference to MyObject and stores the return in "mo". A second test is performed to ensure that "mo" is not null, and if it isn't null we can call a method on mo. Something to note is that the order of operators is important in Java. For example, both != and >= are valid; however, =! and => are invalid and will generate an error from the Java compiler.

Next I like to go over boolean, conditional and arithmetic operators, the flow control statements, Java fields versus PowerBuilder variables, Java methods versus PowerBuilder functions, and object orientation. So stay tuned!


More Stories By Kouros Gorgani

Kouros Gorgani is a software development manager with EAServer Engineering Canada of Sybase Inc. Kouros is a CPD and the author of several technical books.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.