Writing our first java program and understanding it - Java Programming course - Chapter 1

Writing our first java program and understanding the terms in the program

Program:

public class MyClass

{

    public static void main(String args[])

{

    System.out.println(“This is my first program”);

 }

}

Now, we discuss all the terms in our program.

public: Here, public is called an access specifier. Using access specifiers, we can mention the level of accessibility of members and methods in a class.

class: A class can be defined as a template/blueprint that describes the behavior /state of that object.

MyClass: Here MyClass is an identifier.It is nothing but our class name. It should always start with an uppercase letter. No spaces should be given. It should be written in camelCase (it is a naming convention in which each word in a compound word is capitalized except for the first word). Here, file name must be saved as classname.java i.e., MyClass.java . If a program contains so many number of classes then file name should be saved with the class name which has main method.

static  : It is a keyword. It allows main method to be called without creating instantiable variable of the class (object).

void :  This keyword indicates that the main method (or any function)

does not return any value.

main(): Any program execution starts from main method.

String : It is a predefined class in JAVA

args[] : It is an array of objects of class String .It is used to  receive any command line arguments present when a program is executed.

System: It is a predefined class that provides access to the system & out is output string i.e., connected with console. println() is a method which is used to print message on the screen.

 

Printing Hello World!

There are many ways to print Hello World

 Method1 :

    Class Myclass{

            public static void main(String ars[])

                {

                    System.out.println("Hello World!");

                }

}

This is a general method. Now we will discuss a method which is more useful for competitive programming. But this is not in java it is in C language.But as a competitive programmers we have to know the important things.

 

#include <stdio.h>

int main()

{

long long int p=1,e=2,t=5,a=61,l=251,n=3659,r=271173410,

g=1479296389;

long long int x={g*r*e*e*t,p*l*a*n*e*t};

puts((char*)x);

}

 

So, the above snippet also gives the output as Hello World!

Explanation: When we calculate the products, G*R*E*E*T and P*L*A*N*E*T , and given that the processor is little endian, array’s in-memory representation turns out to be :48 65 6c 6f 20 57 6f 72 6c 64 21 00 00 00 00 which is “Hello World!\x00\x00\x00\x00” in ASCII.  



Do leave your doubts and queries in the comments section an follow along the course and subscribe for other posts on java and othe topics .


Comments

Popular Posts