A Programming Language That Is Executed Line by Line, as It Is Read, Is Called a/an

Lab 7
Page 3


Programming in Java

  • What is Java?
  • Translators
  • Portability
  • Writing Java Code

What is Java?

Sunday Microsystems (the company that designed Coffee) describes Java equally a simple, object-oriented, distributed, interpreted, robust, secure, compages-neutral, portable, high-functioning, multithreaded, and dynamic language. Fortunately for everybody's sake, nosotros will simply discuss 2 features of Java. Specifically, we will consider Java equally an object-oriented, portable linguistic communication. Get-go, all the same, a few words about how programs become from the high-level programming language to the motorcar code your computer understands.


Translators

OK. You've written a program in a high-level langugage such every bit C or Java. The trouble is, your CPU was designed to execute only its own native motorcar language instructions. Then you need some fashion of converting your program into an equivalent sequence of machine language instructions. This is the job of a translator program.

Loftier-level languages normally employ one of two types of translators: a compiler or an interpreter. A compiler is a translator that reads your high-level program and converts the entire matter into a working machine language programme. An interpreter is a translator that converts and executes your plan, i line at a time. That is, an interpreter will read one line of your programme, convert it to auto intstructions, execute those, then convert the next line, execute that, and then on. (The translator for assembly linguistic communication programs is chosen an assembler.)


Portability

A program is said to exist portable if it can be made to run on many different kinds of computers. While a programme in a high-level linguistic communication can exist compiled for dissimilar kinds of computers, the resulting auto language programme tin can run on only one kind of computer. Most application programs that you use are in machine-language and are not portable. If yous were to copy the give-and-take processor from your Intel PC to, say, a DEC Alpha workstation, it would not run there. Why? Because the same numbers that represent the motorcar code for a useful working program on your Pentium flake will represent a seemingly nonsensical "gibberish" plan on the Alpha flake.

