SlideShare a Scribd company logo
JAVA-NLP Lecture series
Variables, Expressions and
Arithmetic Operators in JAVA
Lecture #3
manish@cse.iitb.ac.in
JAVA-NLP Lecture series
Variables
 A variables can be considered as a name given
to the location in memory where values are
stored.
 One syntax of variable declaration
dataType variableName;
 Do you imagine that the variable can change its
value – yes, that is why it is called as variable.
 Is the following variable name is legal:
GrandTotal ? Suggest good name for it?
JAVA-NLP Lecture series
Variable Names and Keywords
 Use only the characters 'a' through 'z', 'A' through 'Z', '0'
through '9', character '_', and character '$'. A name can’t
contain space character.
 Do not start with a digit. A name can be of any length.
 Upper and lower case count as different characters.
So SUM and Sum are different names.
 A name can not be a reserved word.
 A name must not already be in use in this part of the
program.
 A reserved word is a word which has a predefined
meaning in Java. For example int, double, true, and
import are reserved words.
JAVA-NLP Lecture series
Questions on Variable Names
 int good-bye;
 int shrift = 0;
 char thisMustBeTooLong;
 int bubble = 0,toil = 9, trouble = 8
 int 8ball;
 int double;
JAVA-NLP Lecture series
Answers
 int good-bye; //bad variable name
 int shrift = 0; //OK
 char thisMustBeTooLong; //OK in syntax
//but poor choice in variable name
 int bubble = 0,toil = 9, trouble = 8
// “;” missing at the end
 int 8ball; //can’t start with a digit
 int double; //double is a reserve word
JAVA-NLP Lecture series
Expressions
 An expression is a combination of constants
(like 100), operators ( like +), variables(section
of memory) and parentheses ( like “(” and “)” )
used to calculate a value.
 x = 1; y = 100 + x;
 This is the stuff that you know from algebra, like:
(32 - y) / (x + 5)
 There are rules for this, but best rule is that
an expression must look OK as algebra.
 Based on what you know about algebra, what is
the value of 12 - 4/2 + 2 ?
 Spaces don’t much matter.
JAVA-NLP Lecture series
Arithmetic Operators
 What is the value of –12 + 3
 An arithmetic operator is a symbol that
asks for doing some arithmetic.
Operator Meaning Precedence
- Unary minus highest
+ Unary Plus highest
* Multiplication middle
/ Division middle
% Modulus middle
+ addition low
- Subtraction low
JAVA-NLP Lecture series
basicOperation.java
class basicOperation {
//arithmetic using variables
public static void main() {
int a = 1+ 1;
int b = 3 * 3;
int c = 1 + 8/ 4;
int d = -2;
System.out.println( “a = ” + a);
System.out.println(“b = ” + b);
System.out.println(“c = ” + c);
System.out.println(“d = ” + d);
}
}
JAVA-NLP Lecture series
basicMath.java
class basicMath {
//arithmetic using variables
public static void main() {
int a = 1+ 3;
int b = a * 3;
int c = b / 4;
int d = c – a;
int e = -d;
System.out.println( “a = ” + a);
System.out.println(“b = ” + b);
System.out.println(“c = ” + c);
System.out.println(“d = ” + d);
System.out.println(“e = ” + e);
}
}
JAVA-NLP Lecture series
Parentheses
 Difference between -1 * ( 9 - 2 ) * 3 and -1 * 9 - 2 * 3
 To say exactly what numbers go with each operator, use
parentheses.
 Nested Parentheses: The expression is evaluated starting
at the most deeply nested set of parentheses (the
"innermost set"), and then working outward until there
are no parentheses left. If there are several sets of
parentheses at the same level, they are evaluated left to
right.
 ( ( ( 32 - 16 ) / ( 2 * 2 ) ) - ( 4 - 8 ) ) + 7
 Are arithmetic expressions the only kind of
expression?
JAVA-NLP Lecture series
intergerDivision.java
class integerDivision {
public static void main ( String[] args ) {
System.out.println("The result is: " + (1/2 +
1/2) );
}
}
 What is the value of 99/100 ?
 Change print statement to “print” (1.0/2 + 1/ 2 .0)
