150 likes | 229 Views
Learn fundamental concepts in Java such as variables, assignments, operators, parameter passing, and arithmetic operations. Understand how to work with primitive data types and how to utilize logical and relational operators effectively.
E N D
Names of variables, functions, classes (all user defined objects), Identifiers: Examples: if else while int float double main ... Can’t be used as identifiers They are reserved word a b gcd GCD A COSC1373 TAX Tax_Rate Tax Rate No special chars $ “ ; , ... Two identifiers, Tax andRate 1 123 No numbers ITK 168
A variable: A name of a location in memory to hold value A value of what? • 11110101 int a; String A; 245 0010 0010 0010 0100 0010 0101 • the name • the type • the value ITK 168
Text String Binary: 0101 0100 0110 0101 0110 1100 0010 1010 0010 0000 0011 0010 0011 0010 0011 1000 0011 0111 0011 0010 0011 0100 0011 1001 ITK 168
http://www.dynamoo.com/technical/ascii.htm ASCII American National Standard Code for Information Interchange • 7-bit code ITK 168
Numbers inside Computers Java’s Types short int long float double ITK 168
8 primitive data types in Java short i,j; int no; long sum; float total; double tax; char grade; byte b; boolean yes_or_no; i = 3; j = 3.14; tax = 8.25/100; grade = ‘A’; grage = ‘B’; grade = 97; yes_or_no = true; yes_or_no = false; Every variable has to be declared before we use it. ITK 168
Variables and Assignments Everything that can receive a value L-value = R-value Everything that can give a value Assignment operator variable function expression literal variable The data types in both sides have to be consistent. If not, type-casting must be done. ITK 168
L-value = R-value Assignment examples i = (3+7)*2-4-2*5; i = 3/2-0.5; illegal 3 = j; gcd(24,18) = 2+3; i+1 = j+3; i = 3/2.0-0.5; i = i+2; total = i+j; average = total/2; sum = average*10*tax_rate; i = gcd(24,18); i = Integer.parseInt( ); JOptionPane.showInputDialog("Input y") ITK 168
More on Assignments This operation itself will give a value L-value = R-value L-value = R-value L-value = L-value = a = b = c = i+j+3+gcd(15,6); illegal: a = b+1 = c = i+j+3+gcd(15,6); ITK 168
Parameter Passing formal parameters int plus (int a, int b) { return a+b; } int x = plus(1,2); function body • How are parameters passed? • Looks simple enough… • There are some techniques actual parameters (arguments) function call ITK 168
Parameter Correspondence • Which actual parameters go to which formal parameters? • Most common case: positional parameters • Correspondence determined by positions • nth formal parameter matched with nth actual ITK 168
Parameter Passing int minus (int a, int b) { return a - b; } int x = minus(1, 2); function call 1 a 2 b ITK 168
Local variables int minus (int a, int b) { int x = a – b; return x; } publicstaticvoid main(String[] args) { .... int a = 3; int b = 2; int d = 0; int x = a+b; d = minus(b,a); ..... } ITK 168
Arithmetic operators in Java + - * / % (x % y) is the remainder of x divided by y + - * / % (x % y) is the remainder of x divided by y Relational and logical operators in JavaResulting true or false < <= > >= == != && || ! & | ^ (20 < y) && (x <= i) ITK 168
|| && ! Logical Operators Assume x = 10 true (1 || 0) ((18 <= x) && (x <= 50)) ((18 <= x) || (x <= 50)) !(x < 5) is same as (x >= 5) (((x % 2) == 0) && ((x % 3) == 0)) false true true false ITK 168