Java Core: Getting Started

What is Java?

Java is one of the most popular programming languages used to build a software for any type of devices. Including mobile, computer, TV, vehicles, and more. And it is also one of a great example of platform-independent language. Meaning, java program can run in any type machine or operating system with the help of JVM.

Java is pretty known for its object-oriented programming (OOP) structure. Therefore, this structure focuses on the data(object) and interfaces of that object. Refer to the link below for OOP tutorial:

What is JVM or Java Virtual Machine?

JVM is a virtual machine for the java programming language. Therefore, during java compilation, JVM generates bytecode to send it as a message to the device. This bytecode is translated into machine code to easily interpreted by the device.

We are not going deep deeper into this topic. Therefore, this section will only show how the java code works inside of the device with the help of JVM.

JVM Structure

For example, you write a line of code in java like the source code below:

System.out.print("Hello World");

Java compiler converts the java source code into bytecode. Therefore, the compiler produces a new file called java class. And it contains the bytecode that will process inside of RAM with JVM. Additionally, JVM will help the machine to interpret the java bytecode. Therefore, if compilation is success the result will display on screen. Like for the example code above “Hello World” will be displayed.

Install Java

Before we start, we must know which version of java you should install. Therefore, the latest version of java is 13 or JDK 13, it was released on 10th September 2019. However, it doesn’t mean that, you should install the latest version, but, it’s up to you.
I recommend Java 8 since this tutorial’s examples are written in that version.

What is JDK?

JDK stands for Java Development Kit, and it contains a package for building java application. Including Java Virtual Machine, Java Runtime Environment, and other Java libraries

You can refer to this link for Java 8 or JDK 8 installer: https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

Note: Before you install the JDK make sure it is matched to your Operating System specs. For example, your OS specs is 64-bit Windows OS then you should install and download JDK for 64-bit Windows

Compile Java Code

There are two ways of compiling java code, first is in IDE and second in the terminal. Follow steps below on how java code compiles on your computer.

Compile Java code in Terminal

Windows
  1. Open Terminal / Command Prompt
  2. Create a new folder for your java code, execute this script: “mkdir HelloWorldDir”
  3. Move into the new folder “HelloWorldDir”, execute this script: “cd HelloWorldDir”
  4. Create a java file, execute “notepad PrintHelloWorld.java” then the confirmation message will popup, click yes to create a new file.
  5. to open the java file re-execute on step number 4: “notepad PrintHelloWorld.java“.
  6. Copy-paste the code below to your java file :
public class PrintHelloWorld {
	
	public static void main(String[] arg){
		System.out.print("Hello World");
	}
}
  1. 7. Press ctrl + s to your keyboard to save the file.
  2. 8. Now compile the program by executing this script: “javac PrintHelloWorld.java” into your Terminal.
  3. 9. Enter “java PrintHelloWorld” to run it.
  4. You will see the “Hello World” message display on the terminal.

Note: If you encountered this message: "javac is not recognized as an internal or external command" . The first thing you need to do before compiling the java code. You should add the JDK path into your system environment, so JDK compiler can find it. Go to the “Advanced system settings”, click “Environment Variables”. And then look for the “Path” in “User variables” section. At the end of the “Path” value add JDK path together with semicolon at the start and end of it. For example ";C:\Program Files\Java\jdk1.8.0_241\bin;" (the semicolon serves as a separator of each path), click ok button and restart your terminal.

Mac OS
  1. Open Terminal / Command Prompt
  2. Create a new folder for your java code, execute this script: “mkdir HelloWorldDir”
  3. Move into the new folder “HelloWorldDir”, execute this script: “cd HelloWorldDir”
  4. Create a java file, execute “touch PrintHelloWorld.java”
  5. to open the java file, execute “nano PrintHelloWorld.java”.
  6. In the Nano editor Copy-paste the code below:
public class PrintHelloWorld {
	
