When naming your fields or variables, there are rules and conventions that you should (or must) follow. The eight primitive data types are: byte, short, into, long, float, double, Boolean, and char. The java. Lambasting class represents character strings. The compiler will assign a reasonable default value for fields of the above types; for local variables, a default value is never assigned. A literal is the source code representation of a fixed value. An array is a container object that holds a fixed number of values of a single type.
The length of an array is established when the array is created, After creation, its length is fixed. Summary of Operators The following quick reference summarizes the operators supported by the Java programming language. Simple Assignment Operator = Simple assignment operator Arithmetic Operators * Additive operator (also used for String concatenation) – Subtraction operator * Multiplication operator / Division operator % Remainder operator Lunar Operators * Unary plus operator; indicates positive value (numbers are positive without this, however) – unary minus operator; negates an expression
Increment operator; increments a value why I Decrement operator; decrements a value by I ! Logical complement operator; inverts the value of a Boolean Equality and Relational Operators Equal to Not equal to > Greater than Greater than or equal to < Less than Less than or equal to Conditional Operators Conditional-AND II Conditional-OR ? : Ternary (shorthand for if-thenelse statement) Type Comparison Operator instance Compares an object to a specified type Bitwise and Bit Shift Operators -? unary bitwise complement Signed left shift Signed right shift unsigned right shift gamma, Bitwise AND
Bitwise exclusive OR I Bitwise inclusive OR Expressions, Statements, and Blocks Now that you understand variables and operators, it’s time to learn about expressions, statements, and blocks. Operators may be used in building expressions, which compute values; expressions are the core components of statements: statements may be grouped into blocks. Expressions An expression is a construct made up of variables, operators, and method invocations, which are constructed according to the syntax of the language, that evaluates to a single value.
You’ve already seen examples of expressions, illustrated in bold below: into cadence -O; unary[O] = 1 00; 1 at index O: ” + unary[0]); into result = I 2; // result is now 3 if (value values) values”); The data type of the value returned by an expression depends on the elements used in the expression. The expression cadence O returns an into because the assignment operator returns a value of the same data type as its left- hand operand; in this case, cadence is an into_ As you can see from the other expressions, an expression can return other types of values as well, such as Boolean restoring.
The Java programming language allows you to construct impound expressions from various smaller expressions as long as the data type required by one part Of the expression matches the data type Of the Other. Here’s an example of a compound expression: In this particular example, the order in which the expression is evaluated is unimportant because the result of multiplication is independent of order; the outcome is always the same, no matter in which order you apply the multiplications. However, this is not true of all expressions.
For example, the following expression gives different results, depending on whether you perform he addition or the division operation first: x 4 y,’ 100 // ambiguous You can specify exactly how an expression will be evaluated using balanced parenthesis: and For example, to make the previous expression unambiguous, you could write the following: (x ;y)/ 100 // unambiguous, recommended It you don’t explicitly indicate the order for the operations to be performed, the order is determined by the precedence assigned to the operators in use within the expression. Operators that have a higher precedence get evaluated first.
For example, the division operator has a higher precedence than does the addition operator. Therefore, the following two statements are equivalent: my/ 100 x + (y / 100) // unambiguous, recommended When writing compound expressions, be explicit and indicate With parentheses which operators should be evaluated first. This practice makes code easier to read and to maintain. Statements Statements are roughly equivalent to sentences in natural languages. A statement forms a complete unit of execution, The following types of expressions can be made into a statement by terminating the expression with a semicolon C).
Assignment expressions Donna use of or Committed invocations object creation expressions Such statements are called expression statements. Here are some examples of expression statements. // assignment statement value = 8933. 234; // increment statement value++; // method invocation statement World! “); // object creation statement Bicycle imbibe = new Bicycle(); In addition to expression statements, there are two Other kinds Of statements: declaration statements and control flow statements. A declaration statement declares a variable.
You’ve seen many examples Of declaration statements already: // declaration statement double value = 8933. 34 Finally, control flow statements regulate the order in which statements get executed. You’ll learn about control flow statements in the next section, Control Flow Statements Blocks A block is a group of zero or more statements between balanced braces and can be used any. Veer a single statement is allowed, The following example, Blockaded, illustrates the use of blocks: class Blockaded { public static void main(String[] rags) { Boolean condition true; if (condition) { // begin block 1 is true. ); ) // end block one else { // begin block 2 System. Out. Print(“Condition is false. “” // end block 2 Summary of Control Flow Statements The if-then statement is the most basic of all the control flow statements. It tells your program to execute a certain section of code only if a particular test evaluates to true. The if-then-else statement provides a secondary path of execution when an “if” clause evaluates to false. Unlike if-then and if-then- else, the switch statement allows for any number of possible execution paths, Thiele and do-while statements continually execute a block to statements while a particular condition is true.
The difference between do-while and while s that do-while evaluates its expression at the bottom to the loop instead to the top. Therefore, the statements within the do block are always executed at least once. The for statement provides a compact way to iterate over a range of values. It has two forms, one of which was designed for looping through collections and arrays. Bicycle mike= new Bicycle(); In addition to expression statements, there are t’,VOW Other kinds of statements: double value = 8933. 234; A block is a group dozer or more statements between balanced braces and can he used nap. ‘here a single statement is allowed.