NETB131 Programming Project
Programming language Java
An overview

Filip Stojkovski ,  F41758



Navigation:

1. History and Special Features
2. "Hello World" Program
3. Fundamental Data Types and Assignment Operator
4. Basic Control Flow
5. Functions
6. Arrays
7. Compilers
8. Projects and Software in Java
9. Standard
10. References





1. History and Special Features   (^)

Java is a programming language originally developed by Sun Microsystems and released in 1995 as a core component of  Sun's Java  platform. The Java programming Language evolved from a language named Oak. Oak was developed in the early nineties at Sun Microsystems as a platform-independent language aimed at allowing entertainment appliances such as video game consoles and VCRs to communicate . Oak was first slated to appear in television set-top boxes designed to provide video-on-demand services. Just as the deals with the set-top box manufacturers were falling through, the World Wide Web was coming to life. As Oak's developers began to re cognize this trend, their focus shifted to the Internet and WebRunner, an Oak-enabled browser, was born. Oak's name was changed to Java and WebRunner became the HotJava web browser. The excitement of the Internet attracted software vendors such that Jav a development tools from many vendors quickly became available. That same excitement has provided the impetus for a multitude of software developers to discover Java and its many wonderful features.

Java became popular quickly. With the advent of Java 2, new versions had multiple configurations built for different types of platforms. For example, J2EE was for enterprise applications and the greatly stripped down version J2ME was for mobile applications. J2SE was the designation for the Standard Edition. In 2006, for marketing purposes, new J2 versions were renamed Java EE, Java ME, and Java SE, respectively.
In 1997, Sun Microsystems approached the ISO/IEC JTC1 standards body and later the Ecma International to formalize Java, but it soon withdrew from the process. Java remains a de facto standard that is controlled through the Java Community Process. At one time, Sun made most of its Java implementations available without charge although they were closed source, proprietary software. Sun's revenue from Java was generated by the selling of licenses for specialized products such as the Java Enterprise System. Sun distinguishes between its Software Development Kit (SDK) and Runtime Environment (JRE) which is a subset of the SDK, the primary distinction being that in the JRE, the compiler, utility programs, and many necessary header files are not present.

There were five primary goals in the creation of the Java language:





2. "Hello World" Program  (^)


class HelloWorld {
public static void main(String args[])
{
System.out.println("Hello world!");
}
}


3. Fundamental Data Types (integer, floating point, string) and Assignment Operator  (^)
   int: integers, no fractional part
      1, -4, 0double: floating-point numbers (double precision)
      0.5, -3.11111, 4.3E24, 1E-14  A numeric computation overflows if the result falls outside the range for the number type
      int n = 1000000;
   System.out.println(n * n); // prints -727379968
 


      Java: 8 primitive types, including four integer types and two floating point types
     

Type
Description
Size
int
The integer type, with range -2,147,483,648 . . . 2,147,483,647 4 bytes
byte
The type describing a single byte, with range -128 . . . 127
1 byte
short
The short integer type, with range -32768 . . . 32767
2 bytes
long
The long integer type, with range -9,223,372,036,854,775,808 . . . -9,223,372,036,854,775,807 8 bytes
double
The double-precision floating-point type, with a range of about ±10308 and about 15 significant decimal digits
8 bytes
float
The single-precision floating-point type, with a range of about ±1038 and about 7 significant decimal digits 4 bytes
char
The character type, representing code units in the Unicode encoding scheme
2 bytes
boolean
The type with the two truth values false and true
1 bit


        Rounding errors occur when an exact conversion between numbers is not possible
    double f = 4.35;
    System.out.println(100 * f); // prints 434.99999999999994
        Java: Illegal to assign a floating-point expression to an integer variable
    double balance = 13.75;
    int dollars = balance; // Error
       Casts: used to convert a value to a different type
    int dollars = (int) balance; // OK
       Cast discards fractional part.
    Math.round converts a floating-point number to nearest integer
    long rounded = Math.round(balance); // if balance is 13.75,
                                    // rounded is set to 14

The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings.
Because String objects are immutable they can be shared. For example:

     String str = "abc";

is equivalent to:

 char data[] = {'a', 'b', 'c'};
String str = new String(data);

Here are some more examples of how strings can be used:

System.out.println("abc");
String cde = "cde";
System.out.println("abc" + cde);
String c = "abc".substring(2,3);
String d = cde.substring(1, 2);


4. Basic Control Flow  (^)

Conditional statements execute a block or set of other statements only if certain conditions are met. The condition is always enclosed in round brackets.
The statements to be performed if the condition is true are enclosed in curly brackets. For example:
  
   
if (value > 5) { x = 7 };

Occasionally you may want to perform some actions for the false outcome of the condition as well. The else keyword is used to separate branches.

   
 
if (name == "Fred")
{
x = 4;
}
else {
x = 20;
};

     
For statements allow a set of statements to be repeated or looped through a fixed number of times. The round bracket contains initializer(s) ;
conditional test ; incrementer(s). If more than one initializer or incrementer is used they are separated with commas. The test condition is checked prior to executing the block.
The incrementing is completed after executing the block. For example to output #1 #2 #3 etc. on separate rows you could write:

 
for (int i=1; i<=15; i++)
{
document.writeln("#"+i);
};

Caution: For loops can be written in ways that violate structured programming principles by allowing counter variables to be used outside of the scope of the loop block
if they are defined outside the loop. Avoid the looseness of the language by always localizing the loop variable.

