Java Concepts (doc) - Java Concepts and Tips

By Travis Sims,2014-04-24 11:36
11 views 0
Java Concepts (doc) - Java Concepts and Tips

    Java Concepts and Tips (for C programmers)

    N. Guydosh


The following is based some commentary on the concepts presented in Appendix A (Java Primer)

    in “Applied Operating Systems Concepts”, by Silberschatz. These comments are not complete,

    but hopefully should serve as a review or introduction. They relate or compare Java concepts to

    corresponding ANSI C concepts.

If you need a “tutorial” review or would like to download the JDK package, see the “Useful Links”

    link on the CS 350 web site. The tutorial given at: appears to be useful. Download the package and expand it out on your hard drive for

    the tutorial.

    The following pertains to standalone java programs not applets.

? General Comments

    The source code for a java Program is a collection of formal structures called classes. A class

    in java is similar to a “structure” in C language. A C language structure contains only data

    declarations/definitions, whereas a java class contains both data declarations and function

    declarations. The function definitions are called “methods” in java. Java functions can only

    exist as methods within a class. See code samples in this document for examples.

    There are two important special methods: a constructor “method”, and the main method. The

    constructor has a function name which is the same as the name of the class. The purpose of he

    constructor is to initialize the data variables within the class containing the constructor. There

    may be more than one constructor (with the same name) in a class. Multiple constructors are

    distinguished by having different input parameter lists. Note that technically a constructor is

    not a method since it does not return a value. It more closely resembles a pascal procedure.

    Methods, on the other hand, are essentially C functions since they return a value. The main

    method defines the logical “entry point” of he Java program. A Java program may consists of

    multiple files containing the various classes; the name of the file containing main() must match

    tha class name in which it appears. Every stand-alone program (run from the command line)

    must contain a main() method.

    One property of a Java method which distinguishes it from an ANSI C function is the way

    parameters are passed to the function. In ANSI C when a variable is passed to a function, it is

    passed by copy. A copy of the current value is passed to the function. If the variable is

    modified on the inside of the function, it has no affect on the corresponding variable on the

    outside. This is obviously a problem if the variable represents a large data structure. The only

    way an outside variable could be modified is by passing a pointer (the address of the variable)

    to it to the function. In Java, only primitive variables are passed by value. Objects, on the

    other hand, are passed by reference to a method. Any modifications to the object are

    immediately reflected in the original object on the outside. A “reference” to an object is

    CS350 Spring 2002 Java Concepts page 1

defined as the name of the object. A reference is logically similar to a pointer in C. When you

    pass a reference to a method, you pass a copy of the reference (not the object being referenced).

    As long as you do not change the value of the reference pased, then changing the internals of

    the referenced object, will be reflected to the object outside of the method that was passed. See

    below for more on objects.

    How to instantiate or allocate an object from a class definition. Objects are at the heart of java programming. An object is a particular instantiation or allocation of a class in much the same way that when we define a variable against a declaration of a C data structure, the declaration becomes allocated in memory (defined).

    Each time a variable is defined against this C data structure, we have another instantiation or

    allocation of the structure. Now in Java, a class is allocated or instantiated to an object by the

    “new” operator. Taking an example from the sample Java code below, we have a class defined

    called Manager. The constructor for Manager is also called Manager (…) with three

    parameters. We may now instantiate manager to an object called boss using the new operator:

    Manager boss = new Manager(“Pat”, “Supervisor”, 30000); The left side of the assignment declares boss to be of type Manager (the class), and the right

    hand side allocates this variable boss to be an instantiation of Manager. Now an important

    event takes place (besides the allocation of memory). Whenever an object is instantiated with

    new, the constructor is invoked using the parameters specified in the new statement, and the

    resulting object is initialized by invoking the constructor. This initialization is the current state

    of the object. Multiple instantiations of an object generally have different states.

How to access data variables and call methods in a class

    Once an object has been instantiated, we can now refer to data variables and methods, in the

    object in much the same way we reference data variables in a C structure namely by using the “dot” notation to get at stuff inside. In our example below, we may invoke the

    computeRaise() method in Manager (object boss) as follows:


Data is referenced the same way as in C data structures. If you want to call a method from

    within the object in which it is defined, there is no need to use the dotted qualifier. If you want

    to make it explicit to refer to the local object the dotted qualifier “this” could be used:

    this.computeRaise(); “this” is a reference to the object in which it is used.

