Introduction to Java and Object Oriented Programming
Essay Preview: Introduction to Java and Object Oriented Programming
Report this essay
Programming Theory
Basic Ideas
/* A simple example program*/
import java.io.*;
class ASimpleExample {
public static void main(String[ ] args)
System.out.println (“Hello, World!”);
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”.
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.
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.
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.
Public is a Java keyword which declares a members 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.
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.
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.
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.
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.
The method that is used in the above program is Main.
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:
Bytes of Storage taken up
Range
Example
-231231-1
i=42
-128…+127
b=65
short
-215…215-1
s=65
-263…263-1
l=65L
Fractions (decimal numbers)
Bytes of Storage taken up
Range
Example
float
-1.4 * 10^-45 to 3.4 * 10^38 99.9
double
-4.9 * 10^-324 to 1.8 * 10^308 1500
Single Characters
Bytes of Storage taken up
Example
boolean
true or false
More than one character
Bytes of Storage taken up
Example
0-65.535
Logical outcomes: true or false
Bytes of Storage taken up
Example
boolean
true or false
Variables
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.
In Java programming language, there are two kinds of variables:
Instance variables (Non-Static Fields): Their values are unique to each instance of a class.
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.
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 (“_”)