1
JAVA LANGUAGE FUNDAMENTALS
Prepared by: Ms. Jullie Ann M. Ocampo
JAVA LANGUAGE FUNADAMENTALS
▪ Elements of a Java Program
▪ Identifiers
▪ Literals and Data Types
▪ Operators
2
Elements of Java Program
Line 1, all java code should be in a class
declaration
“{“ Indicates the start of the code block
Line 2, main starting point of a java console
program
3
Cont…
Line 3, System.out.println is used to
display text on the screen
Line 4, “}“ Indicates the closing of the
code block for main
Line 5, “}” indicates the closing of the
code block for class
4
Cont…
File Naming
▪ Java programs should always have a
“.java” extension
▪ Filenames should always be the same
as the class name where the main
method exist.
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
• Used to name variables, methods, classes or
labels
• Are case-sensitive
• May start with “_” or “$”
• May contains digits (0-9)
• Cannot be the same as the keywords
13
Cont…
VALID INVALID
IDENTIFIER IDENTIFIER
Name 12345
firstName First name
_name 5name
$name Long
*this is a keyword
14
Cont… Keywords
abstract do implements package throw
boolean double import private throws
break else inner protected transient
byte extends instanceof public try
case final int rest var
cast finally interface return void
catch float long short volatile
char for native static while
class future new sure
const generic null switch
continue goto operator synchronized
default if outer this
15
Literals and Data Types
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
short 2 -32,768 to 32,767
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
Exclusive OR Operator – returns
^ True if one of cond1 and cond2 is
true but not both
27
Cont… Conditional (?:)
Condition ? <value if true> : <value
if false>
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