How does one write their first java program?

We are going to make our computer say,

Hello Human, I am your servent..

I’ll do as you please!

I bow down to you master. 

But (I like big butts and I cannot lie, sorry I can’t stop singing that song).. but before that, we need to do some shit called “setup” for writing our first java program.

Your first Java program is going to look lengthy and wordy compared to other programming languages such as Python. So, I say fuck setting up your computer for now. Heck, Java’s “hello world” scares off people even if it is dumb easy just due to sheer length. Now, if I made you download a bazillion files and made you follow 10 different step to set up things, you are going to run the fuck away. 

My intention is to show you, Java is a fucking easy language. It’s not C++ which you’re only qualified to write if you’re a nerd with no sex life (ba dum diss). So, I suggest you go over here. 

An online coding environment will save us from so many steps that will come between you and your code. Fuck that for now. 

You talk a lot, show me the code 

Okay. Just in a bit..

See.. a java program has a structure to it that looks like,

java program structure

So, a java program is a bunch of thingies called classes that have code inside. That’s all to it for now. 

How does one define a class?

All you need to do is use a class keyword. 

class myClass{

//code

}

Okay, how does one write the program you mentioned above?

public class IAmTheBoss {
public static void main(String args[]) {
System.out.println("Hello Human, I am your servent..");
System.out.println("I'll do as you please!");
System.out.println("I bow down to you master.");
}
}

In JDoodle it’ll look like this if you hit the execute button. 

first java program

Explain the code, please

I knew you’d ask. Let’s break down the code. 

Iteration 1

public class IamTheBoss{

}

This is our class definition. The public here is something called access modifier. Ignore what that means for now. Just think of it like our class can be accessed from anywhere within the same folder if it’s set to public.

Iteration 2

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

} // main ends here
} //class ends here

Java programs require something called the “main method” to run. The class that has this main method is called the main class. You need one main class in your java program.

Now methods are synonymous with functions. But if you don’t know what “function” means don’t sweat it. We’ll have a whole article on that. 

public static void main(String args[])  is a way to define the main method in java. Now, each of the words there such as static, void, String etc. have certain meanings which we’ll explore in some other article. For now, just understand that it’s needed to define the main method thingy. You don’t need to rote memorize it either. If you type main and press tab on Jdoodle, it’ll generate the code for you. 

Iteration 3

public class IAmTheBoss {
public static void main(String args[]) {
System.out.println("Hello Human, I am your servent..");
System.out.println("I'll do as you please!");
System.out.println("I bow down to you master.");
}
}

System.out.println displays the output on the screen. If you type println and press tab on Jdoodle then it’ll generate the whole thing for you. println means the thing you want to display is going to be printed on a new line. So, you could do System.out.print but everything would be on the same line. 

There’s a reason behind x.y.z kinda shit in Java. It means z exists inside y which exists inside x. So, System.out.print thingy means that the “print” function resides inside “out” which further resides inside the “system”. 

So, what else can I do?

I suggest you play around with Jdoodle. And see what works and what does not. And subscribe/bookmark to this blog to read more articles on Java.

Leave a Reply

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