Upload
natalie-banks
View
246
Download
0
Embed Size (px)
Citation preview
Java Structureimport java_packages;class JavaClass {
------member variables declarations;------
void otherMethod( ) {------------
}
public static void main(String[] args) {------local variables declarations;------
}}
Java Structure
Example:
public class BasicsDemo { public static void main(String[] args) {
int test1 = 10;int test2; test1 += 5;test2 = test1 + 15;System.out.println(“Test 1 = " + test1); System.out.println(“Test 2 = " + test2);
} }
Variables
Definition: A variable is an item of data named by an identifier.
– You must explicitly provide a name and a type for each variable you want to use in your program.
– The variable's name must be a legal indentifierAn unlimited series of Unicode characters that begins with a letter.
– You use the variable name to refer to the data that the variable contains.
– The variable's type determines what values it can hold and what operations can be performed on it.
Variables Declaration
A variable declaration:var_type var_name = var_initialization ;
Example:MaxVarDemo.java
Data Types
• Every variable must have a data type. • A variable's data type determines the values
that the variable can contain and the operations that can be performed on it.
• The Java programming language has two categories of data types: – Primitive – Reference
• A variable of primitive type contains a single value of the appropriate size and format for its type: a number, a character, or a boolean.
• The value of a reference type variable is a reference to (an address of) the value or set of values represented by the variable: an arrays, a class, and an interface.
Primitive Data Types
Keyword Description Size/Format
(integers)
byte Byte-length integer 8-bit two's complement
short Short integer 16-bit two's complement
int Integer 32-bit two's complement
long Long integer 64-bit two's complement
(real numbers)
floatSingle-precision floating point
32-bit IEEE 754
doubleDouble-precision floating point
64-bit IEEE 754
(other types)
char A single character 16-bit Unicode character
booleanA boolean value (true or false)
true or false
Literal Values• A literal primitive value can directly put in the code.• Example: int anInt = 4; • The digit 4 is a literal integer value.
Literal Data Type
178 int
8864L long
37.266 double
37.266D double
87.363F float
26.77e3 double
' c ' char
true boolean
false boolean
Variable Name
• In the Java programming language, the following must hold true for a simple name: – It must be a legal identifier. An identifier is an
unlimited series of Unicode characters that begins with a letter.
– It must not be a keyword, a boolean literal (true or false), or the reserved word null.
– It must be unique within its scope. A variable may have the same name as a variable whose declaration appears in a different scope.
• By Convention : – Variable & Method names begin with a
lowercase letter – Class name begin with an uppercase letter. – If a variable, method or class names consist of
more than one word, the words are joined together, and each word after the first begins with an uppercase letter, like this:
isVisible.
Variable Scope• The location of the variable declaration within your program establishes its scope
and places it into one of these four categories: – member variable– local variable – method parameter – exception-handler parameter
Variable ScopeMember (Instance) Variable:• A member variable is a member of a class or an object. • It is declared within a class but outside of any method or
constructor. • A member variable's scope is the entire class.
Local Variable:• Local variables are declared within a block of code. • In general, the scope of a local variable extends from its
declaration to the end of the code block in which it was declared.
• Consider the following code sample: if (...) {
int i = 17; ...
} System.out.println("The value of i = " + i); // error
• The final line won't compile because the local variable i is out of scope.
Variable ScopeMethod Parameter:• Parameters are formal arguments to
methods and are used to pass values into methods.
• The scope of a parameter is the entire method for which it is a parameter.
Exception-handler Parameter:• Exception-handler parameters are
similar to parameters but are arguments to an exception handler rather than to a method.
• The scope of an exception-handler parameter is the code block between { and } that follow a catch statement.
Variable Initialization • Local variables and member variables can be
initialized with an assignment statement when they're declared.
• The data type of the variable must match the data type of the value assigned to it.
• Examples:// integers byte byte largestByte = Byte.MAX_VALUE; short largestShort = Short.MAX_VALUE; int largestInteger = Integer.MAX_VALUE; long largestLong = Long.MAX_VALUE;
// real numbers float largestFloat = Float.MAX_VALUE; double largestDouble = Double.MAX_VALUE;
// other primitive types char aChar = 'S'; boolean aBoolean = true;
Final Variable• The value of a final variable cannot
change after it has been initialized. • Such variables are similar to constants
in other programming languages. • To declare a final variable, use the
final keyword in the variable declaration before the type:
final int aFinalVar = 0;
• The previous statement declares a final variable and initializes it, all at once.
• Subsequent attempts to assign a value to aFinalVar result in a compiler error.
Final Variable• A final local variable that has been
declared but not yet initialized is called a blank final.
final int blankfinal; . . . blankfinal = 0; . . .
• Once a final local variable has been initialized, it cannot be set again.
• Any later attempts to assign a value to blankfinal result in a compile-time error.
Operators• An operator performs a function on one, two, or
three operands. • An operator that requires one operand is called a
unary operator.
operator operand //prefix notation operand operator //postfix notation
• An operator that requires two operands is a binary operator.
operand1 operator operand2 //infix notation
• An operator that requires two operands is a ternary operator.
operand1 ? operand2 : operand3 //infix notation
Arithmetic Operators
Operator Use Description
+ op1 + op2 Adds op1 and op2
- op1 - op2 Subtracts op2 f rom op1
* op1 * op2 Multiplies op1 by op2
/ op1 / op2 Divides op1 by op2
% op1 % op2 Computes the remainder of dividing op1 by op2
Example: ArithmeticDemo.java
Binary Operator:
Arithmetic Operators
Example: ArithmeticDemo2.java
Unary Operator:
Operator Use Description
++ op++
I ncrements op by 1; evaluates to the value of op
before it was incremented
++ ++op
I ncrements op by 1; evaluates to the value of op
af ter it was incremented
-- op--
Decrements op by 1; evaluates to the value of op
before it was decremented
-- --op
Decrements op by 1; evaluates to the value of op
af ter it was decremented
Relational Operators
Example: RelationalDemo.java
Operator Use Returns true if
> op1 > op2 op1 is greater than op2
>= op1 >= op2 op1 is greater than or equal to op2
< op1 < op2 op1 is less than op2
<= op1 <= op2 op1 is less than or equal to op2
== op1 == op2 op1 and op2 are equal
!= op1 != op2 op1 and op2 are not equal
A relational operator compares two values and determines the relationship between them.
Conditional Operator
Relational operators often are used with conditional operators to construct more complex decision-making expressions.
Conditional Operators:
Operator Use Returns true if
&& op1 && op2 op1 and op2 are both true
|| op1 || op2 either op1 or op2 is true
! ! op op is f alse
Example: ConditionalDemo.java
Shift OperatorsA shift operator performs bit manipulation on data by shifting the bits of its first operand right or left.
Operator Use Operation
>> op1 >> op2 shif t bits of op1 right by distance op2
<< op1 << op2 shif t bits of op1 lef t by distance op2
>>> op1 >>> op2 shif t bits of op1 right by distance op2 (unsigned)
Example:13 >> 1 6
• The binary representation of the number 13 is 1101. • The result of the shift operation is 1101 shifted to the
right by one position 110, or 6 in decimal. • The left-hand bits are filled with 0’s as needed.
Logical (Bitwise) Operators
Operator Use Operation
& op1 & op2 bitwise AND
| op1 | op2 bitwise OR
^ op1 ̂op2 bitwise XOR
~ ~op2 bitwise complement
AND Truth Table
op1 op2 Result
0 0 0
0 1 0
1 0 0
1 1 1
OR Truth Table
op1 op2 Result
0 0 0
0 1 1
1 0 1
1 1 1
XOR Truth Table
op1 op2 Result
0 0 0
0 1 1
1 0 1
1 1 0
Logical (Bitwise) Operators
Example:13 & 12 1213 & 12 1313 & 12 1
• The binary representation of the number 13 is 1101. • The binary representation of the number 13 is 1100.
1101 13 1101 13 1101 13& 1100 12 | 1100 12 ^ 1100 12
------ ------ ------1100 12 1101 13 0001 1
• Example Program:ShiftLogicalDemo.java
Assignment Operators Operator Use Equivalent to
+= op1 += op2 op1 = op1 + op2
-= op1 -= op2 op1 = op1 - op2
*= op1 *= op2 op1 = op1 * op2
/ = op1 / = op2 op1 = op1 / op2
%= op1 %= op2 op1 = op1 % op2
&= op1 &= op2 op1 = op1 & op2
|= op1 |= op2 op1 = op1 | op2
=̂ op1 =̂ op2 op1 = op1 ̂op2
<<= op1 <<= op2 op1 = op1 << op2
>>= op1 >>= op2 op1 = op1 >> op2
>>>= op1 >>>= op2 op1 = op1 >>> op2
Other Operators Shortcut if-else Statement• The ?: operator is a conditional operator that is short-
hand for an if-else statement:
op1 ? op2 : op3 • The ?: operator returns op2 if op1 is true or returns op3
if op1 is false.
The [ ] Operator• Square brackets are used to declare arrays, to create
arrays, and to access a particular element in an array. • Here's an example of an array declaration:
float[] arrayOfFloats = new float[10]; • The previous code declares an array that can hold ten
floating point numbers. • Here's an example to access the 7th element of array :
arrayOfFloats[6];
Other Operators The . Operator• The dot (.) operator accesses instance members of an
object or class members of a class.
The () Operator• When declaring or calling a method, the list of the
method's arguments are put between ( and ).
The (data type) Operator• Casts (or "converts") a value to the specified type.• Example:
……int i;float j = 10.5;i = (int) j + 11;……