How to begin with JAVA

Neri Shrestha
4 min readJan 14, 2022

Let’s begin with the introduction.

What is Java?

Java is an object-oriented programming language, created by James Gosling in 1996 and owned by Oracle. It is case-sensitive in nature.

Application area of Java

  • Mobile applications (mostly Android apps)
  • Desktop and web applications
  • Web servers and application servers
  • Game development
  • Database connection, etc.

Why use Java?

  • Java is an platform independent language. It is flexible with Linux, Windows, Mac, and so on.
  • It is free open-source language which is easy to learn and simple to use.
  • It is secure, fast and powerful language with tens of millions of developers all across the world.

Lets start with the simple Java file, and find out what is happening

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

Java programs only runs the code that is enclosed within the class. In the example above, Main is the name of class. The name of class should always have the uppercase first letter and use PascalCase naming convention.

The name of the java file must match its class name. Taking the above example, the java file should be named Main.java as the class name is Main.

The main Method

The main() method is required in every Java program and can be found here:

public static void main(String[] args)

The code within the main() method is executed in runtime.

System.out.println()

println() method can be used inside main() method which helps to print a line of text.

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

The S in the System.out.println() should always be in uppercase as Java is case-sensitive programming language.

Congratulations, this way you will be printing your first Java program.🎉

Now let’s dive into the Java comments.

Java Comments

Comments are used to understands the code. It usually explains the code used and make it more understandable. It can help you a lot if you are working with a team or if you are the only individual working on it, it helps you to know what is being executed even after long period of time.

Types of Comments

Basically, there are two types of comments :

a. Single line comment

Single line comment starts with two forwarded slash (//). Any text after this two slash is not executed.

// This is a comment 
System.out.println("Hello World");
OR System.out.println("Hello World"); // This is a comment

In both of the cases //This is a comment is not executed as it is written with two forwarded slash in the beginning.

b. Multi line comment

Multi line comment starts with /* and ends with */ . Any text between /* and */ are simply ignored by the Java compiler.

/* Hi, I am a useless line and 
will simply be ignored by Java */
System.out.println("Hello World");

Java Variables

Variables are like the containers that store the data values. There are many types of variables available. Some of them are listed below:

  • int : stores whole number without decimals like 100 or -200
  • float : stores number with decimals like 5.5 or -1.3
  • char : stores single character which should be bounded within single quotes like ‘a’ or ‘A’
  • String : stores text and are bounded by double quotes like “Hello” or “World”
  • boolean : stores value with two stats either true or false

Declaring Variables

It is not possible to declare variable without assigning it with datatypes and its value.

//Syntax
type variable = value;

Lets understand what is going on in this syntax code. Here type refers to the datatypes (such as int or float), then the variable is the name of variable that is assigned by us (such as x or name). The equal sign is used to assign value to the variable through value.

This is the general syntax, lets have some look at the real examples using some of the datatypes.

int num = 9;
float dec = 9.50f;
char ch = 'R';
boolean bool = true;
String text = "Hello World";

Now, lets get deeper with some more example of int type:

Create a variable called num of type int and assign it the value 100:

int num = 100;
System.out.println(num);

Declare a variable without assigning the value, and assign the value later:

int num;
num = 100;
System.out.println(num);

Assign a new value to an existing variable:

int num = 100;
num = 200;
System.out.println(num); //now the program will print 200 as number

Final variables

If you don’t want others (or yourself) to replace existing values, you can use the final keyword (which declares the variable as “final” or “constant,” which implies unchangeable and read-only):

final int num = 100;
num = 200; // will generate an error: cannot assign a value to a final variable

Happy Learning!

The best way to fail at being a programmer is to spend all your time writing a program and debugging it rather than learning the algorithms and understanding it.

Stop procrastinating. Start practicing. And have fun.

--

--