Java for beginners-I

Dul’s Blog
2 min readDec 28, 2020

hello world with java …make errors and become better, accidentally success doesn't help us to think like a compiler

Let’s compile our first code. I hope you have installed the Java Development Kit, and Java Runtime Environment. Then open up a text editor and type up this code.

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

System.out.println (s.o.p) tells the computer to display the output of what is written inside the brackets of s.o.p, “” marks.Now save this code as helloworld.java

In the command prompt or terminal .Type the following lines to compile the file.

javac helloworld.java

Next let’s run our generated class file

java helloworld

The output you should obtain

Hello World!

Suppose you saved your code in the text editor as world.java

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

Next you try to compile the file

javac hello.java

But you would get output as:

Error: Could not find or load main class hello
Caused by: java.lang.ClassNotFoundException: hello

Hence to prevent getting this error remember to give same name to the class as your filename. In my case class is helloworld, filename is helloworld, then no error.

Now try to run this code :

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

save the file as helloworld.java, yet in this code there is no class. Anyways let’s try compiling the file:

javac helloworld.java

Following errors resulted.

helloworld.java:2: error: class, interface, or enum expected
public static void main(String[] args) {
^
helloworld.java:4: error: class, interface, or enum expected
}
^
2 errors

Have fun coding…

--

--