Introduction
About JAVA
"Java refers to a number of computer software products
and specifications from Sun Microsystems
(the Java"" technology) that together provide a system
for developing and deploying
applications. Java 0s used in a wide variety of computing platforms spanning from cross-platform
embedded devices
and mobile phones on the low end to enterprise servers and super
computers on the high end. Java
is fairly ubiquitous in mobile phones, Web servers and enterprise
applications, and somewhat less
common in desktop applications, though users may have come across Java applets when
the Web.
browsing
Writing in the Java programming language is the primary way to produce code that will be
deployed
as Java bytecode, though there are compilers available for other languages such as
JavaScript,
Python and Ruby, and a native Java scripting language called Groovy. Java syntax
borrows heavily
from C and C++ but it eliminates certain low-level constructs such as pointers and
has a very simple
memory model where every object is allocated on the heap and all variables of object types are
references. Memnory management is handled through integrated automatic garbage collection
performed by the Java Virtual Machine (JVM).""
OOP - object Oriented Programming
OOP is a particular style of programming which involves a particular way of designing solutions to
particular problems. Most modern programming languages, including Java, support this paradigm.
When speaking about O0P one has to mention:
Inheritance
Modularity
Polymorphism
Encapsulation (binding code and its data)
However at this point it is too early to try to fully understand these concepts.
This guide is divided into two major sections, the first section is an introduction to the language and
llustrates various examples of code while the second part goes into more detail.
Variables and Data Types
Variables
Avariable is a place where the program stores data temporarily. As the name implies the value
stored in such a location can be changed while a program is executing (compare with constant).
class Example2 {
public static void main (String args 0) {
int varl; // this declares a variable
int var2; // this declares another variable
varl 1024; // this assigns 1024 to var1
System. out.println ("var1 contains " + var1) :
var2 = varl / 2;
System.out.print ("var2 contains varl / 2: "):
System. out. println (var2);
Predicted Output:
var2 Contains varl / 2: 512
The above program uses two variables, var1 and var2. var1 is assigned a value directly while var2 is
filled up with the result of dividing var1 by 2, I.e. var2 = var1/2. The words int refer to a particular
data type, i.e. integer (whole numbers).
Test your skills Example3
As we saw above, We used the / to work out the quotient of var1 by 2. Given that '+ would
perform addition, subtraction and "* multiplication, write out a program which performs all the
named operations by using two integer values which are hard coded into the program.
Hints:
You need only two variables of type integer
Make one variable larger and divisible by the other
You can perform the required calculatlons directly in the print statements, remember to
enclose the operation within brackets, e.g. (var1-var2)
useful method is the Math.random() which would return a random number ranging between 0.0
and 1.0.
Scope and Lifetime of Variables
The following simple programs, illustrate how to avoid programming errors by taking care where to
initialize variables depending on the scope.
class Examplel6 {
public static void main (String args [) {
int x; / / known to all code within main
x = 10;
if (x == 10) { / / start new scope
int y = 20; // known only to this block
1/ x and y both known here.
System. out.println("x and y: " + x t " " + y) ;
X =y * 2;
1/ y = 100; // Error! y not known here
1/x is still known here.
System.out.println ("x is " + x);
Predicted Output:
x and y: 10 20
x is 40
If we had to remove the comment marks from the line, y=100; we would get an error during
compilation as y is not known since it only exists within the block of code following the 'if
statement.
The next program shows that y is initialized each time the code belonging to the looping sequence is
executed; therefore y is reset to -1 each time and then set to 100. This operation is repeated for
three (3) times.