2-Elements in Java

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

Elements of a Java

Program
CSE1018

1
2
Rule to follow while naming identifiers such as class,
package, variable, constant, method, etc.

It is not forced to follow.

Suggested by Java communities such as Sun Microsystems


and Netscape.
Naming All the classes, interfaces, packages, methods and fields of
Java are given according to the naming convention.
Conventio If you fail to follow these conventions, it may generate

n
confusion or erroneous code.

By using standard Java naming conventions, code is easy to


read.

Readability indicates that less time is spent to figure out


what the code does.
3

Class:
• Start with the uppercase letter.
• It should be a noun such as Color, Button,

Naming System, Thread, etc.


• Use appropriate words, instead of acronyms.

Conventio public class Employee

n {

//code snippet

}
Method : 4

• It should start with lowercase letter.


• It should be a verb such as main(), print(), println().
• If the name contains multiple words, start it with a lowercase letter
followed by an uppercase letter such as actionPerformed().

class Employee

Naming
{

// method

Conventio void draw()

n {

//code snippet

}
Naming Convention: Variable :

 It should start with a lowercase letter such as id,


name.
 It should not start with the special characters
like & (ampersand), $ (dollar), _ (underscore).
 If the name contains multiple words, start it with
the lowercase letter followed by an uppercase
letter such as firstName, lastName.
 Avoid using one-character variables such as x, y, z.
class Employee
{
// variable int id;
//code snippet
} 5
6

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

static final int MIN_AGE = 18;

//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.

It must be defined before any class and interface declaration.


It is necessary because a Java class can be placed in different packages and directories
based on the module they are used.
For all these classes package belongs to a single parent directory.

We use the keyword package to declare the package name.

package packaename;
9
Class Definition

• In this section, we define the class. It is vital part


of a Java program. Without the class, we cannot
create any Java program.
• A Java program may conation more than one
class definition. We use the class keyword to
define the class.
• The class is a blueprint of a Java program.
• It contains information about user-defined
methods, variables, and constants.
• Every Java program has at least one class that
contains the main() method. For example:

class Student //class definition


{
}
10
• In this section, we define variables and
constants that are to be used later in the
program.
Class • In a Java program, the variables and
constants are defined just after the class

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

Constant defines the life of the variables. For


example:
class Student //class definition

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”);
}
}

BCSE103E Computer Programming: JAVA 13


14

Compiling Java Program


Compile and
Run – javac first.java
Command
Line Running Java Program
Execution
java first
BCSE103E Computer Programming: JAVA
After
Compilation – Bytecode

Class file version of


the
program

generation
BCSE103E Computer Programming: JAVA 15
After Running
- Output
BCSE103E Computer Programming: JAVA 16
First Java Program
File Name: first.java

class first class – Java Keyword


{ first – Identifier for ‘first’ class

} Class Definition with members along with


access specifier (default or public)

BCSE103E Computer Programming: JAVA 17


First Java Program
public class first
Access Specifier – Code from outside class can access this method
{
Program Entry Point – First method to
public static void main(String args[]) be executed in the Compilation Unit
{ Call this method, without creating
an object of ‘first’ class
Return value of this method
}

} Array of String Object – args - Read values from command line @


running
BCSE103E Computer Programming: JAVA 18
First Java Program
public class first
{
public static void main(String args[])
{ Display the String and values
System.out.println (“Hello VIT Students”);
}
Predefined Java Class to
}
access this method
Output stream to standard output device
BCSE103E Computer Programming: JAVA 19
Next Program
class basic
{
public static void main(String args[])
{ variable declaration type var-name

int num; assignment operator


num = 100; num to be appended

System.out.println("This is num: " + num); operator


num = num * 2;
System.out.println("The value of num * 2 is ");
System.out.println(num);
}
} BCSE103E Computer Programming: JAVA 20
To Display your Residential and University
Address

To convert money amount from Rupees into


US Dollars and Vice-versa (Rs. 6000/-) &
($456)
Write a Java
Program To perform subtraction, multiplication,
division and modulo of two integers (20, 10)

To convert minutes into hours and seconds


into minutes (220 minutes) & (45 seconds)

BCSE103E Computer Programming: JAVA 21


22

Herbert Schildt, The Complete Reference – java, Tata McGraw-Hill Education,


10th Edition, 2017

Paul J. Deitel, Harvey Deitel, Java SE8 for programmers (Deitel Developer
Series) 3rd Edition, 2014

Reference Y. Daniel Liang, Introduction to Java Programming-Comprehensive Version-


10th Edition, Pearson Ltd, 2015

s Kathy Sierra, Bert Bates , Head First Java, 2nd Edition 2nd Edition , O'Reilly
Media; 2nd edition (February 19, 2005)

Cay S. Horstmann, Core Java Volume I—Fundamentals 9th Edition, Prentice


Hall; 9 edition (December 7, 2012) Joshua Bloch, Effective Java-2nd Edition,
Addison-Wesley; (May 28, 2008).

https://www.javatpoint.com/java-tutorial

You might also like