Comp Notes
Comp Notes
- Java has been designed to resemble some of the most popular programming languages
like C & C++
COMMENTS
Comments are useful in documenting a java program. They provide valuable information to a
programmer and make it easier for the programmer to understand the program.
- /* */ style comments can comment out multiple lines so they are useful when you want to
remove large blocks of code, perhaps for debugging purposes.
- // style comments are better for short notes of no more than a line.
- /** */ style comments are used for documentation purposes. These type of comments
stand out clearly and are easier to read.
- Note: /* */ can be used in the middle of a line whereas // can only be used at the end.
However putting a comment in the middle of a line makes code harder to read and is
generally considered to be bad.
TOKENS
The smallest individual unit in a program is known as a token.
Keywords
Identifiers
Literals
Operators
Separators
KEYWORDS
A keyword are words that conveys a special meaning to the compiler.
i.e. reserved words
IDENTIFIERS
Identifiers are names given to different parts of the program.
LITERALS
Literals are data items that have fixed data values.
- Integer literals, Float literals, Boolean literals, Character literals, String literals, Null
literals
OPERATORS
Operators are tokens that perform specific functions on data.
- Arithmetic, Relational, Logical
SEPARATORS
Used as separators or punctuators.
() {} [] ; , .
DATA TYPES, VARIABLES, LITERALS AND CONSTANTS IN JAVA
DATA TYPES
- Primitive or In-built data type
- Reference Data Type
- Character
char
- Boolean
boolean
type size
byte 1 byte
short 2 bytes
int 4 bytes
long 8 bytes
float 4 bytes
double 8 bytes
char 2 bytes
boolean reserves 8 bits, uses only 1 bit
User-defined reference types (also known as user-defined objects or user-defined data types)
VARIABLES
CONSTANTS
A constant is a variable whose value cannot be changed once it has been assigned a value.
If the value is changed subsequently in the program, the compiler gives an error.
The keyword final makes a variable as constant.
example: final double pi=3.14
Advantages of constants:
- Indicates to the programmer that the value in that constant (variable) cannot be changed
and hence makes it easier to read and check for correctness. Even if the programmer
accidentally changes the value of that constant in the code, the compiler gives an error
- If the value in the constant needs to be changed, only the declaration needs to be
modified. On the other hand if a literal is used, one has to search through the entire
program for every occurrence of that specific value and then change all the occurrences
SUFFIXES
Suffixes are used to explicitly specify the data type of a literal data value.
- In some situations it is required, because the compiler gives an error, when the value that
is assigned to a variable is beyond the capacity of the variable on the left hand side.
- Suffixes are f or F , d or D, l or L.
OPERATORS
PRECEDENCE
SCANNER CLASS
Scanner Syntax
Scanner in = new Scanner(System.in); // creating a Scanner object
<variable data type>=<scanner object>.next(<data type>);
ARITHMETIC AND BOOLEAN EXPRESSIONS
EXPRESSIONS
An expression is composed of one or more operations.
Types of expressions:
- Arithmetic expression
- Boolean/Logical expression
ARITHMETIC EXPRESSIONS
An expression that results into false or true is called a Boolean expression or a Logical
expression
- A Boolean expression that is formed only with variables, constants, literals and relational
operators is called a Simple Relational or Logical expression.
- A Boolean expression that is formed with more than one simple relational expression,
linked by logical operators is called a Complex Relational or Logical Expression
TYPES OF ERRORS
Compilation Error
- Semantic error
- Syntax error
Logical Error
Runtime Error
SEMANTIC ERROR
Caught by compiler at the time of compilation
- When you use prefix operators with Boolean variable
- Declaring a variable twice using the same data type/different data type
- Type incompatibility
- Using a non-initialized variable
SYNTAX ERROR
Caught by compiler at the time of compilation
- Semicolon is missing
- The keyword is misspelt or in upper case
- Curly braces or parenthesis are missing
- Identifiers are not valid
- Variables are not declared
- When 2 operands and/or literals occur continuously without an operator in between them.
LOGICAL ERROR
The programmer has to trace this type of error.
When the program does not give the expected output, then it is said to have a logical error.
Can be the hardest errors to find. You need to spend time going through your code looking for a
precise reason for the error.
RUNTIME ERROR
This occurs during the execution of the program and it causes the program to end abruptly.
The programmer has to anticipate these types of errors while writing the program.
Occurs when
◦ a numeric literal or variable is divided by zero
◦ when the user inputs a string or a character, where a number is expected.
CONDITIONAL CONSTRUCT
IF CONSTRUCT
An “if” statement consists of a Logical Expression. If the condition evaluates to true the
statement to be executed is placed after the “if” statement. The statement after the else part is
executed if the condition is not true.
Syntax:
if(logical expression)
statement;
else
statement;
IF ELSE IF CONSTRUCT
Multiple conditions which are related can be specified with the “else if” clause.
When there are N number of possibilities, then there needs to be N-1”if” and “else if” statements.
if(logical expression)
statement;
else if(logical expression)
statement;
else
statement;
NESTED IF CONSTRUCT
A “nested if” is basically another set of if statements, inside another “IF” construct with or
without the else clause.
“Nested if” statements are useful when we need to execute specific set of statements for each
conditional construct.
if(num>0) // outer if
{
if(num<10) //inner if
System.out.println(num+” is between 0 and 10”)
TYPE CONVERSIONS
the process of converting one predefined data type into another is called type conversion.
this type of conversion usually is applied in a mixed arithmetic expression. all operands are
converted to the largest data type in the expression, to prevent loss of data.
a data type of lower size (occupying less memory) is assigned to a data type of higher size. this is
done implicitly.
example:
int x=10
double y=x
this is a user defined conversion that forced an expression to be of specific type. this is called
type casting.
impossible conversions:
- primitive type to a reference type and vice versa
- null value to a primitive type
- primitive to Boolean
- Boolean to primitive type
SWITCH CASE
A “switch… case” is a more compact statement and a faster alternative to the “if” construct.
It is generally used when a value must be tested against a list of values for equality.
it cannot be used in situation where a range of values or more complex conditions need to be
checked.
it is a multiple branch selection statement that allows a variable or the result of an arithmetic
expression to be tested for equality against a list of values.
FALL THROUGH
whenever there is no break statement involved in a switch case block, all the statements are
executed even if the test expression is satisfied.
PROGRAMMING CONSTRUCTS
A for loop is a repetition control structure that allows you to repeatedly execute one or more
statements a specific number of times.
syntax:
for(initialization; boolean_expression; update_expression)
{
statement;
statement;
}
It is also known as an entry-controlled loop, as the body of the loop is only executed if the test
expression is true.
multiple initializations and update expressions are separated by commas.
infinite loop:
- it is a loop that runs indefinitely. it normally occurs if the test condition is incorrect. it
results in a logical error.
empty loop:
- this loop does not contain any statement in its loop-body, thus becomes an empty loop.
- semicolon at the end of the for loop indicates that it is a for loop with no statement under
it.
- empty loops can be used as timer loops and for making the code more compact.
- timer loops can be used to cause a delay or create a pause in the program.
WHILE LOOP
A while loop continually executes a block of statements while a particular condition is true.
In a while loop, a loop control variable should be initialized before the loop begins as an
uninitialized variable cannot be used in the expression in the while statement.
The loop variable should be updated inside the body of the while loop.
syntax:
initialization statement;
while(boolean expression)
{
statement(s);
update statement;
}
Similar to the ‘for’ loop the while loop is also an entry-controlled loop as the body of the loop
might never be executes, when the expression is tested and the results is false.
the actions inside the loop will be executed till the result of the Boolean expression remains true.
In a for loop construct, initialization, conditional expression and update statement can be in the
loop statement, whereas in a ‘while’ loop construct, only the conditional expression can be in the
loop statement.
DO WHILE LOOP
A do while loop is like a while loop, except that a do while loop is guaranteed to execute at least
once.
syntax:
do
{
//statements
}
while(boolean_expression);
this is called an exit controlled loop because the condition is tested after the statements are
executed at least once.
TERNARY/CONDITIONAL OPERATOR
It is an operator that is used for evaluating a conditional expression and executing an operation
based on the result of the condition.
syntax:
boolean expression ? value 1 : value 2
The increment operator ++ or the decrement operator -- can be put in front of a variable or
behind a variable.
When it is put before, it is a prefix operator. When it is put behind a variable it is a postfix
operator.
In both the cases, the operators either increment or decrement the value of a variable. However,
the operators work differently when they are used as part of an arithmetic expression.
The prefix operator has the highest precedence. The value of the variable it precedes is first
increased/decreased by 1 and then it used in a binary operation.
JUMP STATEMENTS
BREAK STATEMENT
The ’break’ statement in Java is used in the “switch..case” construct and in the Looping
constructs.
When the break statement is encountered inside a loop, the loop is terminated immediately (even
if the test expression of the loop is still true), and the program control resumes at the next
statement following the loop. It is used with the ‘if’ construct.
The break statement must be the last statement inside the ‘if’ construct.
CONTINUE STATEMENT
The “continue” statement continues with the next iteration skipping the rest of the loop
statements in the loop body.
In the case of for loop, “continue” transfers the control to the update statement in the for loop
and then evaluates the test-expression.
In the case of “do while” and “while” loop, the program control passes to the test-expression.
The next iteration is executed only if the test expression evaluates to true.
NESTED LOOPS
When a loop contains another loop in its body, then it is said to be a Nested Loop.
In a nested loop, the outer loop determines the number of times the inner loop runs.
If a “break” statement appears in a nestedloop, then it causes an exit from the very loop it
appears in. In other words it exits out of the inner loop only. In order to exit out of the outer loop,
another break statement needs to be given.
Similarly the “continue” statement, continues with the iteration in the loop it appears in. For
example, if it is in the inner loop, then it continues with the iteration in the inner loop.
DEFINTIONS
An expression that results into false or true is called a Boolean expression or a Logical
expression
An “if” statement consists of a Logical Expression. If the condition evaluates to true the
statement to be executed is placed after the “if” statement. The statement after the else part is
executed if the condition is not true.
A “nested if” is basically another set of if statements, inside another “IF” construct with or
without the else clause.
Mixed Expressions are where the operands are of different data types
the process of converting one predefined data type into another is called type conversion.
a data type of lower size (occupying less memory) is assigned to a data type of higher size. this is
done implicitly and is known as implicit type conversion.
this is a user defined conversion that forced an expression to be of specific type. this is called
type casting.
A “switch… case” is a more compact statement and a faster alternative to the “if” construct.
A for loop is a repetition control structure that allows you to repeatedly execute one or more
statements a specific number of times.
infinite loop is a loop that runs indefinitely. it normally occurs if the test condition is incorrect. it
results in a logical error.
If a loop does not contain any statement in its loop-body, it is said to be an Empty loop.
A while loop continually executes a block of statements while a particular condition is true.
A do while loop is like a while loop, except that a do while loop is guaranteed to execute at least
once.
The ternary/conditional operator is the only operator to take three operands.