SlideShare a Scribd company logo
1 of 14
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

Lecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptxLecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptxSaqlainYaqub1
 
CSL101_Ch1.pptx
CSL101_Ch1.pptxCSL101_Ch1.pptx
CSL101_Ch1.pptxshivanka2
 
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Sagar Verma
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Martin Odersky
 
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)Live Exam Helper
 
This first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docxThis first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docxabhi353063
 
Chapter 02-logistic regression
Chapter 02-logistic regressionChapter 02-logistic regression
Chapter 02-logistic regressionRaman Kannan
 
a basic java programming and data type.ppt
a basic java programming and data type.ppta basic java programming and data type.ppt
a basic java programming and data type.pptGevitaChinnaiah
 
03 expressions.ppt
03 expressions.ppt03 expressions.ppt
03 expressions.pptBusiness man
 
Introduction to Erlang
Introduction to ErlangIntroduction to Erlang
Introduction to ErlangRaymond Tay
 

Similar to java_lect_03-2.ppt (20)

Lecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptxLecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptx
 
JavaScript.pptx
JavaScript.pptxJavaScript.pptx
JavaScript.pptx
 
CSL101_Ch1.ppt
CSL101_Ch1.pptCSL101_Ch1.ppt
CSL101_Ch1.ppt
 
CSL101_Ch1.pptx
CSL101_Ch1.pptxCSL101_Ch1.pptx
CSL101_Ch1.pptx
 
CSL101_Ch1.pptx
CSL101_Ch1.pptxCSL101_Ch1.pptx
CSL101_Ch1.pptx
 
CSL101_Ch1.ppt
CSL101_Ch1.pptCSL101_Ch1.ppt
CSL101_Ch1.ppt
 
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2
 
Guide to Java.pptx
Guide to Java.pptxGuide to Java.pptx
Guide to Java.pptx
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
 
MODULE_2_Operators.pptx
MODULE_2_Operators.pptxMODULE_2_Operators.pptx
MODULE_2_Operators.pptx
 
This first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docxThis first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docx
 
CAP615-Unit1.pptx
CAP615-Unit1.pptxCAP615-Unit1.pptx
CAP615-Unit1.pptx
 
Chapter 02-logistic regression
Chapter 02-logistic regressionChapter 02-logistic regression
Chapter 02-logistic regression
 
Java unit 2
Java unit 2Java unit 2
Java unit 2
 
[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types
 
a basic java programming and data type.ppt
a basic java programming and data type.ppta basic java programming and data type.ppt
a basic java programming and data type.ppt
 
03 expressions.ppt
03 expressions.ppt03 expressions.ppt
03 expressions.ppt
 
Introduction to Erlang
Introduction to ErlangIntroduction to Erlang
Introduction to Erlang
 

More from kavitamittal18

Lec7!JavaThreads.ppt java multithreading
Lec7!JavaThreads.ppt java multithreadingLec7!JavaThreads.ppt java multithreading
Lec7!JavaThreads.ppt java multithreadingkavitamittal18
 
JDBC.ppt database connectivity in java ppt
JDBC.ppt database connectivity in java pptJDBC.ppt database connectivity in java ppt
JDBC.ppt database connectivity in java pptkavitamittal18
 
chapter7.ppt java programming lecture notes
chapter7.ppt java programming lecture noteschapter7.ppt java programming lecture notes
chapter7.ppt java programming lecture noteskavitamittal18
 
09slide.ppt oops classes and objects concept
09slide.ppt oops classes and objects concept09slide.ppt oops classes and objects concept
09slide.ppt oops classes and objects conceptkavitamittal18
 
480 GPS Tech mobile computing presentation
480 GPS Tech mobile computing presentation480 GPS Tech mobile computing presentation
480 GPS Tech mobile computing presentationkavitamittal18
 
gsm-archtecture.ppt mobile computing ppt
gsm-archtecture.ppt mobile computing pptgsm-archtecture.ppt mobile computing ppt
gsm-archtecture.ppt mobile computing pptkavitamittal18
 
ELECTORAL POLITICS KAMAL PPT.pptx
ELECTORAL POLITICS KAMAL PPT.pptxELECTORAL POLITICS KAMAL PPT.pptx
ELECTORAL POLITICS KAMAL PPT.pptxkavitamittal18
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.pptkavitamittal18
 

More from kavitamittal18 (16)

Lec7!JavaThreads.ppt java multithreading
Lec7!JavaThreads.ppt java multithreadingLec7!JavaThreads.ppt java multithreading
Lec7!JavaThreads.ppt java multithreading
 
JDBC.ppt database connectivity in java ppt
JDBC.ppt database connectivity in java pptJDBC.ppt database connectivity in java ppt
JDBC.ppt database connectivity in java ppt
 
chapter7.ppt java programming lecture notes
chapter7.ppt java programming lecture noteschapter7.ppt java programming lecture notes
chapter7.ppt java programming lecture notes
 
09slide.ppt oops classes and objects concept
09slide.ppt oops classes and objects concept09slide.ppt oops classes and objects concept
09slide.ppt oops classes and objects concept
 
480 GPS Tech mobile computing presentation
480 GPS Tech mobile computing presentation480 GPS Tech mobile computing presentation
480 GPS Tech mobile computing presentation
 
gsm-archtecture.ppt mobile computing ppt
gsm-archtecture.ppt mobile computing pptgsm-archtecture.ppt mobile computing ppt
gsm-archtecture.ppt mobile computing ppt
 
AdHocTutorial.ppt
AdHocTutorial.pptAdHocTutorial.ppt
AdHocTutorial.ppt
 
ELECTORAL POLITICS KAMAL PPT.pptx
ELECTORAL POLITICS KAMAL PPT.pptxELECTORAL POLITICS KAMAL PPT.pptx
ELECTORAL POLITICS KAMAL PPT.pptx
 
Input and Output.pptx
Input and Output.pptxInput and Output.pptx
Input and Output.pptx
 
ch11.ppt
ch11.pptch11.ppt
ch11.ppt
 
11.ppt
11.ppt11.ppt
11.ppt
 
Java-operators.ppt
Java-operators.pptJava-operators.ppt
Java-operators.ppt
 
Ch06Part1.ppt
Ch06Part1.pptCh06Part1.ppt
Ch06Part1.ppt
 
IntroToOOP.ppt
IntroToOOP.pptIntroToOOP.ppt
IntroToOOP.ppt
 
09slide.ppt
09slide.ppt09slide.ppt
09slide.ppt
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.ppt
 

Recently uploaded

VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 

Recently uploaded (20)

VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 

java_lect_03-2.ppt

  • 1. JAVA-NLP Lecture series Variables, Expressions and Arithmetic Operators in JAVA Lecture #3 manish@cse.iitb.ac.in
  • 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.