Introduction to Java

Introduction

Java is an object-oriented programming language with a built-in application programming interface (API) that can handle graphics and user interfaces and that can be used to create applications or applets. Because of its rich set of API's, similar to Macintosh and Windows, and its platform independence, Java can also be thought of as a platform in itself. Java also has standard libraries for doing mathematics.
Much of the syntax of Java is the same as C and C++. One major difference is that Java does not have pointers. However, the biggest difference is that you must write object oriented code in Java. Procedural pieces of code can only be embedded in objects. In the following we assume that the reader has some familiarity with a programming language. In particular, some familiarity with the syntax of C/C++ is useful.
. In Java we distinguish between applications, which are programs that perform the same functions as those written in other programming languages, and applets, which are programs that can be embedded in a Web page and accessed over the Internet. Our initial focus will be on writing applications. When a program is compiled, a byte code is produced that can be read and executed by any platform that can run Java.

Objects

Object-oriented programming focuses on constructs called “objects.” An object consists of data and functions known as methods which use or change the data. (Methods are similar to procedures or functions in other languages.) Objects of the same kind are said to have the same type or be in the same class. A class defines what data can be in an object, and what operations are performed by the methods. One or more objects can be created or “instantiated” from a class. The structure of a Java program consists of various objects exchanging messages. The keyword class defines a blueprint for objects with similar properties. In our first example we define the class Particle.


public class Particle
{
double x, y, vx, vy, mass;
}
This class does not do anything (because it has no methods), but it can be used to describe a particle by its position, velocity, and mass. For simplicity, we will consider only two dimensions.

Constructors

Every class has at least one constructor, a method which has the same name as the class. A constructor initializes a new object belonging to the class.
public class Particle
{
double x, y, vx, vy, mass; // these variables can be used by any method in the class
// example of constructor method
public Particle(double x, double y, double vx, double vy, double mass)
{
/* Use this keyword to make explicit that a method
is accessing its own variables. */
this.x = x; // set instance variable x equal to value of x in parameter list
this.y = y;
this.vx = vx;
this.vy = vy;
this.mass = mass;
}
}
The constructor Particle creates an object of the Particle class by specifying five parameters: the initial position and velocity of a particle and the value of its mass. We say that Particle is the constructor for the Particle class.
We have used the following properties of Java:
  • Variable Declaration: The types of all variables must be declared. The primitive types are byte, short, int, long (8, 16, 32, and 64 bit integer variables, respectively), float and double (32 and 64-bit floating point variables), boolean (true or false), and char. Boolean is a distinct type rather than just another way of using integers. Strings are not a primitive type, but are instances of the String class. Because they are so common, string literals may appear in quotes just as in other languages. Summary of primitive data types.
  • Naming Conventions: Java distinguishes between upper and lower case variables. The convention is to capitalize the first letter of a class name. If the class name consists of several words, they are run together with successive words capitalized within the name (instead of using underscores to separate the names). The name of the constructor is the same as the name of the class. All keywords (words that are part of the language and cannot be redefined) are written in lower case.
  • Instance variables and methods can be accessed from any method within the class. The x in the argument list of the above constructor refers to the local value of the parameter which is set when Particle is called. We use the this keyword to refer to those variables defined for the entire class in contrast to those defined locally within a method and those that are arguments to a method. In the above example, this.x refers to the variable x which is defined just after the first line of the class definition. After we have introduced multiple constructors, we show a more iillustrative example of the this keyword.
  • Classes are effectively new programmer-defined types; each class defines data (fields) and methods to manipulate the data. Fields in the class are template for the instance variables that are created when objects are instantiated (created) from that class. A new set of instance variables is created each time that an object is instantiated from the class.
  • The members of a class (variables and methods) are accessed by referrring to an object created from thr clas using the dot operator. For example, suppose that a class Particle contains and instance variable x amd a method step. If an object of this class is named p, then the instance variable in p would be accessed as p.x and the method accessed as p.step.
  • A semicolon is used to terminate individual statements.
  • Comments. There are three comment styles in Java. A single line comment starts with // and can be included anywhere in the program. Multiple line comments begin with /* and end with */; these are also useful for commenting out a portion of the text on a line. Finally, text enclosed within /** ... */ serves to generate documentation using the javadoc command.
  • Assignments. Java uses the C/C++ shortcuts summarized in Table.
Table Assignment shortcuts. operator example meaning += x += y x = x + y -= x -= y x = x - y *= x *= y x = x*y /= x /= y x = x/y %= x %= y x = x % y
The modulus operator % returns the remainder of a division. For example, 5 % 3
returns 2. The increment operator ++ works as follows:
i++;
is equivalent to
i = i + 1;
Similarly, the decrement operator -- works as follows:
i--;
is equivalent to
i = i - 1;
Type casting changes the type of a value from its normal type to some other type. For example:
double distance;
distance = 10.0;
int x;
x = (int)distance; // example of a type cast
What would be the value of x in the above example if distance = 9.9?

Multiple Constructors

The arguments of a constructor specify the parameters for the initialization of an object. Multiple constructors provide the flexibility of initializing objects in the same class with different sets of arguments, as in the following example.
public class Particle
{
double x, y, vx, vy, mass;
// examples of multiple constructors
public Particle()
{
this(0.0,0.0);
}
public Particle(double x, double y)
{
this(x,y,1.0);
}
public Particle(double x, double y, double m)
{
this(x,y,0.0,0.0,m);
}
public Particle(double x, double y, double vx, double vy)
{
this(x,y,vx,vy,1.0);
}
public Particle(double x, double y, double vx, double vy, double mass)
{
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.mass = mass;
}
}
  • Using multiple constructors is called method overloading -- the method name is used to specify more than one method. The rule for overloading is that the argument lists for all of the different methods must be unique, including the number of arguments and/or the types of the arguments.
  • All classes have at least one implicit constructor method. If no constructor is defined explicitly, the compiler creates one with no arguments.
We next give an example of a Particle class with methods for computing the weight of a particle and its distance from the origin. We will omit the velocity components for now.
public class Particle { double x, y, mass; public Particle() { } public Particle(double x, double y) { this(x,y,1); } public Particle(double x, double y, double mass) { this.x = x; this.y = y; this.mass = mass; } public double getWeight() { return 9.8*mass; } public double distanceFromOrigin() { return Math.sqrt(x*x + y*y); } }

Comments

Popular Posts