How to start coding Java in 60 minutes

How to start coding Java in 60 minutes

Table of Contents

​​How long have you been thinking about starting coding in Java? Advised to dive deep into tutorials for endless hours, learned the basics already but not a single line of code yet? Fear not!

You will illustrate the basic syntax of Java in 60 minutes from now on. Java is a platform-independent language that means “Write Once Run Everywhere.” 

Let’s explain this a bit more; You can develop a Java application  on any device, you will have to compile it into a standard bytecode, and the produced bytecode is expected to run on any OS which has Java Virtual Machine (JVM)

Running your first Java code is simple and a fun process. 

A Simple Java Program

To run a Java program, we need to have a Java Development Kit (JDK) installed on our local machine.

How to install JDK?

macOS

Download the JDK.dmg file, jdk-16.0.1_osx-x64_bin.dmg from the Java SE Downloads page, double-click to install, and follow the instructions.

Windows

Download the JDK.exe file, jdk-16.0.1_windows-x64_bin.exe from the Java SE Downloads page, double-click to install, and follow the instructions.

When you complete the installation, check the Java version on terminal/cmd to verify the installation:

				
					$ java -version
				
			

Expected output

				
					java version "16.0.1" 2021-04-20
Java(TM) SE Runtime Environment (build 16.0.1+9-24)
Java HotSpot(TM) 64-Bit Server VM (build 16.0.1+9-24, mixed mode, sharing)
				
			

Writing your first Java program

A Java program gets executed in a specific order. So to start a program, we need to let Java Virtual Machine (JVM) know where to start execution. And all instructions are being implemented in Java classes. So we can define a Java class as a set of grouped instructions and for related data.

Java Class Declaration

				
					
public class MyFirstClass { 

}
				
				
			

This Java code needs to be in a file with same name as the class and ending with .java suffix

So let’s create a file with the name MyFirstClass.java and place the above code inside.

A Java program starts the execution from the main method;

				
					public class MyFirstClass {
   public static void main(String[] args) { 
   
   } 
}
				
			

We defined a class and a main method but the method is empty and there are no instructions to be executed. Let’s try to print something to the console and add this as an instruction to the main method.

				
					public class MyFirstClass { 
   public static void main(String[] args) { 
      System.out.println("Hello World!"); 
   } 
}
				
			

Running a Java Program

JDK has a command-line compiler with the name javac.

To be able to run a Java class; we need to compile source code into Java bytecode first and then start the execution. 

To compile our source code into the Java bytecode; in the terminal go to the folder you saved the file MyFirstClass.java and run the below code. 

				
					$ javac MyFirstClass.java
				
			

This will create a MyFirstClass.class file in the same folder with your class name which can be used for the execution. 

Trigger the execution:

				
					$ java MyFirstClass
				
			

Output:

				
					Hello World!
				
			

Congratulations! You are at the point where Java begins. The keys to learning a programming language are practice and consistency.

I strongly suggest you practice with the Java coding challenges on Exercism.io. Exercism is a great platform with lots of free, gamified scenarios for you to improve your Java skills until our next post! Don’t forget to share this post on social media!

9 Responses

  1. تنبيه: Exceptionly
  2. تنبيه: Exceptionly
  3. تنبيه: Exceptionly
  4. تنبيه: Exceptionly

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *

arArabic