JAVA-NLP Lecture series
final reserve word
class calculateTax {
public static void main ( String[] arg ) {
l final char c = ‘A’; f
final int count = 0;
. . . . . .
}
}
JAVA-NLP Lecture series
Assignment No.1
 Write a program that averages the synsets created
for three months April, May and June. Declare and
initialize variable to the synset entered for each
month. Compute the average and write out the
results, something like this:
Synsets Entered for April: 12
Synsets Entered for May : 14
Synsets Entered for June: 8
Average Synset Entered: 11.333333
Check that your program prints the correct results.
JAVA-NLP Lecture series
Assignment No. 2
 Write a program that computes summation of 1 to n
numbers where n can be any positive number.
Declare and initialize variables and print the output
as
Value of n is : 5
Sum of first 5 positive numbers : 15
Check the correctness of the result.

More Related Content

Similar to java_lect_03-2.ppt (20)

PPTX
Introduction to Java
Ashita Agrawal
 
PDF
vb.net.pdf
VimalSangar1
 
PPTX
Lecture2_MCS4_Evening.pptx
SaqlainYaqub1
 
PDF
Variables in Python & Data Types and Their Values
Raza Ul Mustafa
 
PDF
Zoho Interview Questions By Scholarhat.pdf
Scholarhat
 
PPTX
JavaScript.pptx
Govardhan Bhavani
 
PPTX
JNTUK python programming python unit 3.pptx
Venkateswara Babu Ravipati
 
PDF
4.Lesson Plan - Java Operators.pdf...pdf
AbhishekSingh757567
 
PPT
CSL101_Ch1.ppt
kavitamittal18
 
PPT
CSL101_Ch1.ppt
DrPriyaChittibabu
 
PPTX
CSL101_Ch1.pptx
shivanka2
 
PPTX
CSL101_Ch1.pptx
Ashwani Kumar
 
PPT
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Sagar Verma
 
PPTX
JAVA CLASS PPT FOR ENGINEERING STUDENTS BBBBBBBBBBBBBBBBBBB
NagarathnaRajur2
 
PPT
Matlab_Introduction_by_Michael_Medvinsky.ppt
khaliq1
 
PPT
Matlab Introduction for beginners_i .ppt
DrSeemaBHegde1
 
PPT
MatlabIntroduction presentation xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
divyapriya balasubramani
 
PPTX
Guide to Java.pptx
PrathamVaishnav1
 
PPTX
Java fundamentals
Jayfee Ramos
 
PPTX
datatypes-and-operators in wed development.pptx
FahimMousa
 
Introduction to Java
Ashita Agrawal
 
vb.net.pdf
VimalSangar1
 
Lecture2_MCS4_Evening.pptx
SaqlainYaqub1
 
Variables in Python & Data Types and Their Values
Raza Ul Mustafa
 
Zoho Interview Questions By Scholarhat.pdf
Scholarhat
 
JavaScript.pptx
Govardhan Bhavani
 
JNTUK python programming python unit 3.pptx
Venkateswara Babu Ravipati
 
4.Lesson Plan - Java Operators.pdf...pdf
AbhishekSingh757567
 
CSL101_Ch1.ppt
kavitamittal18
 
CSL101_Ch1.ppt
DrPriyaChittibabu
 
CSL101_Ch1.pptx
shivanka2
 
CSL101_Ch1.pptx
Ashwani Kumar
 
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Sagar Verma
 
JAVA CLASS PPT FOR ENGINEERING STUDENTS BBBBBBBBBBBBBBBBBBB
NagarathnaRajur2
 
Matlab_Introduction_by_Michael_Medvinsky.ppt
khaliq1
 
Matlab Introduction for beginners_i .ppt
DrSeemaBHegde1
 
MatlabIntroduction presentation xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
divyapriya balasubramani
 
Guide to Java.pptx
PrathamVaishnav1
 
Java fundamentals
Jayfee Ramos
 
datatypes-and-operators in wed development.pptx
FahimMousa
 

More from kavitamittal18 (20)

PPT
pptTopic2IntroductionToJavaProgramming.ppt
kavitamittal18
 
