Basic Coding Elements of Java


Buy Shoes Online
 Basic Coding Elements of Java  

 

 

 

  Your code is written in a series of statements, which can be organized into blocks. These statements contain data and operators, which are components of expressions. You can annotate your code using comments, which makes the code more understandable.

Statements

Any line of code before a semicolon is known as a statement and is executed by the Java interpreter when it hits the semicolon; after executing that statement, the interpreter moves to the next statement. Each statement contains instructions for using data and operators.

Expressions

An expression is a part of a statement that uses data and operators to return a value.

Blocks

A block is a collection of statements enclosed in curly braces. Any variables that you declare and that assign values within a block are erased when the flow of execution leaves the block. The block in which the variable's value exists is called the scope of the variable.

Comments

Comments are used to annotate the code so that a reader can understand the purpose of certain lines and blocks of code. Comments are ignored by the Java compiler. Multiline comments are preceded by /* and are ended with */. Single line comments are preceded by //. The double slash "comments out" text only to the end of a line. A comment would appear as follows:

/*  This declares the length variable for the Rectangle class   */
int length;

or

int length;   //Declares the length variable for the Rectangle class

Data Types

To represent data values in your code you use literals. Literals are described by types, named by identifiers, and stored in variables.

When you use literals in your code, they appear in their raw form rather than as a result of an expression. Several types of literals are commonly used: numbers, integers, floating points, characters, Booleans, and strings.

Table 1.1 outlines Java's strict definitions of these data types.

 

Table 1.1. Rules for Java literals.

Literal Type Typename Rule
Number Num Can_ be integer, floating point, or character.
Integer   Can be decimal, hexadecimal, or octal.
  Byte 8-bit integers between -128 and 127.
  Short 16-bit integers between -32768 and 32767.
  Int 32-bit integers between -2147483648 and 2147483647.
  Long 64-bit integers between -9223372036854775808 and 9223372036854775807 or have L or l appended to them.
  Hex Preceded by 0x or 0X.
  Oct Preceded by 0.
Floating point   Any number with a decimal point. Can be made exponential by appending an e or E, followed by the exponent.
  Float 32-bit.
  Double 64-bit.
Character Char 16-bit integers represented by a single character and enclosed in single quotes.
    In Java, the Unicode character map is used. The following special characters must be represented by escape sequences:
    backspace      \b
    backslash       \\
    carriage return  \r
    double quote   \"
    formfeed        \f
    hex number     \xhh
    horizontal tab   \t
    newline         \n
    octal number    \000
    question mark  \q
    single quote     \'
    vertical tab      \v
Boolean Boolean Can only be true or false. Are not represented by 0 or 1.
String String Zero or more characters enclosed in double quotes.

 

Literals are described by identifiers. Identifiers are sequences of letters and digits, and can also be used to describe variables, methods, and classes. Identifiers can consist of any letter from a to z, underscore, dollar sign, digits from 0 to 9 (except as the first character); identifiers are case-sensitive.

Java has several reserved keywords that are its own identifiers, which cannot be used as identifiers in any way other than that defined by Java, as listed in Table 1.2. Though these words are reserved, not all are used in the most recent release.
 

 

Table 1.2. Reserved keywords.

Abstract else int short
boolean extends interface static
break final long super
byte finally native switch
case float new synchronized
cast for null this
catch future operator throw
char generic outer throws
class goto package transient
const if private try
continue implements protected var
default import public void
do inner rest volatile
double instanceof return while

 

Operators

Operators are used to compare values. Java has strict definitions of operators. It doesn't allow for overloading, which is a C developer's common practice of changing the behavior of operators.

Java provides two types of operators: binary and unary. Binary operators are used to compare two values. Unary operators use a single value, for example:

a >= b
a++

The first example uses a binary operator, >=, which compares variables a and b. The second is a unary operator, ++, which increments the value of a by one.

All of Java's binary and unary operators can be found in Table 1.3. They are organized according to the precedence with which they are performed.
 

 

Table 1.3. Binary and unary operators.

Operator Description
., (), [] Precedence overriding decimal, parentheses, brackets
!, ~, ++, -- Boolean negation, bitwise complement, increment, decrement
*, /, % Multiplication, division, modulus
+, - Addition, subtraction or unary negation
<<, >>, >>> Left shift, right shift, zero-fill right shift
<, <=, >, >= Less than, less than or equal to, greater than, greater than or equal to
==, != Equals, is not equal to
& Bitwise or Boolean AND
^ Bitwise or Boolean XOR
| Bitwise or Boolean OR
&& Evaluation AND, Logical AND
|| Evaluation OR, Logical OR
?: If…then…else
=, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>= Assignment operators
, Comma

 

Declaring Variables

There are four types of statements to use for variables: declarations, assignments, initializers, and casts.

You must always declare variables before you can use them in your Java program. Variable declarations assign data types to variables. A declaration statement in Java consists of a data type followed by an identifier. Any of the data types listed in the previous table can be used to declare variables, for example:

Boolean IsReady;
float miles;
int x, y, z;
short pages;

Assignments are statements that assign values to variables. These, like declarations, are required before variables can be used. They are called by setting an identifier equal to a value. This value, of course, must be compatible with the data type assigned to the variable identifier. Initializers are assignment statements that are combined with the declaration statement, for example:

Boolean IsReady = false
float miles = 3.62
short pages = 240

If you want certain variable values to remain constant in your code, you can use the final keyword, which ensures that the variable cannot be changed by the code. Its form is this:

final int pages = 500

Casts are statements you use if you need to place a value of one type into a variable of another type. In C++, automatic coercion allows you to do this without declaring that you were aware of this change. In Java, you must explicitly call such an instruction with a cast statement. Cast statements are generally called as follows:

datatype identifier = (datatype) identifier

Java allows casts only between compatible data types.

In this section, you have learned about the data and operators that are used in expressions that are parts of statements. You now understand that statements are organized with blocks and annotated with comments. You have also examined some basic statements that deal with variable declarations. These are the fundamental elements of Java coding.

 

 

 

 

 
 
 
Copyright Manjor Inc.