Week2 Lecture 83665
Week2 Lecture 83665
Lecture 2
Sushil Paudel
erspaudel 1
HELLO WORLD
erspaudel 2
PREVIOUS TOPICS
• Course Introduction
• Language
• Programming language
• Introduction to Java
• Java fields
• Java Prerequisites
erspaudel 3
TODAY’S TOPICS
• Separators
• Comments
• Identifiers
• Keywords
• Syntax
• Indentation
• Variable
• Operators
erspaudel 4
TARGET
erspaudel 5
SEPARATORS
• Separators are symbols that indicate the division and arrangement of groups
of code.
erspaudel 6
PARENTHESES ( )
System.out.println("Hello world!");
erspaudel 7
BRACES { }
System.out.println("Hello world!");
erspaudel 8
BRACKETS []
System.out.println("Hello world!");
erspaudel 9
SEMICOLON
System.out.println("Hello world!");
erspaudel 10
COMMA ,
array.
erspaudel 11
PERIOD .
System.out.println("Hello world!");
erspaudel 12
DON’T BE LIKE THIS!
erspaudel 13
COMMENTS
erspaudel 14
SINGLE LINE COMMENT
• It reach till the end of the line only.
• Symbolized by //
• E.g.
erspaudel 15
MULTI LINE COMMENTS
• It is used to write multiple lines of
System.out.println("Hello World");
/*This is the
erspaudel 16
DOCUMENTATION COMMENTS (JAVADOC)
• Javadoc/Documentation comments structure look very similar to a regular multi-line
comment, but the key difference is the extra asterisk at the beginning.
• It is set inside the comment delimiters /** ... */ with one comment per class, interface,
or member.
• The comment should appear right before the declaration of the class, interface or
member.
• They can be processed by the javadoc tool to generate the API documentation for
your classes.
erspaudel 17
DOCUMENTATION COMMENTS (JAVADOC)
/**
* This class just prints the Hello World in the main method
*
* @author Sushil Paudel
* @version 1.0.1
*/
public class Hello {
System.out.println("Hello World");
erspaudel 18
COMMENTS
erspaudel 19
JAVA CLASS LIBRARY
• Java class library is a collection of thousands of classes included in the JDK and ready
to be used.
• Once you have installed Java, you get them as part of the installation and can start
building your application code up using the classes of Java Class Library.
• Data Structures
• Networking
• Files, etc.
erspaudel 20
SYNTAX
• Syntax is a grammatical rule to write programs in any language.
• A program written using Java and Php have different syntax, but their meaning/output(
Semantics) can be same.
• E.g.
erspaudel 21
INCORRECT SYNTAX EXAMPLE
erspaudel 22
INDENTATION
• Giving proper tab/space to similar/related elements to group them together.
• The compiler cares nothing about indentation, but if you indent well, you make your
code so much easier to read, which means you'll make it easier to debug.
• It is to display how the braces match up and show the logic of the program in an
organized fashion
erspaudel 23
INDENTATION
public class
(String[] args)
{System.out.println("Hello world!"); }
erspaudel 24
INDENTATION
System.out.println("Hello world!");
erspaudel 25
VARIABLES
• Variables are containers that hold values(as in algebra) temporarily in memory.
• The value held by a variable can be changed but its data type remains the same.
• Datatype variableName;
• Eg.
int a;
String color = “red”;
Here, color is a variable that holds the value red.
erspaudel 26
IDENTIFIERS
• Identifiers are combination of words, numbers and symbols used for naming classes,
methods and variables.
• Identifiers consists of letters, digits and two special symbols, underscore(_) and
dollar($)
String firstName;
• E.g. Double salary;
String bank_branch;
erspaudel 27
NAMING CONVENTION
personalaccount
salaryinnepali
BankAccountDetail
myFirstName
erspaudel 28
RULES FOR IDENTIFIERS
• Except $ and _ , other symbols are not allowed while declaring identifier. E.g.
• Reserved keywords are not allowed for declaring identifiers. E.g. public, new, static, int
• Note : Java is case sensitive so, “Name”, “NAME” and “name” are three different
terms.
erspaudel 29
CLASSES AND INTERFACES
System.out.println("Hello world!");
erspaudel 30
METHODS
• It should start with lowercase letter.
System.out.println("Hello world!");
erspaudel 31
VARIABLE
• If the name contains multiple words, start it with the lowercase letter followed by an
uppercase letter such as firstName, lastName.
erspaudel 32
NAMING CONVENTION
Package
• It should be a lowercase letter such as java, lang.
• If the name contains multiple words, it should be separated by dots (.) such as
java.util, java.lang.
Constant
• It should be in uppercase letters such as RED, YELLOW.
erspaudel 34
KEYWORDS
• In Java certain words are reserved that give special functionality/meaning.
System.out.println("Hello world!");
erspaudel 35
JAVA KEYWORDS LIST
erspaudel 36
OPERATORS
• An operator is a sign or symbol which performs some operations between one
or multiple operands. For example,
int a = 5 + 3;
a++;
String message = “Hello” + “World”;
erspaudel 37
ARITHMETIC OPERATOR
erspaudel 38
RELATIONAL OPERATOR
erspaudel 39
LOGICAL OPERATOR
Assume Boolean variables A holds true and variable B holds false, then −
erspaudel 40
LOGICAL OPERATOR
erspaudel 41
ASSIGNMENT OPERATOR
erspaudel 42
BITWISE OPERATOR
~ (bitwise ~1 = 0
It changes binary digits 1 to 0 and 0 to 1.
compliment) ~0 = 1
erspaudel 43
BITWISE OPERATOR
erspaudel 44
TERNARY/CONDITIONAL OPERATOR
variable x = (expression) ? value if true : value if false
E.g
int a = 10;
Int b;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );
erspaudel 46
END OF LECTURE 2
Any Questions?
erspaudel 47