Chapter 4-Java Language Fundamentals
Chapter 4-Java Language Fundamentals
2
Elements of Java Program
3
Cont…
4
Cont…
File Naming
5
Cont…
Comments
• Are used to provide notes and
information about the
implementation of the program
• Comments could be a Single
Line (//), Multi-Line (/*
terminated by */) or Javadoc
comments (/** terminated by */)
6
Cont…
Single line
Multi-line
7
Cont…
Javadoc Comments
• Used for automatically generating
HTML documentation for your
program
• Used to add more information to
comments
8
Cont…
Statements
• One or more lines of code
terminated by a semicolon
9
Cont…
Blocks
• One or more statements
bounded by an opening and
closing curly braces
10
Cont…
Coding Conventions
• Curly brace are placed after
every class or method
declaration or where it begins
11
Cont…
• Indent the statements after the block
for better readability
• Four spaces is the recommended unit
of indentation
12
Identifiers
13
Cont…
VALID INVALID
IDENTIFIER IDENTIFIER
Name 12345
_name 5name
$name Long
*this is a keyword
14
Cont… Keywords
Literals
• Represents fixed values
• Examples are String, Integer, Floating Point,
Boolean and Character
16
Cont…
Character Sequence
\n New line
\b Backspace
\t Tab
\r Return
\\ Backslash
\’ Single quote
\” Double quote
17
Cont… Data Types
DATA Byte Range
TYPE Used
boolean 1
char 2
byte 1 -128 to 127
int 4 -2,147,483,648 to
2,147,483,647
long 8 -9,223,372,036,854,775,808
to 9,223,372,036,854,775,807
float 4 ±3.40282347 x 1038 to
±1.40239847 x 10-45
double 8 ±1.76769313486231570 x
10308 to
±4.94065645841246544 x 1018
-
324
Cont…
Variables
• Serves as container that holds values
• To declare variables, it must have a
name and type
19
Cont… Declaring Variables
• First letter of variables should be
lowercase
• Succeeding should start with
capital letters
• Use description names for
variables
• Initialize variable as you declare
• Declare variables one line at a
time
20
Cont…
Casting Primitive Types
• Casting enables you to convert
one data type to another
Types of Casting
• Implicit casting
• Explicit casting
21
Cont…
Implicit casting
Explicit casting
22
Cont…
Outputting to the Screen
23
Operators
Types of Operators
• Arithmetic
• Relational
• Logical
• Conditional
• Assignment
• Increment and Decrement Operators
24
Cont…
Arithmetic
Operation Arithmetic
Operator
Addition +
Subtraction -
Multiplication *
Division (float) /
Modulus %
25
Cont…
Relational
Java Operator Description
== x is equal to y
!= x is not equal to y
> x is greater than y
< x is less than y
>= x is greater than or
equal to y
<= x is less than or
equal to y
26
Cont… Logical
Java Operator Description
AND Operator – returns True if
&& both condition is True otherwise
False
OR Operator – returns True if one
|| of the condition is True otherwise
False
NOT Operator – returns True if
! condition is False otherwise False
27
Cont… Conditional (?:)
28
Cont…
Assignment
Java
Example Description
Operator
+= x += y x=x+y
-= x -= y x=x-y
*= x *= y x=x*y
/= x /= y x=x/y
%= x %= y x=x%y
= x=y x=y
29
Cont…
Increment and Decrement
Java
Example Description
Operator
++ x++ Use the value then
increment
++x Increment first
before using the
value
-- x-- Use the value then
decrement
--x Decrement first
before using the
value
30