The History and Philosophy of Java
- Java was conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystem, Inc. in 1991.
- It took 18 months to develop the first working version.
- The objective is to create a platform-independent/non-propriety programming language.
- This language was initially called “Oak” but was renamed to “Java” in 1995.
- Based on C++ programming language.
Object-Oriented Programming
- OOP is at the core of Java.
- The 3 OOP principles;
- Encapsulation – the mechanism that binds together code and the data it manipulates and keeps both safe from outside interference and misuse.
- Inheritance – the process by which one object acquires the properties of another object.
- Polymorphism – a feature that allows one interface to be used for a general class of actions.
Java Development Kit (JDK)
The Java Development Kit is an implementation of either one of the Java Platforms; Standard Edition, Enterprise Edition, of Micro Edition platforms released by Oracle Corporation in the form of a binary product aimed at Java developers on Linux, MacOS of Windows.
Java Runtime Environment (JRE)
The Java Runtime Environment is a software layer that runs on top of a computer’s operating system software and provides the class libraries and other resources that a specific Java program needs to run.
Java Virtual Machine (JVM)
A Java Virtual Machine is a virtual environment that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java binaries (bytecodes). The JVM is detailed by a specification that formally describes what is required in a JVM implementation.
Although Java programming language is platform-independent, however the JVM itself is platform-dependent.
A First Simple Program
- Launch Eclipse.
- Close the welcome page.
- Create a new Java project via File > New > Java Project.
- In the New Java Project wizard, enter below details;
- Project name: HelloProject
- Module: unchecked
- Leave everything else to default settings and then click Finish to proceed.
- Next is to create a new class. Right-click project > New > Class.
Enter HelloProgram in the Name field, then click Finish. - Enter codes below in the HelloProgram class.
- To run the program, go to Run > Run.
- The string “Hello World!” should be displayed in the console/output.
public class HelloProgram {
public static void main (String[] args) {
System.out.println("Hello World!");
}
}
Java Basics
- Java programs are enclosed in a pair of curly brackets that indicates a block of codes.
- Every statement in Java must ends with a semicolon.
- Java is case-sensitive.
- Whitespace in Java program is ignored.
Indentifiers
Identifiers are used to name things; such as classes, variables, methods, etc.
- Must begin with a letter (alphabet), underscore, or a dollar-sign $.
- Cannot start with a number.
- A single-word.
- Case sensitive.
- Not a Java keyword.
Comments
- // for a single-line comment.
- /* */ for a multiple-lines comment.
- /** */ also for a multiple-lines comment, also known as javadoc comment.
The Java Keywords
| abstract | continue | for | new | switch |
| assert | default | goto* | package | synchronized |
| boolean | do | if | private | this |
| break | double | implements | protected | throw |
| byte | else | import | public | throws |
| case | enum | instanceof | return | transient |
| catch | extends | int | short | try |
| char | final | interface | static | void |
| class | finally | long | strictfp* | volatile |
| const* | float | native | super | while |
Notes:
- In Java, the
constkeyword is a reserved word but is not implemented. Attempting to use it will result in a compile-time error. Instead, Java uses thefinalkeyword to declare constants. - No, Java does not have a
gotostatement; it is a reserved keyword that is not used in the language. Thegotostatement is generally considered harmful to code readability and maintainability, leading to “spaghetti code,” which is why it was deliberately excluded from Java’s design. - The
strictfpkeyword in Java was historically used to ensure that floating-point calculations produced the exact same results on all platforms by enforcing the strict IEEE 754 standard.- However, as of Java 17 and later, the
strictfpmodifier is obsolete because all floating-point operations are now consistently strict by default, making the keyword’s presence or absence have no effect at run time. Using it in modern Java will trigger a compiler warning.
- However, as of Java 17 and later, the
The Java Class Libraries
Also known as the Java API, the official reference of all Java classes. It can be found here https://docs.oracle.com/en/java/javase/21/docs/api/index.html