Scope of methods and variables:

    Variables and methods defined with the key word public can be referenced outside of the defining class using the above dot notation. If they are defined with the private key word, the can only be accessed inside the defining class.

.Summary and a few loose ends:

    o A class consists of methods and data declarations. One of the “methods” which has the

    same name as the name of the class is the constructor, which gets executed whenever an

    object instantiation of the class is allocated using the new operator.

    CS350 Spring 2002 Java Concepts page 2

o A program consists of a number of classes compiled from *.java to *.class using the javac

    compiler. One of the classes, say must have a “main” method. When you

    compile this one: javac, all the other classes referenced by xyz will get compiled

    also. It is xyz which gets run from the command line using the java command: java xyx .

    ? Inherentance: The primer considers three aspects:

In all cases below, an abstract method is a method which is not defined, but only declared

    (used in an interface or an abstract class to be defined later). It essentially has the format of

    a prototype statement in C language. We consider inheritance in three general categories:

    o A general class: Extending some arbitrary “base” class to a derived class:

    You can extend any class (to another class) by adding (augmenting) variables and methods

    to the base class. The format is:

    public class base-class {…}

    public class derived-class extends base-class {…}

    where base-class is the base class

    The derived class “derived-class” will use the super statement it initialize base-class from

    which it is derived.

    You can have many different derived classes.

    o An interface: Implementing an interface to a “implemented” class:

    An interface is like a class that contains only abstract methods and no data definitions

    except constants. You can create a class by “implementing” an interface. This is done by

    defining all the abstract methods, and adding any required variable data definitions in the

    resulting implemented class.

    The format is:

    public interface interface-name {…}

    public class implemented-class implements interface-name {…}

    where “interface-name” is the name of the interface and “implemented-class” is the

    name of the class which got implemented.

    The resulting class “implemented-class” will use NOT the super statement it initialize

    interface from which it is derived because the latter has no implemented methods.

    You can have many different implemented classes.

    See page 775.

    o An abstract class: Extending an abstract class to a derived class:

    **** this is what is used in programming with threads ********

    An abstract class is like an interface, but can contain both abstract methods and fully

    defined methods including variable data definitions. You can “extend” an abstract class

    (to a class). The derived class implements the abstract methods of the abstract class.

    CS350 Spring 2002 Java Concepts page 3

The format is:

    public abstract abstract-class-name {…} public class derived-class extends abstract-class-name {…}

    where abstract-class-name is the name of the abstract class.

The derived class “derived-class” will use the super statement it initialize abstract class

    from which it is derived.

    You can have many different derived classes.

     stThe importance of inheritance in our study of threads is that threads are created from an abstract ed., by

    thread class by using inheritance. Silberschatz to illustrate these ideas.

    An example will now be given from “Applied Operating Systems Concepts”, 1public abstract class Employee


     public Employee (String n, String t, double s) { // consructor

     name = n;

     title = t;

     salary = s;


    public void printInfo() {

     System.out.println(“Name: “ + name);

     System.out.println(“Title: “ + title);

     System.out.println(“Salary: “ + salary);


    public abstract void computeRaise();

    private String name;

    private String name;

    private double salary;

} // end of class Employee

public class Manager extends Employee


     public Manager (String n, String t, double s) { //constructor

     super(n, t, s); // calls constructor on class Employee


     public void computeRaise() {

     salary += salary * .05 + BONUS;


     private static final double BONUS = 2500;

    } // end of class Manager

    CS350 Spring 2002 Java Concepts page 4

public class Developer extends Employee


     public Developer (String n, String t, double s, int np) { //constructor

     super(n, t, s); // calls constructor on class Employee

     numOfPrograms = np;


     public void computeRaise() {

     salary += salary * .05 + numOfPrograms * 25;


     private int numOfPrograms;

    } // end of class Developer

public class Test // this is the class containing the main method


     public static void main(String args[]) {

     Employee[] worker = new Employee[3];

     worker[0] = new Manager(“Pat”, “Supervisor”, 30000);

     worker[1] = new Developer(“Tom”, “Programmer”, 28000, 20);

     worker[2] = new Developer(“Jay”, “Intern”, 26000, 8);

     for (i = 0; i <3; i++) {




     private int i;

     } // end of main

    } // end of class Test

    CS350 Spring 2002 Java Concepts page 5

Report this document

For any questions or suggestions please email