Lecture-03-JAVA
Lecture-03-JAVA
•Compile and run the Java program using CMD or any IDE (intelli j, Eclipse e.t.c)
Basic Java Program
•Multi-line Comment
/* This is
a multi-line comment */
•Documentation Comment
It is also known as a doc comment
/** This is a doc comment */
2. Source File Name
Source File Name Rule in
Java
1. When There is a public class in the file 2. When There is no public class in the file
The name of a source file must exactly match The source file name can be anything, but it must
the name of the public class name with the have the .java extension.
extension. java
class Program {
Example: Assume you have a public class public static void main(String[] args)
named GFG.
{
import java.io.*; System.out.println("Hello, World!");
public class GFG { }
public static void main (String[] args) { }
System.out.println("Hello World!");
Here, the class is not declared as public so you can
} make the file name anything
} like Example.java, Program.java etc
Note:
•GFG. java is a Valid Syntax
Java is a case-sensitive language, which means that the identifiers AB, Ab, aB , and ab are different in Java.
System.out.println(“Hello World”); // valid syntax
system.out.println(“Hello World”); // invalid syntax because of the first letter of System keyword is always uppercase.
4. Class Names
•The first letter of the class should be in Uppercase (lowercase is allowed but discouraged).
•If several words are used to form the name of the class, each inner word’s first letter should
be in Uppercase. Underscores are allowed, but not recommended. Also allowed are numbers and
currency symbols, although the latter are also discouraged because they are used for a special
purpose (for inner and anonymous classes).
•If several words are used to form the name of the method, then each first letter of the inner word should be in Uppercase.
Underscores are allowed, but not recommended. Also allowed are digits and currency symbols.
7. Identifiers in Java
•Identifiers are the names of local variables, instance and class variables, and labels, but also the names for classes, packages,
modules and methods. All Unicode characters are valid, not just the ASCII subset.
•All identifiers can begin with a letter, a currency symbol or an underscore ( _ ). According to the convention, a letter should be
lowercase for variables.
•The first character of identifiers can be followed by any combination of letters, digits, currency symbols and the underscore.
The underscore is not recommended for the names of variables. Constants (static final attributes and enums) should be in all
Uppercase letters.
A keyword cannot be used as an identifier since it is a reserved word and has some special meaning.
Legal identifiers: MinNumber, total, ak74, hello_world, $amount, _under_value
Illegal identifiers: 74ak, -amount
1.Classes and Interfaces: PascalCase (Upper
CamelCase): Each word starts with an uppercase letter. This
style is used for naming classes and interfaces.
Examples:ActionEvent,ActionListener,EmployeeDetails,
AccountManager.
Note that a blank space should not be used between a method name and its opening parenthesis. This helps to
distinguish keywords from method calls.
•A blank space should appear after commas in argument lists.
•All binary operators except . should be separated from their operands by spaces. Blank spaces should never
separate unary operators such as unary minus, increment ("++"), and decrement ("--") from their operands.
Example:
a += c + d;
a = (a + b) / (c * d);
9. Constants
The names of variables declared class constants and of ANSI constants should be all uppercase
with words separated by underscores ("_"). (ANSI constants should be avoided, for ease of
debugging.)
EXAMPLE
static final int MIN_WIDTH = 4;
static final int MAX_WIDTH = 999;
static final int GET_THE_CPU = 1;
Identifiers Type Naming Rules Examples
It should start with the uppercase letter. public class Employee
{
Class It should be a noun such as Color, Button, System, Thread, etc. //code snippet
Use appropriate words, instead of acronyms. }
It should start with a lowercase letter such as id, name. class Employee
{
It should not start with the special characters like & (ampersand), $ (dollar), _ // variable
Variable (underscore). int id;
If the name contains multiple words, start it with the lowercase letter followed by //code snippet
an uppercase letter such as firstName, lastName. }
Avoid using one-character variables such as x, y, z.
It should be a lowercase letter such as java, lang. //package
package com.javatpoint;
If the name contains multiple words, it should be separated by dots (.) such as class Employee
Package java.util, java.lang. {
//code snippet
}
•public keyword is an access modifier that represents visibility. It means it is visible to all.
•static is a keyword. If we declare any method as static, it is known as the static method. The
core advantage of the static method is that there is no need to create an object to invoke the
static method. The main() method is executed by the JVM, so it does not require creating an
object to invoke the main() method. So, it saves memory.
•void is the return type of the method. It means it does not return any value.
•System.out.println() is used to print statement on the console. Here, System is a class, out
is an object of the PrintStream class, println() is a method of the PrintStream class. We will
discuss the internal working of System.out.println() statement in the coming section.
Class Object