You are on page 1of 21

BASIC ELEMENTS OF

JAVA
BASICS OF A JAVA PROGRAM

• Java program: collection of classes


• There is a main method in every Java
application program
LITERALS
• LITERALS MAYBE THOUGHT OF AS CONSTANTS OR FIXED VALUES.
THEY COULD EITHER BE NUMERIC, STRING OR CHARACTER IN
GENERAL.
a. NUMERIC – literals that are of numeric type. Only the following
symbols are allowed: { 0, 1, 2, 3…….9, +, -, .}
b. STRING – literals that are enclosed in double quotation marks (“ “)
“Hello” “Green World”
c. CHARACTER – a single character enclosed in a single quotation
marks (‘’) ‘a’ ‘b’ ‘c’
IDENTIFIERS
• IDENTIFIERS are names given to a variables and literals. Literals are
constants or fixed values while variable are entity that can hold those
literals.

Suppose we have:
y = 10 , 10 is the LITERAL while y is the VARIABLE holding the literal
value of 10.
RULES WHEN NAMING VARIABLE
1. A variable name may start with a letter, and underscore (_) or a
dollar sign.
2. It must not be a keyword.
3. Subsequent characters may be letters, numbers, underscore or
dollar sign.
4. In following Java’s coding guidelines, the first letter of a variable
name should be in lowercase. The first letter of the subsequent
words should be capitalized
5. You can use Camel case - nameOfStudent
EXPRESSIONS
• EXPRESSIONS – is a group of elements with operators that determine
how they may be evaluated. An expression could either be
ARITHMETIC or a BOOLEAN expression.

ARITHMETIC EXPRESSIONS
( +, - , *, / , %, ++ , --)
Modulo
17%7=3
10%4=2 23%9=5
ORDER OF PRECEDENCE
* / % (same precedence)
+ - (same precedence)

• Operators in 1 have a higher precedence than operators in


2
• When operators have the same level of precedence,
operations are performed from left to right
6*3*2/2 *(2+4)=
• BOOLEAN EXPRESSIONS consists of a relational expression or
relational expressions with logical operators. A relational expression
consists of operands and relational operators.
Example:
A Boolean expression with a relational expression

rate >= 100

The operands are the variable rate and 100 with the relational
operator of >=. If the value of rate is 150 then the expression yields a
TRUE value.
• RELATIONAL OPERATORS ( >, >=, <, <=, ==, !=)
• LOGICAL OPERATORS (&&, ||, !) AND,OR,NOT

EXPRESSION 1 EXPRESSION 2 RESULT

True True True

&& AND True False False

False True False

False False False


EXPRESSION 1 EXPRESSION 2 RESULT

True True True

True False True


|| OR
False True True

False False False

EXPRESSION RESULT
True False
! NOT False True
Data Types
• Data type: set of values together with a set of operations

11
Primitive Data Types
Integral Data Types
Values and Memory Allocation for Integral Data Types
STATEMENTS
• STATEMENTS are keywords that performed a predefined task. A
statement in programming is a collection of keywords and elements
that expresses a complete instruction.
• A statement ends with a semi-colon;
Generally, statements in basic programming include:
1. VARIABLE DECLARATION Ex. int num;, int square;
2. OUTPUT Ex. System.out.println(“The square is:” + square);
3. ASSIGNMENT/PROCESS Ex. Square = num * num;
4. INPUT Ex. Scanner s = newScanner (System.in);
num = s.nextInt();
UNDERSTANDING THE JAVA PROGRAM
STRUCTURE
• Java program structure also referred to as application is made up of
one or more classes. These classes may contain variables, methods
and comments.
public Class Hello
{ ….
}
This is a class declaration. The keyword PUBLIC is called a
MODIFIER. It indicates that our class declaration is accessible to other
classes.
The keyword CLASS is required whenever we create classes. The
identifier HELLO indicates the name of the class
The symbols {} indicate the START and END of the CLASS BLOCK. The
symbol may be placed on the same or next line.
String name = “RAQUEL SALAZAR”;

This is an attribute variable declaration and initialization. The


STRING is one of the variable types indicating the variable name can
only contain string values.
The string constant “RAQUEL SALAZAR” is used to initialize the
variable NAME.
// the following message displays RAQUEL SALAZAR

This is a COMMENT. Comments are used to clarify what the code


does. These are useful when other programmers would have to read or
update your code.
Three (3) types of comments style are used:
1. // - Single line Comment
2. /* */ - Multiple Line Comment
Ex. /* the following message
display RAQUEL SALAZAR
*/
3. /** */ - Documentation Comment. This is used to create
documentation for our classes using javadoc tool.
public static void main (String [] args)
{…
}
This is a method declaration named MAIN. The MAIN() METHOD is where the
program execution starts in java applications.
The keyword PUBLIC and STATIC are required. The main method has a return
type VOID as it does not return any value.
The STRING [] ARGS indicates that the main method can accept zero or more
string arguments.
The symbol {} indicates the START and END of the MAIN BLOCK.

Other METHODS can be declared within a CLASS.


Only CLASSES intended for program execution should contain the main method.
System.out.println (“Hello “ + name);

System.out.println is a method that displays the string constant and


variable values.

You might also like