PPT
11MappingDesigntoCode.ppt Software engineering
kavitamittal18
 
PPT
UseCase.ppt software engineering use3 cases
kavitamittal18
 
PPT
11MappingDesigntoCode.ppt ooad software software
kavitamittal18
 
PPT
Introduction.ppt wireless
kavitamittal18
 
PPT
CellularNetworks.ppt ppt
kavitamittal18
 
PPTX
Dr.C S Prasanth-Physics ppt.pptx computer
kavitamittal18
 
PPT
CSL101_Ch1.ppt Computer Science
kavitamittal18
 
PPT
maincse-150510153437-lva1-app68Computer Science92.ppt
kavitamittal18
 
PPT
Programming language basics.ppt Computer Science
kavitamittal18
 
PPT
02-chapter-1.ppt programming languages 10
kavitamittal18
 
PPT
CS553_ST7_Ch14-CellularWirelessNetworks.ppt
kavitamittal18
 
PPT
Lec7!JavaThreads.ppt java multithreading
kavitamittal18
 
PPT
JDBC.ppt database connectivity in java ppt
kavitamittal18
 
PPT
chapter7.ppt java programming lecture notes
kavitamittal18
 
PPT
09slide.ppt oops classes and objects concept
kavitamittal18
 
PPT
480 GPS Tech mobile computing presentation
kavitamittal18
 
PPT
gsm-archtecture.ppt mobile computing ppt
kavitamittal18
 
PPT
AdHocTutorial.ppt
kavitamittal18
 
PPTX
ELECTORAL POLITICS KAMAL PPT.pptx
kavitamittal18
 
pptTopic2IntroductionToJavaProgramming.ppt
kavitamittal18
 
11MappingDesigntoCode.ppt Software engineering
kavitamittal18
 
UseCase.ppt software engineering use3 cases
kavitamittal18
 
11MappingDesigntoCode.ppt ooad software software
kavitamittal18
 
Introduction.ppt wireless
kavitamittal18
 
CellularNetworks.ppt ppt
kavitamittal18
 
Dr.C S Prasanth-Physics ppt.pptx computer
kavitamittal18
 
CSL101_Ch1.ppt Computer Science
kavitamittal18
 
maincse-150510153437-lva1-app68Computer Science92.ppt
kavitamittal18
 
Programming language basics.ppt Computer Science
kavitamittal18
 
02-chapter-1.ppt programming languages 10
kavitamittal18
 
CS553_ST7_Ch14-CellularWirelessNetworks.ppt
kavitamittal18
 
Lec7!JavaThreads.ppt java multithreading
kavitamittal18
 
JDBC.ppt database connectivity in java ppt
kavitamittal18
 
chapter7.ppt java programming lecture notes
kavitamittal18
 
09slide.ppt oops classes and objects concept
kavitamittal18
 
480 GPS Tech mobile computing presentation
kavitamittal18
 
gsm-archtecture.ppt mobile computing ppt
kavitamittal18
 
AdHocTutorial.ppt
kavitamittal18
 
ELECTORAL POLITICS KAMAL PPT.pptx
kavitamittal18
 
Ad

Recently uploaded (20)

PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PPTX
Hashing Introduction , hash functions and techniques
sailajam21
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
PPTX
Day2 B2 Best.pptx
helenjenefa1
 
PPTX
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPTX
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
PPTX
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Hashing Introduction , hash functions and techniques
sailajam21
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
Day2 B2 Best.pptx
helenjenefa1
 
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
Design Thinking basics for Engineers.pdf
CMR University
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
Ad

