Introduction to Java

Semih Kırdinli
4 min readMay 2, 2021

In this article, I’m going to try to explain how to write code in Java programming language. This will be a series of article that begin with an Introduction to Java.

You will be learning the basics as well as the information that is not used much. These articles covers Java SE(Standart Edition).

Let’s begin.

What is Java?
- Java is an object oriented programming language inspired by the C ++ programming language.
- It is platform independent. It can work on any platform.
- Java source code is written as plain text .java files, and compiled into byte-code .class files for JVM. Java Virtual Machine must be installed on a target computer.
- JVM translates Java byte-code instructions to platform-specific code.

javac HelloWorld.java -> HelloWorld.class (compiled class)

Classes and Objects:
Class and Object are two key of object oriented concept. Java code is structured with classes.
- Class represents a type of thing or a concept, such as Car.
Each class can store some information(attributes): color, type, size.
Each class can contain program logic(behaviors): Car could go fast or slow, but could not fly.
- Object is a specific type of a class. Each object has specific values: A car has white color, another car may be black.

class HelloWorld { //attributes and behaviours }

Inheritance(extends):
-
Inheritance is an idea that you could have classes that are very generic and quiet specific.
- Generic classes describe some attributes and behaviours that should be common for many other classes.
- Specific classes can inherit these common attributes and behaviours from generic ones.

class Car extends Vehicle { // generic attributes and behaviours }
class Maruti extends Car { // specific attributes and behaviours }

Java APIs:
JDK provides hundreds of classes for various programming purposes such as String, ArrayList. Application Program Interface(API) is a term that describes a collection of classes that are designed to serve a common purpose. All Java APIs are documented for each version of language.
https://docs.oracle.com/en/java/javase/16/docs/api/

Define Java Class:
-
Class name is typically represented by one or more nouns.
- Class must be saved into a file with the same name as the class and extension .java
- Classes grouped into packages. Packages are folders where class files are saved.
- To access a class in another package, prefix the class name with the package name or import statement (java.lang.* package is implicitly assumed).

package vehicle;
public class Car { }
package anotherPackage;
import vehicle;
public class Owner { Car car; }

Use Access Modifiers:
Access modifiers describe the visibility of classes, variables, methods.
public: Visible to any other class.
protected: Visible to classes that are in the same package or to subclasses.
<default>: Visible only to classes in the same package.
private: Visible only to within the same class.

Create Main Application Class:
-
The main method is the entry point into your application.
- The method must be public, static, void. Method name must be called main and accept array of String objects as the only parameter.

public class HelloWorld { 
public static void main(String[] args) {
// program execution starts here
}
}

Compile Java Program:
Compile classes with the javac Java compiler.
The -classpath or -cp parameter points to location of the other classes that may be required to compile your code.
The -d parameter points to a path to store compilation results.

$javac -cp target/classes -d target/classes src/main/java/HelloWorld.java

Execute Java Program:
-
Execute program using java executable JVM.
- Specify -classpath or -cp to point to folders where your classes are located.
- Specify fully qualified class name. Use package prefix, don’t use the .class extension.
- Provide a space separated list of parameters after the class name.

$java -cp target/classes src/main/java/HelloWorld.java x y “z”

Since Java 11, it is also possible to run single-file source code as if it is a compiled class. JVM will interpret your code, but not compiled class file would be created.

$java src/main/java/HelloWorld.java

Comments and Documentation:
-
Code comments can be placed anywhere in your source code.
- Documentation comments:
*May contain HTML markups.
*May contain descriptive tags prefixed with @ sign.
*Are used by the javadoc tool to generate documentation.

// single-line comment/* 
multi-line comment
*/
/**
* documentation comment {@code Whatever} * @version 1.0
* @author oracle
*/

$javadoc -d <documentation path> -sourcepath <source code path>-subpackages <name of the root package>

Java Keywords, Reserved Words, and a Special Identifier:
if, else, continue, break, for, do, while, switch, case, default, private, protected, public, import, package, abstract, implements, extends, interface, class, static, final, return, transient, void, byte, short, int, long, char, float, double, boolean, try, catch, finally, throw, throws, new, this, super, instanceof, native, synchronized, volatile, strictfp, assert, enum, module, requires, transitive, exports to, uses, provides, with, opens to, true, false, null, var

See the next episode:
Primitive Types, Operators and Flow Control Statements

--

--