CS4001NT Week 2: Java Syntax, Semantics, and Structure (PDF)

Document Details

OpulentDevotion6404

Uploaded by OpulentDevotion6404

Itahari International College

2024

Jeevan Poudel

Tags

java programming computer science programming language programming concepts

Summary

This document is a lecture on the Syntax, Semantics, and Structure of Java Programming, focusing on week 2. It covers identifiers, reserved words, variables, and operators, with illustrative examples.

Full Transcript

SYNTAX, SEMANTICS, STRUCTURE OF JAVA PROGRAM WEEK 2: LECTURE Jeevan Poudel November 18, 2024 Itahari International College CONTENTS 1. Identifiers Rules for Identifiers 2. Reserved Words 3. Variables Types of Variables 4. Operator Types of Operators CS4001NT Syntax,...

SYNTAX, SEMANTICS, STRUCTURE OF JAVA PROGRAM WEEK 2: LECTURE Jeevan Poudel November 18, 2024 Itahari International College CONTENTS 1. Identifiers Rules for Identifiers 2. Reserved Words 3. Variables Types of Variables 4. Operator Types of Operators CS4001NT Syntax, Semantics, Structure of Java Program November 18, 2024 1 / 21 IDENTIFIERS Identifiers Identifiers are combination of words, numbers and symbols used for naming classes, methods, and variables, etc Identifiers consits of: letters digits and two special characters below: 1. _ (underscore) 2. $ CS4001NT Syntax, Semantics, Structure of Java Program November 18, 2024 2 / 21 Identifiers Example 1 int productPrice = 50; 2 3 double distanceFromItahari2Jhapa = 75.00; 4 5 final String DEFAULT_PASSWORD = " admin "; 6 7 String $city = "Itahari , Sunsari "; CS4001NT Syntax, Semantics, Structure of Java Program November 18, 2024 3 / 21 IDENTIFIERS RULES FOR IDENTIFIERS Identifiers Rules for Identifiers RULES FOR IDENTIFIERS Identifiers cannot start with digits. Exmaple: 1userName; is an invalid identifier Identifiers should not contains spaces. Exmaple: String user Name; is invalid. It should be like String userName; Only allowed special symbols in identifier includes: _ (underscore) and $. Example: #userName, @lengthOfRectangle is invalid but USER_NAME is valid Reserved keywords are not allowed for naming identifiers. Example: public, class, main, etc. are reserved keywords. Remember Java is case sensitive Hence, userName, UserName and USERNAME are three different terms. CS4001NT Syntax, Semantics, Structure of Java Program November 18, 2024 4 / 21 RESERVED WORDS Reserved Words RESERVED WORDS There are reserved words in java. These words have fixed meaning and cannot be redefined by the programmer. Reserved words are reserved which means they cannot be used as variable names, class names, method names, or name for other identifiers. Java includes more than 50 keywords. Number of keyword varies between JDK versions. CS4001NT Syntax, Semantics, Structure of Java Program November 18, 2024 5 / 21 VARIABLES Variables VARIABLES Variable is container that holds value temporarily which value can be changed while writing a program Example String collegeName = ”Itahari International College”; // text value OR int studentEnrolledInClass = 1500; // numeric value CS4001NT Syntax, Semantics, Structure of Java Program November 18, 2024 6 / 21 VARIABLES TYPES OF VARIABLES Variables Types of Variables TYPES OF VARIABLES 1. Instance variables 2. Static variables 3. Local variables Instance Variable (Non-Static Fields) A variable declared inside the class but outside the body of any method, constructor or block is called an instance variable. Static Variable Class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. Local Variables A variable declared inside the body of the method is called a local variable. Also, local variables are declared inside constructors and blocks. CS4001NT Syntax, Semantics, Structure of Java Program November 18, 2024 7 / 21 Variables Types of Variables EXAMPLE OF VARIABLES 1 public class Employee { 2 public String name; // instance variable 3 private double salary ; // instance variable 4 public static int count = 0; // static variable 5 6 public void printInfo () { 7 int serialNumber ; // local variable 8 System.out. println ("SN: " + serialNumber ); 9 } 10 11 public static void main( String [] args) { 12 int age = 50; // local variable 13 } 14 } Listing 1: Example showing declaration of different types of variables in Java CS4001NT Syntax, Semantics, Structure of Java Program November 18, 2024 8 / 21 OPERATOR Operator OPERATORS An operator is a symbol which performs an operation with an operand Operation could range from addition of numbers to subtraction, multiplication, comparison, and so on Operands are the variables or constants that are used to store the value CS4001NT Syntax, Semantics, Structure of Java Program November 18, 2024 9 / 21 Operator EXAMPLE: OPERANDS AND OPERATORS 1 class Test { 2 public static void main ( String [] args) { 3 int priceOfProduct = 20; 4 int quantity = 5; 5 6 int totalPrice = priceOfProduct * quantity ; 7 8 System.out. println ("Total price : " + totalPrice ); 9 } 10 } In the example above: { { Operands −→ priceOfProduct, quantity, totalPrice Operators −→ =, ∗, + CS4001NT Syntax, Semantics, Structure of Java Program November 18, 2024 10 / 21 OPERATOR TYPES OF OPERATORS Operator Types of Operators 1. Assignment operator 3. Unary operator 5. Logical operator 2. Arithmetic operator 4. Relational operator 6. Bitwise operator CS4001NT Syntax, Semantics, Structure of Java Program November 18, 2024 11 / 21 Operator Types of Operators ASSIGNMENT OPERATOR Operator symbol is: = It assigns the value on its right to the operand on its left This operator can also be used on objects to assign object references CS4001NT Syntax, Semantics, Structure of Java Program November 18, 2024 12 / 21 Operator Types of Operators 1 a = 10; 2 b = 5; 3 c = 15; 4 5 a = b; 6 7 b = c; What is the value of a, b, c CS4001NT Syntax, Semantics, Structure of Java Program November 18, 2024 13 / 21 Operator Types of Operators ARITHMETIC OPERATOR Operators that perform addition, subtraction, multiplication, and division Example: +, -, *, %, and / Operator Description + Additive operator (also used for String concatenation) - Subtraction operator * Multiplication operator / Division operator % Modulus/Remainder operator CS4001NT Syntax, Semantics, Structure of Java Program November 18, 2024 14 / 21 Operator Types of Operators USING ARITHMETIC OPERATORS 1 System.out. println (50 + 60); 2 System.out. println (60 - 50); 3 System.out. println (50 * 60); 4 System.out. println (60 / 25); 5 System.out. println (60 / 5.0); 6 System.out. println (60 % 9); CS4001NT Syntax, Semantics, Structure of Java Program November 18, 2024 15 / 21 Operator Types of Operators UNARY OPERATOR The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean. Operator Description + Unary plus operator; indicates positive value - Unary minus operator; negates an expression ++ Increment operator; increments a value by 1 -- Decrement operator; decrements a value by 1 ~ Logical complement operator; inverts the value of a boolean CS4001NT Syntax, Semantics, Structure of Java Program November 18, 2024 16 / 21 Operator Types of Operators EXAMPLE: UNARY OPERATOR 1 int counter = 10; 2 System.out. print ( counter ++); 3 4 int counter2 = 10; 5 System.out. print (++ counter2 ); CS4001NT Syntax, Semantics, Structure of Java Program November 18, 2024 17 / 21 Operator Types of Operators RELATIONAL OPERATOR Determine if one operand is greater than, less than, equal to, or not equal to another operand. Operator Description == Equal to != Not equal to > Greater than < Less than >= Greater than or equal to = 50 ); CS4001NT Syntax, Semantics, Structure of Java Program November 18, 2024 19 / 21 Operator Types of Operators LOGICAL OPERATOR The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit ”short-circuiting” behavior, which means that the second operand is evaluated only if needed. Operator Description && Conditional AND || Conditional OR CS4001NT Syntax, Semantics, Structure of Java Program November 18, 2024 20 / 21 Operator Types of Operators BITWISE AND BIT SHIFT OPERATOR The unary bitwise complement operator ̃ inverts a bit pattern; it can be applied to any of the integral types, making every ”0” a ”1” and every ”1” a ”0”. For example, a byte contains 8 bits; applying this operator to a value whose bit pattern is 00000000 would change its pattern to 11111111 CS4001NT Syntax, Semantics, Structure of Java Program November 18, 2024 21 / 21

Use Quizgecko on...
Browser
Browser