SlideServe
Menu
Préférences
© SlideServe · v2026

Variables, Expressions and Arithmetic Operators in JAVA

mateja avatar
14 diapositives · 423 Views · Jul 02, 2012

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

Variables, Expressions and Arithmetic Operators in JAVA - slide 1
01 / 14Variables, Expressions and Arithmetic Operators in JAVA
1 / 17

Télécharger la présentation

Variables, Expressions and Arithmetic Operators in JAVA
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Variables, Expressions and Arithmetic Operators in JAVA Lecture #3 manish@cse.iitb.ac.in JAVA-NLP Lecture series

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. Arithmetic Operators • What is the value of –12 + 3 • An arithmetic operator is a symbol that asks for doing some arithmetic. OperatorMeaning Precedence - Unary minus highest + Unary Plus highest * Multiplication middle / Division middle % Modulus middle + addition low - Subtraction low JAVA-NLP Lecture series

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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. JAVA-NLP Lecture series