Professional Documents
Culture Documents
JAVA
BASICS OF A JAVA PROGRAM
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)
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 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”;