Java Basics

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

  1. Launch Eclipse.
  2. Close the welcome page.
  3. Create a new Java project via File > New > Java Project.



  4. 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.

      Graphical user interface, application

Description automatically generated

  5. Next is to create a new class. Right-click project > New > Class.




    Enter HelloProgram in the Name field, then click Finish.

    Graphical user interface, text, application

Description automatically generated

  6. Enter codes below in the HelloProgram class.
  7. To run the program, go to Run > Run.

    A picture containing graphical user interface

Description automatically generated

  8. 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

abstractcontinuefornewswitch
assertdefaultgoto*packagesynchronized
booleandoifprivatethis
breakdoubleimplementsprotectedthrow
byteelseimportpublicthrows
caseenuminstanceofreturntransient
catchextendsintshorttry
charfinalinterfacestaticvoid
classfinallylongstrictfp*volatile
const*floatnativesuperwhile

Notes:

  • In Java, the const keyword is a reserved word but is not implemented. Attempting to use it will result in a compile-time error. Instead, Java uses the final keyword to declare constants.
  • No, Java does not have a goto statement; it is a reserved keyword that is not used in the language. The goto statement 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 strictfp keyword 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 strictfp modifier 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. 

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