Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Programming Basics: Compatible Data Types, Constants, and Arithmetic Operators - Prof, Study notes of Javascript programming

An introduction to java programming basics, covering compatible data types, constants, and arithmetic operators. It includes examples and explanations of various concepts, such as data type compatibility, constant declaration, and arithmetic operations.

Typology: Study notes

Pre 2010

Uploaded on 09/17/2009

koofers-user-2o9
koofers-user-2o9 🇺🇸

4.5

(2)

10 documents

1 / 27

Toggle sidebar

Related documents


Partial preview of the text

Download Java Programming Basics: Compatible Data Types, Constants, and Arithmetic Operators - Prof and more Study notes Javascript programming in PDF only on Docsity! Chapter 2 Programming Building Blocks — Java Basics Compatible Data Types Any type in right column can be assigned to type in left column: Data Type Compatible Data Types byte byte short byte, short int byte, short, int, char long byte, short, int, long, char float float, byte, short, int, long, char double float, double, byte, short, int, long, char boolean boolean char char String Literals Escape Sequences • To include a special character in a String, use an escape sequence Character Escape Sequence Newline \n Tab \t Double quotes \" Single quote \' Backslash \\ Backspace \b Carriage return \r Form feed \f • Declare a variable only once • Once a variable is declared, its data type cannot be changed. These statements: double twoCents; double twoCents = .02; generate this compiler error: twoCents is already defined Constants • Value cannot change during program execution • Syntax: final dataType constantIdentifier = assignedValue; ote: assigning a value when the constant is declared is optional. But a value must be assigned before the constant is used. Advantages • Constants make the code more readable – PI is more meaningful than 3.14 • Prevent programmers from making logical errors – An example ? Expressions and Arithmetic Operators • The Assignment Operator and Expressions • Arithmetic Operators • Operator Precedence • Integer Division and Modulus • Division by Zero • Mixed-Type Arithmetic and Type Casting • Shortcut Operators Assignment Operator Syntax: target = expression; expression: operators and operands that evaluate to a single value --value is then assigned to target --target must be a variable (or constant) --value must be compatible with target's data type Operator Precedence Operator Order of evaluation Operation ( ) left - right parenthesis for explicit grouping * / % left - right multiplication, • Use () to clarify the order of calculations • () is essential only when the desired order of precedence is different from the rules of precedence division, modulus + - left - right addition, subtraction = right - left assignment Example You have 2 quarters, 3 dimes, and 2 nickels. How many pennies are these coins worth? int pennies = 2 * 25 + 3 * 10 + 2 * 5; = 50 + 30 + 10 = 90 Can also be written as : int pennies = (2*25) + (3*10) + (2*5); If you want to change the order of evaluation: int pennies = 2 * (25 + 3) * (10 + 2) * 5; Another Example Translate x into Java: 2y // incorrect! double result = x / 2 * y; => x * y 2 // correct double result = x / ( 2 * y ); Mixed-Type Arithmetic • When performing calculations with operands of different data types: – Lower-precision operands are promoted to higher-precision data types, then the operation is performed – Promotion is effective only for expression evaluation; not a permanent change – Called "implicit type casting" • Bottom line: any expression involving a floating-point operand will have a floating- point result. Rules of Promotion Applies the first of these rules that fits: 1. If either operand is a double, the other operand is converted to a double. 2. If either operand is a float, the other operand is converted to a float. 3. If either operand is a long, the other operand is converted to a long. 4. If either operand is an int, the other operand is promoted to an int 5. If neither operand is a double, float, long, or an int, both operands are promoted to int. Explicit Type Casting • Syntax: (dataType)( expression ) Note: parentheses around expression are optional if expression consists of 1 variable • Useful for calculating averages More Shortcut Operators Operator Example Equivalent += a += 3; a = a + 3; -= a -= 10; a = a - 10; *= a *= 4; a = a * 4; /= a /= 7; a = a / 7; %= a %= 10; a = a % 10; Common Error Trap • No spaces are allowed between the arithmetic operator and the equals sign • Note that the correct sequence is +=, not =+ Example: add 2 to a // incorrect a =+ 2; // a = +2; assigns +2 to a // correct a += 2; // a = a + 2; Operator Precedence Operator Order of evaluation Operation ( ) left - right parenthesis for explicit grouping ++ -- right - left preincrement, predecrement ++ -- right - left postincrement, postdecrement * / % left - right multiplication, division, modulus + - left - right addition or String concatenation, subtraction = += -= *= /= %= right - left assignment
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved