0% found this document useful (0 votes)
41 views

Basic Elements of Java: Prof. Ryan Celis

Here are the answers to the seatwork expressions: 1. True 2. False 3. True 4. True 5. True

Uploaded by

Lals Concepcion
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Basic Elements of Java: Prof. Ryan Celis

Here are the answers to the seatwork expressions: 1. True 2. False 3. True 4. True 5. True

Uploaded by

Lals Concepcion
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

Chapter I

Basic Elements of Java


PROF. RYAN CELIS
Some Terms
• program is a sequence of statements intended to
accomplish a task.
• programming is a process of planning and
creating a program.
• syntax are rules that tells which statements are
legal or accepted by the programming language
• programming language is a set of rules, symbols,
and special words used to construct a program
A Java Program
import java.io.*;
public class sample
{
public static void main(String[] args)
{
System.out.print (“Hello World!”);
}
}
Basics of a Java Program
• comments
• special symbols
• reserved words
• identifiers
• constants / literals
• data types
• operators
• expressions
Comments
• these are statements that are not executed by
the compiler
• used to provide information about the
program or its instructions

2 Types:
• Single-line Comments
• Multiple-line Comments
Single-line Comments:
// This is a single line comment.

Multiple-line Comments:
/*
This is an example of
a multiple line
comment
*/

Note: comments can be placed anywhere in your program


Special Symbols
• symbols used by Java to perform specific
functions

+ - * /
. ; ? ,
<= != == >=
Reserved Words
• words used by Java for specific purposes

int float double char


void public static return
Identifiers
• names of things such as variables, constants,
methods, functions etc..

Rules in naming Identifiers:


1. alphanumeric, no special characters
2. must begin with a letter or underscore (_)
3. no spaces
4. no reserved words
IDENTIFIERS
VALID INVALID
first emp salary
conversion Hello!
payRate rate/hr
studNum 2ndPlace
_course System

Note: Identifiers in Java are case sensitive


Practice…
Is the given identifier VALID or INVALID?
1. stud_num
2. courseMajor
3. _fname
4. last name
5. public
6. subj/sect
7. 10thRecord
8. class
9. ex4mp!3
10. CCP111
Data Types
• a set of values with a set of operation on
those values

Primitive Data Types


– Integral
– Floating-point
– Boolean
– String
Integral
• data type that deals with integers, or numbers
without a decimal part
DATA TYPE VALUES STORAGE (in Bytes)
byte -128 to 127 1 (8 bits)
short -32768 to 32767 2 (16 bits)
int -2147483648 to 2147483647 4 (32 bits)
long -922337203684547758808 to 8 (64 bits)
922337203684547758807
Floating Point Data Type
• a data type that deals with numbers with
decimal values

DATA TYPE VALUES STORAGE (in Bytes)


float -3.4 E+38 to 3.4 E+38 4 (32 bits)
double -1.7 E+308 to 1.7 E+308 8 (64 bits)
Boolean
• a boolean data type has only two values,
TRUE or FALSE
String
• string are sequence of characters that can
either be letter, number, or special characters
Constants / Literals
• values remains the same throughout the
execution of program
– integer literals
– floating point literals
– characteral literals
Practice…
Identify the Data Type of the given Identifiers:
1. firstName
2. age
3. pi
4. studentNumber
5. plateNumber
6. Gender (M/F)
7. State (T/F)
8. tuitionFee
9. address
10. units
Operators
• Operators are symbols used to manipulate
data

Types
1. Arithmetic
1. Binary
2. Unary
2. Assignment
3. Relational
4. Logical
Binary Arithmetic Operators
• arithmetic operators used on two operands

SYMBOL OPERATOR
+ Addition
- Subtraction
* Multiplication
/ Division
% Modular Division
Unary Arithmetic Operators
• operators used on only one operand

SYMBOL OPERATOR
++ Unary Addition
-- Unary SUbtraction
Assignment Operator
• used to assign a value
• the equal (=) sign is used
Relational Operator
• tests the relation between two operands
which produces a boolean value
SYMBOL OPERATOR
== Equality
!= Not equal
< Less Than
<= Less Than or Equal
> Greater Than
>= Greater Than or Equal
Logical Operators
• applies logical operation on boolean values
which produces a boolean value

SYMBOL OPERATOR
! not
&& and
|| or
Practice…
Identify what type of operator
1. =
2. %
3. <=
4. +
5. ++
6. ||
7. !=
8. !
9. *
10. >
Expressions
• expression is a literal value or variable or its
combination that can be evaluated to produce
a result
• operands are variables and literals contained
in an expression
• literal is a static value such as a string or a
number
• operators are symbols pertaining to the
operation to perform
data type operator
operand literal
Mixed Expressions
• an expression that has operands of different
data types evaluated by using different
operators

Example:
5 + (6 * 2.5)
(10 * 5) > (50)
(8 == (2*4)) || false
Practice…
Evaluate the following expressions:
1. (5+10) * (10%3)
2. ( 10*10) >= (5 + (5*5))
3. (10 < 15 ) || (true && false)
4. (!(5==5)) && ((5*10) > 100)
5. true && (!(true || false))
Seatwork (1/2 lengthwise)
Evaluate the following expressions:
1. ((3+3) >= (2*3)) && (T || F)
2. (T && (!T)) || ((7*7) < 50)
3. !(T&&F) || !(T||(T&&F))
4. T && ((2+3*4) >= 15))
5. !(T && F) || (T || (!T))

You might also like