2-Elements in Java
2-Elements in Java
2-Elements in Java
Program
CSE1018
1
2
Rule to follow while naming identifiers such as class,
package, variable, constant, method, etc.
n
confusion or erroneous code.
Class:
• Start with the uppercase letter.
• It should be a noun such as Color, Button,
n {
//code snippet
}
Method : 4
class Employee
Naming
{
// method
n {
//code snippet
}
Naming Convention: Variable :
Constant :
• It should be in uppercase letters such as RED, YELLOW.
• If the name contains multiple words, it should be separated by an
underscore(_) such as MAX_PRIORITY.
• It may contain digits but not as the first letter.
class Employee
Naming
{
Convention
//constant
//code snippet
}
7
Structure of Java
Program
• Java is an object-oriented programming,
platform-independent, and secure
programming language that makes it
popular.
• Using the Java programming language,
we can develop a wide variety of
applications.
• So, before diving in depth, it is necessary
to understand the basic structure of Java
program in detail.
Documentation Section
• The documentation section is an important section but optional for a Java
program. It includes basic information about a Java program. Whatever we
write in the documentation section, the Java compiler ignores the
statements during the execution of the program. To write the statements
in the documentation section, we use comments. The comments may be
single-line, multi-line, and documentation comments.
• Single-line Comment: It starts with a pair of forwarding slash (//). For
example:
• Multi-line Comment: It starts with a /* and ends with */. We write
between these two symbols. For example:
8
Package Declaration
The package declaration is optional. It is placed just after the documentation section.
In this section, we declare the package name in which the class is placed.
Note that there can be only one package statement in a Java program.
package packaename;
9
Class Definition
Variables
definition.
• The variables and constants store values
of the parameters. It is used during the
&
execution of the program.
• We can also decide and define the scope
of variables by using the modifiers. It
s {
String sname; //variable
int id;
double percentage; 11
12 • In this section, we define the main()
method.
• It is essential for all Java programs.
• Because the execution of all Java
programs starts from the main()
method.
• In other words, it is an entry point of
the class.
• It must be inside the class. Inside the
main method, we create objects and
call the methods. We use the following
statement to define the main()
Main Method
method:
public static void main(String args[])
Class
{
}
File Name: first.java First Java Program
class first
{
public static void main(String args[])
{
System.out.println (“Welcome to JAVA”);
}
}
generation
BCSE103E Computer Programming: JAVA 15
After Running
- Output
BCSE103E Computer Programming: JAVA 16
First Java Program
File Name: first.java
Paul J. Deitel, Harvey Deitel, Java SE8 for programmers (Deitel Developer
Series) 3rd Edition, 2014
s Kathy Sierra, Bert Bates , Head First Java, 2nd Edition 2nd Edition , O'Reilly
Media; 2nd edition (February 19, 2005)
https://www.javatpoint.com/java-tutorial