Chapter 2 Fundamentals of Java Programming AutoRecovered
Chapter 2 Fundamentals of Java Programming AutoRecovered
<import> <name_of_packacge><dot><asterisk><semicolon>
IDENTIFIERS
ο are tokens that represent names of variables, methods, classes, etc.
▪ examples of identifiers are: Hello, main, System, out
ο these are case-sensitive
▪ this means that the identifier: Hello is not the same as hello.
ο must begin with either a letter, an underscore “_”, or a dollar sign “$”
ο letters may be lower or upper case
ο subsequent characters may use numbers 0 to 9
ο Java keywords should NOT be used as identifiers
Coding Guidelines:
1. For names of classes, capitalize the first letter of the class name. For
names of methods and variables, the first letter of the word should start
with a small letter. For example:
ThisIsAnExampleOfClassName
thisIsAnExampleOfMethodName
2. In case of multi-word identifiers, use capital letters to indicate the start of
the word except the first word. For example, charArray, fileNumber,
ClassName.
3. Avoid using underscores at the start of the identifier such as _read or
_write
KEYWORDS
ο these are predefined identifiers reserved by Java for a specific purpose
and they are always spelled in lowercase letters
ο these are reserved words that has its own special meaning in the Java
programming language, and that meaning doesn’t change from one
program to another
ο it cannot be use as names for your variables, classes, methods …etc.
SEPARATORS
ο it refer to punctuation characters and paired-delimiters that divide the
source code into fragments in order to define the structure of a Java
program
JAVA LITERALS
ο these are tokens that do not change or are constant
• Integer Literals
o come in different formats: decimal (base 10), hexadecimal
(base 16), and octal (base 8).
o For decimal numbers, we have no special notations. We just
write a decimal number as it is.
o For hexadecimal numbers, it should be preceded by “0x” or
“0X”.
o For octals, they are preceeded by “0”
12 = 0xC = 014
• Floating-Point Literals
o represent decimals with fractional parts
▪ an example is 3.1415
o can be expressed in standard or scientific notations
▪ for example
• 583.45 is in standard notation
• while 5.8345e2 is in scientific notation
• Boolean Literals
o have only two values, true or false
• Character Literals
o represent single Unicode characters
JAVA COMMENTS
ο is a special section of text inside a program whose purpose is to help people
understand the program
ο it is not part of the program itself, but used for documentation purposes
and does not affect the flow of the program
ο is indicated by the delimiters
/*
* This is an example of a
* multiline comments
*/
/**
This is an example of special Javadoc comment.
@author: Vilchor G. Perdido
@date: June 12, 2011
@version 1.0
*/
VARIABLES
ο it is sometimes called as a placeholder which is an item of data used to
store state of objects
ο is a named memory location that can be assigned a value
o the value of a variable can be changed during the execution of a
program meaning the content of a variable is changeable, not fixed
ο three kinds of variables in Java
▪ Instance variables
o are used to define attributes or the state for a particular object
o e.g. Button myButton = new Button();
myButton.setEnabled(true);
▪ Class variables
o are similar to instance variables, except their values apply to all
that class's instances (and to the class itself) rather than having
different values for each object
o e.g. StudentRecord student = new StudentRecord()
▪ Local variables
o are declared and used inside method definitions, for example,
for index counters in loops, as temporary variables, or to hold
values that you need only inside the method definition itself
o e.g. double radius = 3.25;
▪ Note: Values enclosed in < > are required values, while those values
enclosed in [ ] are optional.
int aNumber = 5;
char letter = ‘A’;
For example, suppose we have two variables with data types int
and String.
ο examples:
final double PI = 3.1416;
final double SALES_TAX_RATE = 4.5;
final int PASSING_GRADE = 75;
JAVA OPERATORS
Java provides a rich set of operators to manipulate variables. There are
different types of operators. There are arithmetic operators, relational
operators, logical operators and conditional operators. These operators follow
a certain kind of precedence so that the compiler will know which operator to
evaluate first in case multiple operators are used in one statement.
a. Arithmetic Operators
– are used in mathematical expressions in the same way that they are used
in algebra
• When the increment and decrement operators are placed after the
operand, the old value of the variable will be used in the expression
where it appears. For example:
int i = 10,
int j = 3;
int k = 0;
k = j++ + i; //will result to k = 3+10 = 13
b. Relational Operators
– compare two values and determines the relationship between those
values
– the output of evaluation are the Boolean values true or false
c. Logical Operators
– have one or two boolean operands that yield a boolean result
– There are six logical operators: && (logical AND), & (boolean logical AND),
|| (logical OR), |(boolean logical inclusive OR), ^ (boolean logical
exclusive OR), and ! (logical NOT).
d. Conditional Operator
– the conditional operator (?:) is a ternary operator
o this means that it takes in three arguments that together form a
conditional expression
– the structure of an expression using a conditional operator is,
exp1 ? exp2:exp3
//print status
System.out.println(status);
}
}
Operator Precedence
o it defines the compiler’s order of evaluation of operators so as to
come up with an unambiguous result
TYPE CASTING
ο it is the process of converting data from one data type to another
ο it may be implicit or explicit
Implicit Casting
⎯ it is automatically performed by Java when no possible loss of data
may happen during conversion process
⎯ usually occurs when a smaller size data type is converted to a bigger
size data type
⎯ example:
byte a = 50; int b = a;
Explicit Casting
⎯ it is required if some data may be lost during the conversion process
⎯ usually occurs when a bigger size data type is converted to a smaller
size data type
⎯ syntax:
<(data type)> <expression>
• where: data type – is the name of data type you are converting
to (destination data type)
expression – is the value that is cast into the destination type
• example:
long a = 5000000;
int b = (int) a;
EXPRESSIONS
ο it is a combination of variables, operators, literals, and method
invocations that returns a value
ο used to assign values to variables, perform calculations, or control the
flow of execution (e.g. compare values)
STATEMENTS
ο these are any code segment that is terminated by a semicolon
ο it can be an expression statement, declaration statement or control
flow statement, which usually appears in branching and loop control
structures
ο example:
BLOCKS
ο it is one or more statements enclosed by a pair of braces { } that
groups the sequence of statements as one entity
ο an example of a block is the body of a class
import java.util.Scanner;