Professional Documents
Culture Documents
Introduction To JAVA - 2 - 2
Introduction To JAVA - 2 - 2
LECTURE TWO
INTRODUCTION TO JAVA
1. Naming convention
2. Class
3.Program Structure
class Computer {
Computer() {
}
void computer_method() {
}
public static void main(String[] args) {
} /* main method in the class, its syntax wont change in any class and can
appear in many classes of one single source code, you just call a class with a main
you want it to be executed */
}
class Laptop {
Laptop() {}
void laptop_method() { }
}
a. Line comments: It starts with two forward slashes (//) and continues to the end of the
current line. Line comments do not require an ending symbol.
b. Block comments start with a forward slash and an asterisk (/*) and end with an asterisk
and a forward slash (*/).Block comments can also extend across as many lines as needed.
b. static: static is a reserved keyword which means that a method is accessible and usable
even though no objects of the class exist.
c. void: This keyword declares nothing would be returned from the method. The method
can return any primitive or object.
c. println: It is utility method name which is used to send any String to the console.
d. “Hello World from Java”: It is String literal set as argument to println method
6. Import statements
This line indicates that if you want to use a class of another package, then you can do this
by importing it directly into your program.
Example:
import calc.add;
Example:
//Name of this file will be "Hello.java"
Program Output:
Hello Java
/* Comments */
The compiler ignores comment block. Comment can be used anywhere in the program to
add info about program or code block, which will be helpful for developers to understand
the existing code in the future easily.
Braces
Two curly brackets {...} are used to group all the commands, so it is known that the
commands belong to that class or method.
public static void main
●When the main method is declared public, it means that it can also be used by code
outside of its class, due to which the main method is declared public.
●The word static used when we want to access a method without creating its object, as we
call the main method, before creating any class objects.
●The word void indicates that a method does not return a value. main() is declared as void
because it does not return a value.
●main is a method; this is a starting point of a Java program. You will notice that the main
method code has been moved to some spaces left. It is called indentation which used to
make a program easier to read and understand.
1/16/2020 Thobius Joseph(Msc in Eng.), 0783758724
Program Structure
String[] args
It is an array where each element of it is a string, which has been named as "args". If your
Java program is run through the console, you can pass input parameter, and main() method
takes it as input.
System.out.println();
This statement is used to print text on the screen as output, where system is a predefined
class, and out is an object of the PrintWriter class defined in the system. The method println
prints the text on the screen with a new line. You can also use print() method instead of
println() method. All Java statement ends with a semicolon.
NB:
●You have to keep in mind that, Java code is case sensitive.
●To write Java program, you must have to define class first.
●The name of the class in Java (which holds the main method) is the name of the Java
program, and the same name will be given in the filename. As mentioned above in the
sample program; The name of the class is "Hello" in which the main method is, then this file
will be named "Hello.Java".