OtherPapers.com - Other Term Papers and Free Essays
Search

Introduction to Java and Object Oriented Programming

Essay by   •  January 11, 2012  •  Study Guide  •  2,645 Words (11 Pages)  •  1,592 Views

Essay Preview: Introduction to Java and Object Oriented Programming

Report this essay
Page 1 of 11

A. Programming Theory

1. Basic Ideas

/* A simple example program*/

import java.io.*;

class ASimpleExample {

public static void main(String[ ] args)

{

System.out.println ("Hello, World!");

}

}

1(a) The phrase "A simple example program" that is put between the delimeters it is ignored by the compiler because it is a simple comment. The effect of writing anything between these delimeters is that we can comment our program without affecting the execution of the program. The '/*' means "start comment" and the '*/' means "end comment".

1(b) The import java.io* means that the program imports all the static members of a class that are defined in java.io package. This enables our java program to use those classes and their methods to achieve some task. We can use this statement, if we need more than one file to be imported in our program. We importing the external classes we use in our code. This particular import happens to be the Java Input/Output package containing the .class files for reading and writing data. If we use one of the classes in this package without the import statement, the compiler would be unable to find it and would output an error message.

1(c) The purpose of the asterisk is indicating that all static members of the specified class must be available for use in the classes declared in the file.

1(d) A 'Class' is a prototype from which objects are created. The distinction between an object and a class is that a class can be thought of as a type, category or definition. However, an object is an instance of a class.

1(e) Public is a Java keyword which declares a member's access as public. It is a standard Java method, but we have to call this method when a class reference is given on the command line, as above. Public members are visible to all other classes. Any other class can access a public field or method. It indicates visibility, which means that everyone can see this class.

1(f) The 'Static' keyword means that this method exists without the need for an instance of an object of this class. It means that the method 'main' can be called without making a new variable of type HelloWorld. In this case it is important because 'main' is the first method to run. And we can call them directly without creating an object from a class by putting its class name in front of a variable and a method name in between the dot operator.

1(g) The 'Void' basically signifies that the method will not have a return value. It is a java keyword and it is used at method declaration and definition to specify that the method does not return any type; the method returns void. It returns nothing.

1(h) The 'Main' is the first method which the JVM executes. It indicates the place in the program where execution begins. When the program runs, it starts by executing the first statement in main and it continues until it gets to the last statement as they are needed and then it stops.

1(i) We put a semi-colon in the program above, because in Java all statements end with a semi-colon (;). If we leave the semi-colon out, the compiler will complain! Semicolons in Java end all lines of the program other than control constructs like for, if and while or function and class declarations. Everything before the semicolon is one line of code even if it spans many lines of text.

1(j) The method that is used in the above program is Main.

2. Data Types

The basic types are also known as primitive types and when a variable is declared as any of them, the computer creates what you can call a 'box' in its memory that is big enough to hold a value of that type.

Whole (counting) numbers:

Name Bytes of Storage taken up Range Example

2.1 int 4 -231...231-1 i=42

2.2 byte 1 -128...+127 b=65

2.3 short 2 -215...215-1 s=65

2.4 long 8 -263...263-1 l=65L

Fractions (decimal numbers)

Name Bytes of Storage taken up Range Example

2.5 float 4 -1.4 * 10^-45 to 3.4 * 10^38 99.9

2.6 double 8 -4.9 * 10^-324 to 1.8 * 10^308 1500

Single Characters

Name Bytes of Storage taken up Example

2.7 boolean 1 true or false

More than one character

Name Bytes of Storage taken up Example

2.8 char 0-65.535 65.5

'Logical outcomes': true or false

Name Bytes of Storage taken up Example

2.9 boolean 1 true or false

3. Variables

3.1 A variable is a container that stores a meaningful value that can be used throughout a program. It is an important concept in Java language. It causes a compiler to set aside some memory at the time of compilation. Variables are used in a Java program to contain data that changes during the execution of the program.

3.2 In Java programming language, there are two kinds of variables:

a. Instance variables (Non-Static Fields): Their values are unique to each instance of a class.

b. Class Variables (Static Fields): It is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence; regardless of how many times the class has been instantiated.

3.3 The names that would be allowed for a variable are (a), (c) and (d) because they cannot use spaces or other symbols, or start with a digit. They can start with a letter, an underscore ("_")

...

...

Download as:   txt (18.8 Kb)   pdf (231.2 Kb)   docx (18.4 Kb)  
Continue for 10 more pages »
Only available on OtherPapers.com