0% found this document useful (0 votes)
6 views

Lecture-03-JAVA

The document outlines the basic requirements for running a Java program, including installing the JDK, setting the path, and compiling the program. It also covers Java syntax, naming conventions for classes, methods, and variables, as well as the importance of comments and whitespace in code. Additionally, it explains the concepts of classes and objects in Java, providing examples of code structure and usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture-03-JAVA

The document outlines the basic requirements for running a Java program, including installing the JDK, setting the path, and compiling the program. It also covers Java syntax, naming conventions for classes, methods, and variables, as well as the importance of comments and whitespace in code. Additionally, it explains the concepts of classes and objects in Java, providing examples of code structure and usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

LECTURE-03

Basic Requirement to Run a Java Program

•Install the JDK.

•Set path of the jdk/bin directory

•Create the Java program.

•Compile and run the Java program using CMD or any IDE (intelli j, Eclipse e.t.c)
Basic Java Program

public class MyFirstJavaProgram


{
/* This is my first java program. This will print 'Hello World' as the
output */
public static void main(String []args)
{
System.out.println("Hello World");
// prints Hello World
}
}
Java Syntax Key
Elements
1. Comments in Java
There are three types of comments in Java.

•Single line Comment


// This is a single-line comment

•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

•gfg.java is a Invalid Syntax


3. Case Sensitivity

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

class MyJavaProgram // valid syntax


class 1Program // invalid syntax
class My1Program // valid syntax
class $Program // valid syntax, but discouraged
class My$Program // valid syntax, but discouraged (inner class Program inside the class My)
class myJavaProgram // valid syntax, but discouraged
6. Method Names
•All the method names should start with a lowercase letter (uppercase is also allowed but lowercase is recommended).

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

public void employeeRecords() // valid syntax


public void EmployeeRecords() // valid syntax, but discouraged

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.

•Most importantly identifiers are case-sensitive.

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.

2. Methods and Variables: CamelCase (Lower


CamelCase): The first word starts with a lowercase letter, and
each subsequent word starts with an uppercase letter. This
style is used for naming methods and variables.
8 - White Space

8.1 Blank Lines


Blank lines improve readability by setting off sections of code that are
logically related.

Two blank lines should always be used in the following


circumstances:
•Between sections of a source file
•Between class and interface definitions

One blank line should always be used in the following


circumstances:
•Between methods
•Between the local variables in a method and its first statement
•Before a block or single-line comment
•Between logical sections inside a method to improve readability
8.2 Blank Spaces

Blank spaces should be used in the following circumstances:

•A keyword followed by a parenthesis should be separated by a space. Example:


while (true) { ... }

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

while (d++ = s++) {


n++;
}
printSize("size is " + foo + "\n");
•The expressions in a for statement should be separated by blank spaces.
Example:
for (expr1; expr2; expr3)
•Casts should be followed by a blank space. Examples:
myMethod((byte) aNum, (Object) x);
myMethod((int) (cp + 5), ((int) (i + 3))
+ 1);

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 the uppercase letter. interface Printable


{
Interface It should be an adjective such as Runnable, Remote, ActionListener. //code snippet
Use appropriate words, instead of acronyms. }

It should start with lowercase letter. class Employee


{
It should be a verb such as main(), print(), println(). // method
Method If the name contains multiple words, start it with a lowercase letter followed by an void draw()
uppercase letter such as actionPerformed(). {
//code snippet
}
}

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
}

It should be in uppercase letters such as RED, YELLOW. class Employee


{
If the name contains multiple words, it should be separated by an underscore(_) //constant
Constant such as MAX_PRIORITY. static final int MIN_AGE =
It may contain digits but not as the first letter. 18;
//code snippet
Parameters used in First Java Program
Let's see what is the meaning of class, public, static, void, main, String[],
System.out.println().
•class keyword is used to declare a class in Java.

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

•The main() method represents the starting point of the program.

•String[] args or String args[] is used for command line argument.

•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

 Class is the blueprint/layout of an object. It is  An object is an instance of the class having


used to create objects. specific property, identity & behavior.

 Memory is allocated as soon as an object is


 No memory is allocated when a class is declared.
created.

 An object is a real-world entity such as a book,


 A class is a group of similar objects.
car, etc.

 Class is a logical entity.  An object is a physical entity.

 Objects can be created many times as per


 A class can only be declared once.
requirement.

 Objects/instances of the class car can be BMW,


 An example of class can be a car.
Mercedes, Ferrari, etc.
public class Dog
{
String name;
String breed;
int age;
String color;

public Dog(String name, String breed, int age, String color)


{
name = name;
breed = breed;
age = age;
color = color;
}
Public DogMaker()
{
//use name , breed, color to create desired instance of Dog.
System.out.println(“Your papillion is Ready”);
}
public static void main(String[] args)
{
Dog tuffy = new Dog("tuffy", "papillon", 5, "white");
System.out.println(tuffy.toString());
}
}

You might also like