0% found this document useful (0 votes)
32 views47 pages

Week2 Lecture 83665

The document discusses various topics related to programming in Java including separators, comments, identifiers, keywords, Java class library, syntax, indentation, variables, and operators. It provides examples and explanations of parentheses, braces, brackets, semicolons, commas, and periods as separators used in Java. It also covers single-line comments, multi-line comments, and Javadoc comments.

Uploaded by

Krishna Raibhat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views47 pages

Week2 Lecture 83665

The document discusses various topics related to programming in Java including separators, comments, identifiers, keywords, Java class library, syntax, indentation, variables, and operators. It provides examples and explanations of parentheses, braces, brackets, semicolons, commas, and periods as separators used in Java. It also covers single-line comments, multi-line comments, and Javadoc comments.

Uploaded by

Krishna Raibhat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

PROGRAMMING

Lecture 2

Sushil Paudel

erspaudel 1
HELLO WORLD

erspaudel 2
PREVIOUS TOPICS

• Course Introduction

• Language
• Programming language

• Introduction to Java

• Text editors - Notepad, BlueJ

• Java fields

• Java Prerequisites

erspaudel 3
TODAY’S TOPICS

• Separators

• Comments

• Identifiers

• Keywords

• Java Class Library

• Syntax

• Indentation

• Variable

• Operators
erspaudel 4
TARGET

erspaudel 5
SEPARATORS

• Separators are symbols that indicate the division and arrangement of groups

of code.

• The structure and function of code is generally defined by the separators.

• The separators used in Java are-

erspaudel 6
PARENTHESES ( )

• It is used to enclose parameters in method definitions, and enclosing cast types.

public class MyFirstJavaProgram {

public static void main(String[] args){

System.out.println("Hello world!");

erspaudel 7
BRACES { }

• It is used to define a block of code and to hold the values of arrays.

public class MyFirstJavaProgram {

public static void main(String[] args){

System.out.println("Hello world!");

int[] numbers = {1, 2, 3, 4, 5};

erspaudel 8
BRACKETS []

• It is used to declare array types

public class MyFirstJavaProgram {

public static void main(String[] args){

System.out.println("Hello world!");

erspaudel 9
SEMICOLON

• It is used to separate statements.

public class MyFirstJavaProgram {

public static void main(String[] args){

System.out.println("Hello world!");

System.out.println(”I am learning Java!");

erspaudel 10
COMMA ,

• It is used to separate identifiers in a variable declaration or values in an

array.

public class MyFirstJavaProgram {

public static void main(String[] args){

int a =10, b= 20;

int[] numbers = {1, 2, 3, 4, 5};

erspaudel 11
PERIOD .

It is used to separate package names from classes and subclasses and to

separate a variable or a method from a reference variable.

public class MyFirstJavaProgram {

public static void main(String[] args){

System.out.println("Hello world!");

erspaudel 12
DON’T BE LIKE THIS!

erspaudel 13
COMMENTS

• Comments are used for documenting the program.

• What is obvious today may not be obvious after sometime.


• What if someone else wants to read your codes?

• The compiler simply ignores the comments.

• Comments don't have any effect in the execution of the program.

erspaudel 14
SINGLE LINE COMMENT
• It reach till the end of the line only.

• Symbolized by //
• E.g.

// Defining the class MyFirstJavaProgram

public class MyFirstJavaProgram {

public static void main(String[] args){

System.out.println("Hello world!"); // Printing Hello World

erspaudel 15
MULTI LINE COMMENTS
• It is used to write multiple lines of

• Symbolized by /* <comments> */ . E.g

public class Hello {

public static void main (String args[]){

System.out.println("Hello World");

/*This is the

multi line comments,

I can write any lines of comments here*/

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 {

public static void main (String args[]){

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

• Graphical User Interfaces

• Image Processing & Multimedia

• Files, etc.
erspaudel 20
SYNTAX
• Syntax is a grammatical rule to write programs in any language.

• Java throws compilation error if syntax is wrong.

• A program written using Java and Php have different syntax, but their meaning/output(
Semantics) can be same.
• E.g.

System.out.print(“Hello World); // Java


print(“Hello World”); // Python
Console.Write(“Hello World”); // C#

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

MyFirstJavaProgram {public static void main

(String[] args)

{System.out.println("Hello world!"); }

Above code is a nightmare

erspaudel 24
INDENTATION

public class MyFirstJavaProgram {

public static void main(String[] args){

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.

• Syntax for variable declaration

• 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

Camel case in Java Programming : It consists of compound words or phrases such


that each word or abbreviation begins with a capital letter or first word with a
lowercase letter, rest all with capital.

personalaccount

salaryinnepali

BankAccountDetail
myFirstName

erspaudel 28
RULES FOR IDENTIFIERS

• Identifiers cannot start with digits. E.g.

String 1name; is wrong


• Identifiers should not contain spaces. E.g.

int first number is wrong

• Except $ and _ , other symbols are not allowed while declaring identifier. E.g.

String #hello is wrong

• 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

• It should start with the uppercase letter.

• It should be a noun such as Color, Button, ButtonWhite, MyJava, MyJavaProgram,


System, Thread, etc.

• Use appropriate words, instead of acronyms.

public class MyFirstJavaProgram {

public static void main(String[] args){

System.out.println("Hello world!");

erspaudel 30
METHODS
• It should start with lowercase letter.

• It should be a verb such as print(), println(), calculate(), etc


• If the name contains multiple words, start it with a lowercase letter followed by an
uppercase letter such as actionPerformed()

public class MyFirstJavaProgram {

public static void main(String[] args){

System.out.println("Hello world!");

erspaudel 31
VARIABLE

• It should start with a lowercase letter such as id, name.

• If the name contains multiple words, start it with the lowercase letter followed by an
uppercase letter such as firstName, lastName.

public class MyFirstJavaProgram {

public static void main(String[] args){

int firstNumber = 10;

String permanentAddress = “Pokhara”;

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.

• If the name contains multiple words, it should be separated by an underscore(_) such


as MAX_PRIORITY.
erspaudel 33
DON’T BE LIKE THIS

erspaudel 34
KEYWORDS
• In Java certain words are reserved that give special functionality/meaning.

• These are predefined words by Java so it cannot be used as a variable or object


name.

public class MyFirstJavaProgram {

public static void main(String[] args){

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 −

Operator Description Example

Called Logical AND operator. Returns true if both


&& (logical and) statements are true (A && B) is false

Called Logical OR Operator. Returns true if one of the


|| (logical or) statements is true (A || B) is true

Reverse the result, returns false if the result is true.


!(A && B) is true
! (logical not)
!A= false

erspaudel 40
LOGICAL OPERATOR

erspaudel 41
ASSIGNMENT OPERATOR

erspaudel 42
BITWISE OPERATOR

Operator Description Example


0&0=0
0&1=0
& (bitwise and) if both bits are 1, it gives 1, else it gives 0
1&0=0
1&1=1
0|0=0
0|1=1
| (bitwise or) If either of the bits is 1, it gives 1, else it gives 0.
1|0=1
1|1=1
0^0=0
This means that if bits of both the bits are 1 or 0 0^1=1
^ (bitwise XOR)
the result will be 0; otherwise, the result will be 1: 1^0=1
1^1=0

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

What will be the output?


erspaudel 45
GOOGLE

erspaudel 46
END OF LECTURE 2

Any Questions?

erspaudel 47

You might also like