11
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

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

Embed Size (px)

Citation preview

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

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

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

Objectives

• Understand what an expression is

• Understand how to create expressions in Java

Let’s get started!

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

Variables• We always declare the variable, by specifying the type firstint 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 lowercaseletter.

• 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;

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

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 firstDoubleVariablelocation.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.htmlin our book has more detailed information & makes for fun reading!

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

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 conversionratio 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

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

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 + firstDoubleVariableis an expression. What are its components?

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

Expressions

• Operators:+ - * / arithmetic expressionsMath.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 operatorE.g. 11 % 4 is 311.0/ 4 is 2.7511/4 is 2

Operands can be literal values, initialized variables, or other expressions.

 

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

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 8int b = 2* n++; //b is 14, n is 8

 

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

Expressions

• Operators that create a boolean expression:

> < <= >= for comparing quantitiesE.g., (a>b) is an expression. What are its components?

== for comparing two variables or expressions’ valuesE.g., (a ==b ) is an expression. What are it’s components?

!= stands for not equal toE.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

 

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

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

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

• 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