Overview of the Java language


Introduction

This is not a course on Java, this is only an introductory paper. The main purpose of this paper is to give to the reader the skill
to understand the meaning of an easy Java program. Let's start telling that a Java statement can be all in a row, or in more rows, but it is always ended by a semicolon (;). Another thing to know is that Java is case sensitive, for instance, if, If, iF, IF, have different meanings. Now we can take a look to the following subjects:


How to remark the code

To denote a text of comment you have two ways: you can use the // symbol or, else, the /* and */ symbols. 
The // denote that a text, until the end of the row, is a comment. 
The /* and the */ symbols are delimiters for a multi-row comment, they work like brackets:
/* opens a remark and */ closes it.


Variables and data types

You can define a variable in the same way you can do it in C: data type followed by variable's name, and a semicolon. For example

 int counter;  

define an integer variable, called counter. This statement don't assign any value to counter. You can assign a value to a variable, while you are defining it, adding an equal (=) and a value before the semicolon in this way:

 int counter = 0; 

You can also assign a value after the definition statement, in this way:

 variablename = value; 

therefore,

 int counter = 1; 

and

 int counter; counter = 1; 

give the same result.

Moreover you can also assign the result of a function.

 int a = 10; int b = 20; int c = 0;   c = (a + b); 

After the last instruction, c has the value 30.


Expressions

Expressions are used to assign values or to change a variable's contents.  There are a lot of operators and you can define your own operators.  When more than one operator is in an expression, operators are applied following brackets order or, without brackets, following the pre-fixed order.

 int a = 10; int b = 5; int c = 1; int d; int e;  d = a * b + c; e = a * (b + c); 

At the end d is 51, and e is 60.


Control flow statements

There are several families of statements to "govern the sequence in which a program's statements are executed":  decision making statements, loop statements, exception handling statements, and other... We'll overview only decision making statements and loop statements.

It lets you chose between two actions, basing on the truthfulness of a condition.

switch-case

It works similarly to the if-else, but it lets you chose between various actions

It executes a group of statements until a condition is true

do-while

It works similarly to while, but it executes the group of statements at least once

for

It works similarly to while, usually it's used to execute a group of statements for a fixed number of times


Objects support

Java supports objects by classes. A class is like a stamp to print objects; we could also tell that a class is the description/definition of a type of objects.  An object, member of a class, is an instance of that class. We can imagine a class dog: it describes (or defines) the look, the behavior, the characteristics of a dog. For instance we could say that a dog is a mammal, that it has four paws, two eyes, a nose, a mouth, sharp teeth, a good smell, etc.; moreover we could add that it: barks, follows his owner orders, keeps a good watch, eats, etc. An object of dog type is an instance of dog: Fido, Snoopy, or Lassie. They are all dogs (they are members of the dog class), but they are different each other. With Java you can define a class by the keyword class (too easy!:). A class will have its own methods and its own variables, and it can inherit methods and variables from another class by the extend keyword. For example:

 class dog extends mammal{
  int height;
  int weight;
  String name;   
  dog(String name, int height, int weight){
      name = this.name;
      height = this.height;
      weight = this.weight; ...  }
  void eats(int quant){ ...  }
   ...
 } 

defines the dog class, subclass of mammal, with two integer variables (height and weight), a string variable (name), and a couple of methods: dog and eats. The first method, dog, has a particular meaning: it is the class constructor. The class constructor is a method which has the same name of the class,it has the duty of the object initialization when an object is created.

After defining a class, it's possible to create objects from that class, in the same way it's possible to create variables: classname objectname;. By the new statement, followed by the constructor you can initialize the object. So, coming back to the above example,

 dog mydog = new dog("Fido", 50, 80); 

creates an object mydog, and the constructor assigns Fido, 50, 80 at name, height, weight variables.


Index           Home  Back       About  Contact us!

Copyright (c) 1998-2006 Wowarea