logo codesdope

Let's program


Let's start the beautiful journey of Java. We will start by explaining the previous code which printed "Hello World" on the screen for you.

class Hello{
  public static void main(String[] args){
    System.out.print("Hello World\n");
  }
}
Output
Hello World

You won't be able to understand the code thoroughly in this first chapter. But you will get everything gradually. So, we will start explaining here only the basics so that at least you can start to code.

class Hello - 'Hello' is just the name of a class ( full details of the class will be explained later ). There could be any other name of the class like first_program, my_code, etc. But while compiling, you also need to execute your main class. E.g.- for linux, type javac filename.java ( filename is the name of your file ) then java Hello ( as Hello is your main class ( as it contains the main method ) ).

public static void main(String[] args) - It is the main method ( you will know about methods later ). Its significance is that whenever you run your code, the main method is executed first. So, you won't be able to execute your code without the main method. So, for now just learn to write this way, these things will be explained in later chapters.

System.out.print() - print is a function which is available in System directory. It is used to display something on the screen.

function in java

So, the main thing you have to focus on here is how to write your codes. You have to follow this format:

class CLASSNAME{
  public static void main(String[] args){
    STATEMENT
    STATEMENT
    STATEMENT
    ....
  }
}

In Java, we end our statements with ';' ( semicolon ). Notice ';' after the end of System.out.print();

{ } - '{ }' represents the body ( content ) of class or method in Java. So, it represents what things are inside which class or method. E.g.- class CLASSNAME { main method{ statement } } - 'main method' and 'statement' are both inside the 'CLASSNAME' class ( as both are inside '{}' of the class). 'statement' is also inside the 'main method' ( as it is inside '{}' of the 'main method' also ).

{} braces for body in java

Let's have a newline

See this code:

class Hello{
  public static void main(String[] args){
    System.out.print("Hello\nWorld\n");
  }
}
Output
Hello
World

See the difference here. After "Hello World", a new line is printed.

'\n' is the newline character. You can think like - while printing "HelloWorld\n", when Java encounters 'H', it prints 'H' on the screen and when 'e', then 'e' and when it encounters '\n', it comes to a new line.

Try this:

class Hello{
  public static void main(String[] args){
    System.out.print("Hello World\n\n");
  }
}
Output
Hello World

Observe the result yourself.

Let's do the same thing in a bit different way by using 'println' function instead of 'print'. 'println' automatically prints a new line at the end without having '\n'. Let's see an example on this.

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

So, println("Hello World") is same as print("Hello World\n").

{} braces for body in java

Commenting


Comment is some text written inside code which is just ignored by the compiler while compiling your code. Comments are written to make our code more readable. These are written within '/* */' or after //.

Why comments

As mentioned earlier, it makes our code more readable. Assume that you have written a software and after releasing it, you hired few good programmers for maintenance. Without comments, it would be a very difficult job for them to understand your code. And most of the time it happens that the person who has written a code is not the one who is going to modify it. So, make a habit of writing comments.

Comments are written after //. These are single line comments. It means that if you change the line, then the new line will not be the part of comment.

//This is single line comment
//This is also a comment
class Hello{
  public static void main(String[] args){
    System.out.println("Hello World");
  }
}
Output
Hello World

Comments can be multilined also as used in the example given below. But we can't put one comment inside another e.g.- /* This is a /*comment*/ */ is invalid.

/*Hello World code*/
/* Comments will not be compiled and will be ignored */
/* It is a
multiline
comment*/
class Hello{
  public static void main(String[] args){
    System.out.println("Hello World");
  }
}
Output
Hello World

Join

+ is used to join two strings. The example given below will make it clear.

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

'Hello ' and 'World' are two different strings here. And '+' joins them together.

Try one more example

class Hello{
  public static void main(String[] args){
    System.out.println("Codes"+"Dope");
  }
}
Output
CodesDope

Coding is all about practice and solving questions. So, solve questions as much as you can. Solve questions after completing every chapter then only go to the next chapter.

In future, when writing long codes, make sure that you run your code from time to time while writing instead of completing your whole code and running at last. This will make debugging your code easier if you have made some errors.

In theory, there is no big difference between theory and practice. But in practice, there is.
-Yogi Berra

Share what you know
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.