back to top

How to Write Code in Java and the Basic Things You Need to Know About Java Syntax

Follow Us
placeholder text

It’s time to write some Java code, but first, make sure you’ve completed the Java setup. Before you go ahead, you need to learn something, so for every Java file, it will appear with a.java extension. And it is a case-sensitive language, so ensure whatever you write is cross-checked. Without further ado, let’s jump to writing code.

Where to Write Code and Basic Prerequisites to Know

We will be focusing on the main Java, here you go…

  • Under Project Directory, you will find SRC, and within the SRC directory, you will find your package name. There you have your main, where you have to write code.
  • The first line of code shows where you are writing your code.
  • Next line will be The nexts. In Java, we write code under class. **So whatever you have, you have to write it under class and fall under the same class. You can rename or change it accordingly, and the file will be saved. java extension, which appears at the top of the tab.)
  • Under Main Class or any Class, the code referred to as Functions ** A function is like a piece of code that does some work, like if you press the Num key on your keyboard, it will enable the Num keypad. It’s like you have commanded it to perform a certain task with instructions to finish the task.
  • Whenever a code is executed, a function call is used. We will first learn about the main function; later, we will cover what the methods are and how you can create more functions.
  • What is written in grey or with // its called a comment. Which isn’t a part of the code and will be ignored by the compiler while executing the project. ** For your own help, you write whatever you want accordingly in your language. 
  • To write the comment in multiple lines, start with /**/ and press enter in the /**/. To better understand, refer to the below image.

How to Write Hello World

We are beginning to write our first programme, and the first thing that we are going to write is Hello World. A simple programme, and everyone’s favourite code to start with.

  • Let’s start with writing the output statement System.out.println(“Hello world!”);
    Here, println itself is a function. Which is to be written under class. Therefore, we also use ” (semicolon) to indicate our line is ending.
  • To execute the code, if you are a first-time user, you might not be able to run from the Run button, which appears on the upper-right, because you haven’t chosen which file you have to run. For this, use the navigation bar on top, and then click on Run and choose the 2nd Run option. Unless you do this, the run won’t be executed and will show grey. Either way, you can use Alt + Shift + F10.
  • Choose the file you want to run, then choose Main.
  • Your code output will be at the bottom, and yes, we have successfully run our Hello World.

Note: code that doesn’t have any errors. Our exit code will be 0 if there is an error, and the code will be 1.

Things to take away

Instead of using print, you can also use print, and the output will be the same, but there is a slight difference. There won’t be any line space. So whenever you use print, there will be a line space, and with print, there won’t be any.

Another thing is a quick command to use print. Simply write south and then enter the tab to enter. So it helps you to make things easier and doesn’t require you to write the statement (System.out.println()), respectively.

So, this is how you can write codes and some basic things you need to remember in Java Syntx. You can learn more by clicking on the following link to jump to the tutorials. We are open to feedback, and we try to improve the article by updating, commenting, and sharing your feedback in the comment section down below.