java_lect_03-2.ppt

  • 1. JAVA-NLP Lecture series Variables, Expressions and Arithmetic Operators in JAVA Lecture #3 [email protected]
  • 2. JAVA-NLP Lecture series Variables  A variables can be considered as a name given to the location in memory where values are stored.  One syntax of variable declaration dataType variableName;  Do you imagine that the variable can change its value – yes, that is why it is called as variable.  Is the following variable name is legal: GrandTotal ? Suggest good name for it?
  • 3. JAVA-NLP Lecture series Variable Names and Keywords  Use only the characters 'a' through 'z', 'A' through 'Z', '0' through '9', character '_', and character '$'. A name can’t contain space character.  Do not start with a digit. A name can be of any length.  Upper and lower case count as different characters. So SUM and Sum are different names.  A name can not be a reserved word.  A name must not already be in use in this part of the program.  A reserved word is a word which has a predefined meaning in Java. For example int, double, true, and import are reserved words.
  • 4. JAVA-NLP Lecture series Questions on Variable Names  int good-bye;  int shrift = 0;  char thisMustBeTooLong;  int bubble = 0,toil = 9, trouble = 8  int 8ball;  int double;
  • 5. JAVA-NLP Lecture series Answers  int good-bye; //bad variable name  int shrift = 0; //OK  char thisMustBeTooLong; //OK in syntax //but poor choice in variable name  int bubble = 0,toil = 9, trouble = 8 // “;” missing at the end  int 8ball; //can’t start with a digit  int double; //double is a reserve word
  • 6. JAVA-NLP Lecture series Expressions  An expression is a combination of constants (like 100), operators ( like +), variables(section of memory) and parentheses ( like “(” and “)” ) used to calculate a value.  x = 1; y = 100 + x;  This is the stuff that you know from algebra, like: (32 - y) / (x + 5)  There are rules for this, but best rule is that an expression must look OK as algebra.  Based on what you know about algebra, what is the value of 12 - 4/2 + 2 ?  Spaces don’t much matter.
  • 7. JAVA-NLP Lecture series Arithmetic Operators  What is the value of –12 + 3  An arithmetic operator is a symbol that asks for doing some arithmetic. Operator Meaning Precedence - Unary minus highest + Unary Plus highest * Multiplication middle / Division middle % Modulus middle + addition low - Subtraction low
  • 8. JAVA-NLP Lecture series basicOperation.java class basicOperation { //arithmetic using variables public static void main() { int a = 1+ 1; int b = 3 * 3; int c = 1 + 8/ 4; int d = -2; System.out.println( “a = ” + a); System.out.println(“b = ” + b); System.out.println(“c = ” + c); System.out.println(“d = ” + d); } }
  • 9. JAVA-NLP Lecture series basicMath.java class basicMath { //arithmetic using variables public static void main() { int a = 1+ 3; int b = a * 3; int c = b / 4; int d = c – a; int e = -d; System.out.println( “a = ” + a); System.out.println(“b = ” + b); System.out.println(“c = ” + c); System.out.println(“d = ” + d); System.out.println(“e = ” + e); } }
  • 10. JAVA-NLP Lecture series Parentheses  Difference between -1 * ( 9 - 2 ) * 3 and -1 * 9 - 2 * 3  To say exactly what numbers go with each operator, use parentheses.  Nested Parentheses: The expression is evaluated starting at the most deeply nested set of parentheses (the "innermost set"), and then working outward until there are no parentheses left. If there are several sets of parentheses at the same level, they are evaluated left to right.  ( ( ( 32 - 16 ) / ( 2 * 2 ) ) - ( 4 - 8 ) ) + 7  Are arithmetic expressions the only kind of expression?
  • 11. JAVA-NLP Lecture series intergerDivision.java class integerDivision { public static void main ( String[] args ) { System.out.println("The result is: " + (1/2 + 1/2) ); } }  What is the value of 99/100 ?  Change print statement to “print” (1.0/2 + 1/ 2 .0)
  • 12. JAVA-NLP Lecture series final reserve word class calculateTax { public static void main ( String[] arg ) { l final char c = ‘A’; f final int count = 0; . . . . . . } }
  • 13. JAVA-NLP Lecture series Assignment No.1  Write a program that averages the synsets created for three months April, May and June. Declare and initialize variable to the synset entered for each month. Compute the average and write out the results, something like this: Synsets Entered for April: 12 Synsets Entered for May : 14 Synsets Entered for June: 8 Average Synset Entered: 11.333333 Check that your program prints the correct results.
  • 14. JAVA-NLP Lecture series Assignment No. 2  Write a program that computes summation of 1 to n numbers where n can be any positive number. Declare and initialize variables and print the output as Value of n is : 5 Sum of first 5 positive numbers : 15 Check the correctness of the result.