The enhanced for statement allows iteration over a full set of items or objects. For example:


 int[] squares = {0,1,4,9,16,25};
for (int square : squares) {...;}
// is equivalent to
for (int 1;i<squares.length;i++) {...;}


While statements
allow a set of statements to be repeated or looped until a certain condition is met. The test condition is checked prior to executing the block.
For example to output #1 #2 #3 etc. on separate rows you could write:


int i = 0;
while (i<=5)
{
document.writeln("#"+i);
i = i + 1;
};

Do While statements allow a set of statements to be repeated or looped until a certain condition is met.
The test condition is checked after executing the block. For example to output #1 #2 #3 etc. on separate rows you could write:


int i = 1;
do {
document.writeln("#"+i);
i = i + 1;
} while (i<=5);



import java.io.*;
import java.util.*;


public class task {

private static boolean uspeh = true;

public static void main(String args[]){

System.out.println( "Enter value for n : " );
int n = procitajBroj();

int zbir = 0;

while( n > 0 ){
zbir = zbir + n*n;
n--;
}

System.out.println("Result : " + zbir);

}

public static int procitajBroj() {
int result = 0;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;

do {
try {
st = new StringTokenizer(br.readLine());
result = Integer.parseInt(st.nextToken());

if(result > 0)
uspeh = true;
else
throw new Exception();

}
catch(Exception nfe) {
System.out.print("Error in input. Enter again: ");
uspeh = false;
}

}
while(!uspeh);

return result;
}
}



5. Functions  (^)

In Java program language you can use functions on a diferent way ,in Java program language methot is a "function".
You have to declare a class with the function being a method of the class. Then, to use the function, you create an object of your class(remember all objects are created using 'new' in Java). With your object, you can then call the function.

Or, you can create the class, and put the function inside it and declare the function 'static'. That allows you to use the function without creating an object of the class. Strangely enough, in java even main works like that: it is a function inside a class.

Here is an example:

class GroupOfFunctions1
{
public GroupOfFunctions1(){} //default constructor
 
public int aFunc(int n)
{
return n * 2;
}
}
 
class GroupOfFunctions2
{
public GroupOfFunctions2(){} //default constructor
 
//note the 'static' keyword:
public static int anotherFunc(int n)
{
return n * 10;
}
}
 
public class DemoFunctions
{
//main() is 'static', so java can kick off execution of
//your program without creating on object of this class:
public static void main(String[] args)
{
GroupOfFunctions1 obj1 = new GroupOfFunctions1();

int resultA = obj1.aFunc(10);
int resultB = GroupOfFunctions2.anotherFunc(10);
 
System.out.println(resultA);
System.out.println(resultB);
}
}


6. Arrays 
(^)

Java Array Declaration
An array variable is declared the same way that any Java variable is declared. It has a type and a valid Java identifier. The type is the type of the elements contained in the array. The [] notation is used to denote that the variable is an array. Some examples:

  
int[] counts;

  
String[] names;

  
int[][] matrix; //this is an array of arrays


Java Array Initialization
Once an array variable has been declared, memory can be allocated to it. This is done with the new operator, which allocates memory for objects. (Remember arrays are implicit objects.) The new operator is followed by the type, and finally, the number of elements to allocate. The number of elements to allocate is placed within the [] operator. Some examples:

   
counts = new int[5];
   names = new String[100];
   matrix = new int[5][];


An alternate shortcut syntax is available for declaring and initializing an array. The length of the array is implicitly defined by the number of elements included within the {}. An example:

    

String[] flintstones = {"Fred", "Wilma", "Pebbles"};

   
Java Array Usage
To reference an element within an array, use the [] operator. This operator takes an int operand and returns the element at that index. Remember that array indices start with zero, so the first element is referenced with index 0.


  
int month = months[3]; //get the 4th month (April)


In most cases, a program will not know which elements in an array are of interest. Finding the elements that a program wants to manipulate requires that the program loop through the array with the for construct and examine each element in the array.


String months[] =

       {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 

        "July", "Aug", "Sep", "Oct", "Nov", "Dec"};

 

   //use the length attribute to get the number

   //of elements in an array

   for(int i = 0; i < months.length; i++ ) {

      System.out.println("month: " + month[i]);

   }


7.Compilers   (^)


8.Projects and Software in Java
   (^)


9. Standard  (^)

ISO was involved in co-ordinating the development of a standard specification of Java. ISO granted Sun the privilege of proposing a "fast-track" standard. When Sun realized that they would lose control of the Java Specification as a result they backed off.

Microsoft would prefer its own version of Java and libraries to be the standard. 10,000 developers who want Java's to be a cross-platform language have formed the Java Lobby.

Sun sued MicroSoft for abusing its licence of Java by giving out a version that will work with only MicroSoft software rather than on all platforms. So far(Winter 1998-1999) Microsoft has been ordered to stop giving out their polluted version and have been forced to distribute an upgrade to the "Pure Java" version.

Now MicroSoft is promoting .NET and C# as rival tecnologies to Java.

    (^)
10. References

http://en.wikipedia.org/wiki/Java_%28programming_language%29
http://www.engin.umd.umich.edu/CIS/course.des/cis400/java/java.html
http://math.hws.edu/javanotes/
http://www.csci.csusb.edu/dick/samples/java.html
http://www.iam.ubc.ca/guides/javatut99/java/index.html