[last updated: 2019-06-13]
go to: Java
go to: SDR Study
-----
Content here comes from several sources:
tecmint.com
tutorialspoint.com
guru99.com
w3schools.com/java
stackoverflow.com/
-----
(go to:) Program Statements:
System Output:
Write to console:
System.out.println("text to print");
(go to:) Write a File:
(go to:) Calling a class from within another class:
(go to:) Importing Classes:
============================
- Definitions of terms:
- Everything in Java is an object.
- Objects have attributes and methods.
- A Java object is a combination of data and procedures that operate/work on the data.
- Objects have states and behaviors. The state of an object is stored in fields (variables), while its behavior is specified in methods (functions).
- Objects are created from templates known as classes. That is, a class must be defined before you can create an object.
- In Java, an object is created using the keyword "new".
There are three steps to creating a Java object:
Declaration of the object
Instantiation of the object
Initialization of the object
- A class is an entity that determines how an object will behave and what the object will contain. It is an object constructor, a blueprint or a set of instructions to build a specific type of object.
- ...
- Notes on general structure:
- The starting point of a Java programming project is the source file. Source files have a .java extension.
- Each source file must have one and only one public class definition.
- The filename of the source file must exactly match the name of the public class that is defined in it.
- Basic (minimal) structure of a source file:
public class class0 {
[statements] }
- Statements in a class or method definition must end with a semicolon.
- The curly braces of a class definition enclose the statements that specify the class.
Within the public class definition, there may be other, private classes defined.
- Within the curly braces of a class definition, there must be one or more methods defined.
Each public class must have a 'main' method.
(It appears this is not completely true. However it does seem to be true if you intend/expect to be able to directly execute the class, eg. by using a $ java ClassName statement.)
- Nominal format for specifying a 'main' method:
public class class0 {
public static void main(String[] args) {
System.out.println("Hello"); }
}
In this example, the method name is "main", and it is defined as a "public" method, meaning it can be accessed by anyone.
The return type is "void" which means nothing is returned.
"Strings[]" means the arguments for the method main are a String array, and the name of the argument array is "args."
"Static" will be defined later.
- Program execution flow:
- When you execute a Java program, you execute the class.
- But first the source file must be 'compiled." This can be done in Linux with a command line statement like:
$ javac ProgName.java
The javac compiler will create a file named ProgName.class
- The class file is then executed with:
$ java ProgName
- Execution starts with the first instruction in the 'main' method, and ends with the 'main' method closing curly brace.
- (go to:) Calling a class from within another class:
- (go to:) Program Statements:
- Variables in a program:
"Instance variables" are defined within a class, but
"local variables" are defined within a method.
Local variables must be initialized before they can be used.
Not so for instance variables.
.
.
.
eof