Java Programming Fundamentals: By: Jayfee D. Ramos
Java Programming Fundamentals: By: Jayfee D. Ramos
Programming
Fundamentals
System.out.println(“Hello world”);
A block is one or more statements bounded by an
opening and closing curly braces that groups the
statements as one unit. Block statements can be nested
indefinitely. Any amount of white space is allowed. An
example of a block is,
public static void main( String[] args ){
System.out.println("Hello");
System.out.println("world");
}
Java Identifiers
Your Subtitle
• Identifiers are tokens that represent names of variables,
methods, classes, etc.
– Examples of identifiers are: Hello, main, System, out.
• Java identifiers are case-sensitive. This means that the
identifier: Hello is not the same as hello. Identifiers
must begin with either a letter, an underscore “_”, or a
dollar sign “$”. Letters may be lower or upper case.
• Identifiers cannot use Java keywords like class, public,
void, etc.
All Java components require names. Names used for
classes, variables and methods are called identifiers.
In Java, there are several points to remember about
identifiers. They are as follows:
All identifiers should begin with a letter (A to Z or a to z),
currency character ($) or an underscore (_).
After the first character identifiers can have any
combination of characters.
A key word cannot be used as an identifier.
Most importantly identifiers are case sensitive.
Examples of legal identifiers: age, $salary, _value, __1_value
Examples of illegal identifiers: 123abc, -salary
Java Keywords
are predefined identifiers reserved by Java for a
specific purpose. You cannot use keywords as names
for your variables, classes, methods …etc.
Here is a list of the Java Keywords.
BASIC DATA TYPES
Your Subtitle
Variables are nothing but reserved memory locations to
store values.
This means that when you create a variable you reserve
some space in memory. Based on the data type of a variable,
the operating system allocates memory and decides what
can be stored in the reserved memory. Therefore, by
assigning different data types to variables, you can store
integers, decimals, or characters in these variables.
System.out.print("Hello ");
System.out.print("world!");
class Example1 {
public static void main(String args[]) {
int var1; // this declares a variable
int var2; // this declares another variable
var1 = 1024; // this assigns 1024 to var1
System.out.println("var1 contains " + var1);
var2 = var1 / 2;
System.out.print("var2 contains var1 / 2: ");
System.out.println(var2);
}
}
Example Program
class Example2 {
public static void main(String args[]) {
int iresult, irem;
double dresult, drem;
iresult = 10 / 3;
irem = 10 % 3;
dresult = 10.0 / 3.0;
drem = 10.0 % 3.0;
System.out.println("Result and remainder of 10 / 3: " + iresult + " " + irem);
System.out.println("Result and remainder of 10.0 / 3.0: " + dresult + " " +
drem); }
}
Example Program
class Example3{
public static void main(String args[]) {
int var; // this declares an int variable
double x; // this declares a floating-point variable
var = 10; // assign var the value 10
x = 10.0; // assign x the value 10.0
System.out.println("Original value of var: " + var);
System.out.println("Original value of x: " + x);
System.out.println(); // print a blank line
// now, divide both by 4
var = var / 4;
x = x / 4;
System.out.println("var after division: " + var);
System.out.println("x after division: " + x);
}
}
Example Program
public class Example4{
public static void main(String args[]) {
int age = 0;
age = age + 7;
System.out.println("Puppy age is : " + age);
}
}
Group Activity: SOLVE – GROUP –SHARE
Direction:
1. The class will be divided into groups
2. Each group shall choose a leader, a writer, a reporter
3. Answer the following exercises individually and answer the following
question as a group.
- How did you solve this exercises?
- What are the error occurred during solving this exercises?
4. You will be given ___ to do this exercises and five minutes will be given to
each group to report.
Exercise
1. Declaring and printing variables
Given the table below, declare the following variables with the corresponding
data types and initialization values. Output to the screen the variable names
together with the values.