	public static void main(String[] arg){
		System.out.print("Hello World");
	}
}
  1. 7. Press Ctrl + x on your keyboard to save the file and exit Nano.
  2. 8. Now compile the program by executing this script: “javac PrintHelloWorld.java” into your Terminal.
  3. 9. Enter “java PrintHelloWorld” to run it.
  4. You will see the “Hello World” message display on the terminal.

Compile Java code in Eclipse IDE

You can refer to this link for more information about IDE and Eclipse: http://lgeratech.com/2019/11/how-to-become-a-full-stack-developer/#IDE

To install the eclipse go to this link and download Eclipse Installer: http://www.eclipse.org/downloads

When the download is completed, double click the file and click “Eclipse IDE for Java Developers”:

And then click the INSTALL button after the next form will pop up, once the installation is done click LAUNCH button to run the eclipse application:

  1. Open Eclipse IDE
  2. Create a new project, click the “File” menu then select “New” menu item, then click “Java Project
  • 3. Enter the project name e.g. “HelloWorld” then click the Finish button.
  • 4. Add a new file for the java code, right-click src folder into your project folder on the Package Explorer section, then select New menu item and click Class
  • 5. Once Java Class form is displayed, enter the Name e.g. “PrintHelloWorld”, then click/check the “public static void main(String[] args)” checkbox, and click the Finish button to create the file.
  • 6. Copy the code below to print Hello World in eclipse console:
public class PrintHelloWorld {
	public static void main(String[] args) {
		System.out.println("Hello World");
	}
}
  • 7. Press Ctrl + x on your keyboard to save the file
  • 8. To run the java code, press ctrl + F11 on your keyboard
  • You will see the “Hello World” message display on the eclipse console.

Java Basic Syntax

This section explains the program of the PrintHelloWorld java file.

Access modifiers

The java access modifier defines the accessibility of the class, constructor, methods, or variable within the java project.

On the example code below, we use the public access modifier to access the class in any area of the project.

refer to this link for more information and example about the java access modifiers: http://lgeratech.com/2020/02/java-access-modifiers-the-easiest-way-to-understand/

Java Class

Java class is a blueprint of an object, it carries the identity and the behavior of an object, Java object is base on the real-life object like a person, thing, and animal, in technically speaking the class contains the whole code of the java file including fields, methods, constructor and more.

Without the class, the codes in java will not work, to declare a class in java it should be same as the java file name, as the example code we used:

Java uses camel case for writing names of variables, methods, and classes, in the example code above we used camel case to the class name PrintHelloWorld, every class name should start with a capital letter and if the name is more than one word, the first letter of each word should be capitalized:

Main Method

The main method in java is the starting point of the program, java program can’t run without main method:

The declaration of the main method cannot be changed except to the parameter, and if the main method declaration is incomplete, the JVM will not able to detect it.

Static 

The non-static variables and methods in java should use the object instance, but when variables and methods are static, the object instance is not necessary:

Non-static Variables and Methods
public class Person {
	
	public String name = "Joe Joe Joe";
	
	public String getName() {
		return name;
	}
	
	public static void main(String[] args) {
		Person person = new Person();
		System.out.println(person.getName());
	}
}

As you can see the person object was instantiated (Person person = new Person();), to use the method getName() .

Static Variables and Methods
public class Person {
	
	public static String name = "Joe Joe Joe";
	
	public static String getName() {
		return name;
	}
	
	public static void main(String[] args) {
		System.out.println(Person.getName());
	}
}

As you can see, the person object instance was removed, it simply used the class name Person to used the getName() method.

Refer to the link below for more java tutorials:

http://lgeratech.com/category/java/

2 thoughts on “Java Core: Getting Started

  • SEO Services
    January 30, 2020 at 1:37 am

    Awesome post! Keep up the great work! 🙂

  • curry 5 shoes
    August 9, 2020 at 11:02 pm

    Whats up! I simply wish to give an enormous thumbs up for the great info you will have here on this post. I will probably be coming back to your blog for extra soon.

Leave a Reply

Your email address will not be published. Required fields are marked *.

*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.

BCF Theme By aThemeArt - Proudly powered by WordPress.
BACK TO TOP