Ok let us jump in. For this tutorial we are not going to use an IDE. Create a file anywhere on your computer. I am going to create it in a folder called blog/post2 in my home folder.
I am on a mac, but you can choose any operating system. The commands you see above are linux commands.
pwd
- This tells me in which directory I am.ls
- This tells me the list of files in the current directory.Please note the way I have named my file.
I know at this point what you are thinking. What is a class? I will explain all that, but let's create a program first. Open your newly created file and paste this(No color needed, I am just coloring to explain a few things).
public class MyFirstProgram { public static void main(String args[ ]) { System.out.println("Welcome to java programming"); } }
After you are done, save it and open a terminal/command line. You do not need to install it, you probably have it installed. After you are done, type javac MyFirstProgram.java
Now you have two files instead of one. The .class file is the compiled version of your java program. Now lets run it.
Magic isn't it? Not convinced, try changing "Welcome to java programming" with any other string, Make sure to include the quotes and repeat and then come back.
Yes we just created a program to print something on the screen. Now let us understand what we wrote.
Public - Public tells the compiler that this class/method should be visible everywhere and and accessible to everyone. We have a few different types which we will discuss in blog posts to come.
Class - Everything in the java world is composed of classes and objects. A class is a template or blueprint which tells, how the object looks like. It tells what are the properties of the object and what magic it can do.
MyFirstProgram - It is same as our file. The name of the public class should always be same as the file name
{ ... } - Define the sections or blocks of code.
static - Let us ignore this for now. We will cover this in a future blog post.
void - The method does not returning anything back. (Ignore this for now, if you do not understand it, we will cover this as well)
main - Main is the name of a method. A method is something that describes what an object can do. A method is also called function in some languages. It can have 0 or more parameters passed to it. The main method is special. It is the first method that is called when a program is run. It is kind of an entry point to the program. It is always static and its return type is always void. It takes only one parameter- An array of type string denoted by - (String args[ ]). The array can be named anything, it doesn't always have to be named args. Anything that has [ ] next to it is an array.
Ok you must be thinking what is an array?
An array is a group of objects that live together in memory and are accessible by their relative position to the first object. Confused?
Ok let us say args[] has 4 objects. Then you can access first object as args[0], second as args[1], third as args[2] and fourth as args[3].
Elements in an array are all of the same type. The main method accepts an array of the type String. More on parameters and types in the posts to come.
System.out.println("Welcome to java programming"); - This is a statement. A statement in java ends with a semicolon. System is a class, out is a static object in System class and println is a static method in out class. The println method prints whatever is passed to it followed by a new line on the screen. To understand what followed by new line means, change System.out.println("Welcome to java programming"); to System.out.print("Welcome to java programming"); in your program, compile it and run it.
This is all for today, See you next time.
Nothing yet..be the first to share wisdom.