Yet, programs written in Coffee are portable. A Java applet volition run equally well on your calculator, on the computer down the hall, and on the Blastoff workstation (we'll explain why in a moment). Portablity is good considering the programmer tin concentrate all effort on just a single version of the program. No time must exist wasted rewriting the program and so that it will work on each dissimilar kind of reckoner.

Because the Spider web contains many kinds of computers, you cannot assume that everyone is using the same kind that you are. Thus, if you lot want to write a program that can run Web-broad, information technology must exist portable. This excludes compiled programs (those created by compiling a high-level language), considering the compiler creates auto language, which we know is not portable.

The other possibility is to use interpreted programs. If you write your plan in an interpreted language, then everyone can run it, equally long as they have the interpreter for that linguistic communication running on their own computer. On the other hand, interpreted programs tend to run very slowly in comparison to compiled programs, so an interpreted linguistic communication is too not a perfect solution.

Coffee's answer to this dilemma is to be both compiled and interpreted. It does this in the following way:

  • Sun first defined what information technology calls a "virtual machine linguistic communication". This is basically a machine language of the kind nosotros looked at, except that it does NOT actually run on whatsoever computers known to flesh!
  • Sunday developed a piece of software known as a Coffee Compiler which reads a program written in Coffee and translates information technology into a file containing virtual machine language instructions.
  • Finally, Lord's day created a piece of software known as the Coffee Virtual Machine, which is basically an interpreter. The JVM takes files containing virtual automobile language instructions and interprets them, ane at a fourth dimension, into real machine instructions on your computer, which can then be executed.
Of course, because those real auto instructions are unlike on different kinds of computers, Dominicus (or another company licensed by Sunday) really had to write a different version of the JVM for every kind of calculator. In many cases, the JVM is built directly into a spider web-browser (e.grand. Netscape three.0).

Considering the JVM interprets programs which are already in a relatively simple format (namely, Java virtual car language), it tin run much faster than would an interpreter designed to translate Coffee direct into machine instructions. Thus, with Coffee at that place is a proceeds in speed and portability over other interpreted langauges, at least theoretically.


Writing Java Code

Overview

Hither we will provide a very brief overview of some of the commands and syntax available in Java. This is by no means a complete reference, however. There are many peachy web sites that contain the entire Java language specification--if you want more information than we provide here, delight expect to ane of those sites.

Declaring Variables

A variable is only a proper name that refers to a location in retentiveness. Variables can be of many types which specify how the value stored in that retention location should exist interpreted. A few possible types for Coffee variables are: int (integer), bladder (floating point number), char (single ASCII character), boolean (True/False value), and String (cord of ASCII characters).

Before you can use a variable in a Java plan, yous must beginning declare its type. For case:

int age, height, weight; String first_name, last_name; char middle_initial;        

Comments

Comments provide a means for adding explanatory text to your program lawmaking, making information technology more than understandable to human being observers. In Java, comments are indicated by // (double-slash). Everything on a line afterwards the // volition be ignored by the compiler.

Indentation and Space

"Proper" indentation is a programming practise that makes your code easier for other humans to read and follow. However, it has no effect whatsoever on how the compiler reads your program. Cull an indentation way that suits you, and stick with it.

Operators

Operators are special symbols that human activity upon the values or variables that precede or follow them (depends on the operator). Some of the important operators are the following:

Operator Description
=
Assigns value on right to variable on left (e.yard. 10=y+10;)
+,-,*,/,%
Arithmetic: add together, subtract, multiply, dissever (integer), remainder (platonic for Euclid'south greatest-common-divisor algorithm)
+
Strings: concatenate left string with right string
<,<=,>,>=
Comparators: less than, less than or equals, greater than, greater than or equals
==,!=
More comparators: equals, doesn't equal
&&,||
Conditional AND (true if left and right both true) and conditional OR (true if either left or right is true)
++,--
Increase/decrement value by i

Control Statements

Control statements--such every bit if, while, and for--dictate the flow of a programme based upon the truth of some provisional examination. Each of these statements are described beneath.

if

if (condition) action else other_action

If the status evaluates to TRUE, then the activeness is taken, otherwise the other_action is taken. Note that the action and the other_action can be blocks of statements enclosed in { and }.

while

while (condition) activity

Every bit long as the condition evaluates to TRUE, then the action will exist repeated. Notation that the action can be a block of statements enclosed in { and }.

for

for (initialize; status; increment) activity

First, the expressions in the initialize section are evaluated. So, dependent on the truth of the condition, the activeness and then increment sections are repeatedly executed, until the status evaluates to FALSE. Annotation that the activity can be a block of statements enclosed in { and }.

Semicolons and Braces

Java demands that every consummate statement end with a ; (semicolon). Besides, if you would like a group of statements to be used where the syntax calls for a unmarried statement, you lot must enclose this group of statements inside { and }. For example:
if (x==1) {    foo = 10;          // These two statements comprise     y++;             // the "action" of the if. } else {    foo = 20;          // And these three    y--;             // comprise the "other_action"    a = proper noun.length; // of the if }        

Creating Objects

variable = new form(constructor arguments);
To create a new instance of a class (i.e. a new object), employ the new keyword, equally in this example:
Educatee due south;           // first declare a variable of the course Pupil ...                  // (...other code in betwixt...) due south = new Student;   // later, create an instance of the form, referred to by due south        
Constructor arguments may be used to give data such as the size of an object or initial values, depending on how the course is defined. For example, an object of form String (character strings) can be initialized:
String msg;           // get-go declare a variable of the class String ...                  // (...other code in between...) msg = new String("hello");   // later, create a String object, referred to past msg                             // and initialized to "hello"        

Accessing Objects

Once you lot have created an object, yous can view and manipulate its information only by invoking the object'southward methods. The syntax for invoking a method is:
object.method(arguments)
where object is the name of the object case, method is the proper noun of the method to invoke, and arguments is a comma-separated list of values to be passed to the method. For methods that do not take whatsoever arguments, the arguments list is left bare (although the parentheses are all the same kept).

As an example, let'due south suppose we accept divers a Educatee class that contains simply one slice of data, a cord to store the student'southward name. Also, suppose this course has merely one method, called setName, that lets you alter the proper name stored in the string. The code below illustrates how one would create a new pupil object and invoke its setName method:

Student s;             // declare a variable s of type Pupil  due south = new Educatee;       // brand a new Pupil object south.setName("Allison");  // set the proper noun field of this pupil to Allison        


tindellhipen1988.blogspot.com

Source: https://www.cs.princeton.edu/courses/archive/spr00/cs111/labs/introJava/3.html

0 Response to "A Programming Language That Is Executed Line by Line, as It Is Read, Is Called a/an"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel