Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Expressions MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.

Similar presentations


Presentation on theme: "Java Expressions MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh."— Presentation transcript:

1 Java Expressions MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh Bajaj, 2006. All rights reserved

2 Objectives Understand what an expression is Understand how to create expressions in Java Let’s get started!

3 Variables We always declare the variable, by specifying the type first int anIntegerVariable; ch aCharVariable; double firstDoubleVariable,secondDoubleVariable; Variable names must begin with a letter or _ and be a sequence of digits or letters We can declare anywhere in Java, within the method. It’s a good idea to declare at the start of each method, and in one place in the class (outside of the methods of the class) Symbols like + or © cannot be used within a variable name The convention in Java is to start the variable name with a lowercase letter. Just like variable declarations, object declarations need the class first. Again, object names start with a lower case letter, but class names start with an uppercase letter. E.g., String aStringObject;

4 Variables We can typecast between certain types. So, a variable of type int can be assigned a value contained in another variable that is of type double, only if we explicitly typecast the value to int. anIntegerVariable = (int) firstDoubleVariable; This does not change the value in the firstDoubleVariable location.It simply takes the RHS value, and truncates it before assigning it to the LHS. The value in the RHS location is not changed. We can only typecast between certain types. For example, between a double and int. But it makes no sense to typecast between a boolean and anything else and so this is not supported. http://nfp.cba.utulsa.edu/bajaja/mis3023/Text/javanotes4.1/c2/s5.html in our book has more detailed information & makes for fun reading!

5 Constants A constant in Java is a memory location that contains one, unchangeable value It cannot be in a method. It has to be outside of all methods. Why do we need a constant? - make one change instead of throughout the class - More meaningful name for the value E.g., In a program that converts meters to feet, the conversion ratio can be a constant. Class UsesConstants { static final double conversionMetersFeet=3.3; public static void main (String[] args) { ----------//code in main --------//more code in main }//end main }//end class UsesConstants

6 Expressions An expression consists of: -an operator -one or more operands -a type -a value unary operator: 1 operand, binary operator: 2 operands, ternary operator: 3 operands. E.g., anIntegerVariable + firstDoubleVariable is an expression. What are its components?

7 Expressions Operators: + - * /arithmetic expressions Math.pow( double base, double exp ) returns the double value of base raised to the power of exp We will use this in assignment 1! %mod operator E.g. 11 % 4 is 3 11.0/ 4 is 2.75 11/4 is 2 Operands can be literal values, initialized variables, or other expressions.

8 Expressions Operators: The increment and decrement operators: n = n + 1; Can be written as n++; Or ++n; int m = 7; int n = 7; int a = 2* ++m; // a becomes 16, m is 8 int b = 2* n++; //b is 14, n is 8

9 Expressions Operators that create a boolean expression: > = for comparing quantities E.g., (a>b) is an expression. What are its components? == for comparing two variables or expressions’ values E.g., (a ==b ) is an expression. What are it’s components? != stands for not equal to E.g., (a!=b) is an expression. What are its components? && stands for the AND operator || stands for the OR operator. Some examples of creating boolean expressions? TestComparisonOperators.java

10 Expressions boolean sameSign; sameSign = ((x > 0) == (y > 0)); What does this evaluate to? We use expressions to -build larger expressions -on the RHS of assignment statements -boolean expressions can be used in if, for, while, do-while, switch statements

11 We understood how to use expressions in Java More details can be found in section 2.5 of our text: http://nfp.cba.utulsa.edu/bajaja/mis3023/Text/javanotes4. 1/c2/s5.html which should be fun to read now! CONCLUSION


Download ppt "Java Expressions MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh."

Similar presentations


Ads by Google