Академический Документы
Профессиональный Документы
Культура Документы
(3 hrs)
Chapter Objectives:
List the three (3) types of Java Comments and describe when to use each one.
List the fifty (50) Java Keywords.
List the eight (8) Primitive Data Types
Distinguish the difference between primitive data types and reference types
Recall the process of constructing and initializing objects
Show how to pass references of an object by using Pass-by-Value
Assess the use of the this keyword.
Compare the Java Programming Coding and Naming Conventions with other Languages.
Three (3) types of Java Comments
// Singe Line Comment
The Single comment is used to comment out a single-line of code.
/*
*/
The multi line comment is used to comment out multiple lines or a large chunk of code. Lets say you
have a long for loop that you would like to delete for testing purposes. You do not delete it immediately. You just
surround it with comments first, so that when you want your loop back you simply remove the comments
surrounding it.
/**
*/
The JavaDoc comment is used to put descriptions on what the structure is all about. It is normally placed
on top of a class declaration, an attribute declaration or a method declaration so as to provide a description on
what the said construct do. After coding the application and putting the proper javadoc comments, you can now
create a HTML file that is very similar to the Java API Documentation.
Semicolons, Blocks and White Spaces
A Semicolon is used by Java as a language signal that it is already an end of a statement. Deleting a
semicolon will tell the Java compiler that next line of your code is a continuation of the current line.
int = a + b
+ c + d;
is the same as
int = a + b + c + d;
A Block or also known as a scope is used to group related statements together. We use curly braces to
represent a block ({ }). Curly braces are normally used on classes, methods and control structures like if-else,
for loop and the likes.
Having a block also signifies the scope of the variable. Given the two program snipplet.
Page 1 of 9
10
11
12
13
14
15
16
17
18
if (true)
{
int x;
}
the given code snipplet will have a compilation error on line 16 for having a duplicate declaration of the
variable x that was declared in line 12. But we we rewrite it this way:
10
11
12
13
14
15
16
17
18
19
if (true)
{
int x;
}
int x;
the compilation on this sample code snipplet will be ok. The first variable x declared in line 16 will only
have a life span within the if curly braces, between lines 14 to 17, and it will be destroyed after line 17. So the
variable x declared in line 18 will be fine as if it is a new variable x was declared.
White spaces actually refers to spaces, tabs, and enter keys (or next line characters). Java ignores
theses white spaces, which means any amount of white spaces in Java is allowed.
for (int i=0;i < 3;i++){System.out.println(i);}
is the same as
for (int i = 0; i < 3; i++)
{
System.out.println(i);
}
We use white spaces to make our codes more readable.
Java Identifiers
Identifiers are used to identify something. We use names or identifiers to refer to variables, methods,
classes, constant values and the likes.
Identifiers do not have any maximum length and are case sensitive. You must not use Java keywords as
identifiers. Your identifier must either start with any Unicode letter, an underscore or a dollar symbol.
Here are valid examples identifiers:
Int, $$$, ___, basic_pay, OverTime, getTaxPayer
Here are examples of invalid identifiers:
Page 2 of 9
continue
for
new
switch
assert
default
goto
package
synchronized
boolean
do
if
private
this
break
double
implements
protected
throw
byte
else
import
public
throws
case
enum
instanceof
return
transient
catch
extends
int
short
try
char
final
interface
static
void
class
finally
long
strictfp
volatile
const
float
native
super
while
null, true and false are NOT Java keywords. These are literal values.
Literal values are possible values of a variable. null is a possible value of a reference data type, while
true and false are possible values of a boolean data type.
Java Programming and Language Conventions
Packages
com.myapp.domain;
Classes, Interfaces and enum Types
TestPerson
Methods
getName()
Variables
name
Constants
SALES_VAT
Control Structures
for (int i = 0; i < 5; i++)
// do something
}
Spacing
Comments
Page 3 of 9
Decimal
Octal
Hexadecimal
Binary
-9,876, 8,762, 0
077, -012
0xABCD, -0x1234
0Bx0100_1110_0101_0011
this.name = name;
this.age = age;
}
?????
?????
name -->
null
age -->
?????
name -->
noname
age -->
?????
name -->
Jose
age -->
20
Page 5 of 9
p1 -->
0x12345678
name -->
Jose
age -->
20
int x = 10;
int y = x;
Person p1 = new Person(Jose, 20);
Person p2 = p1;
x
10
10
p1
0x1234567
p2
0x1234567
Jose
20
While in line 4, when you say Person p2 = p1, what your actually doing is your assigning the value of p1
to p2. In this case, its the address of p1 that is assigned to p2. So there are two variables pointing to the same
objects.
5
0x1234567
-------->
Jose
20
p2
0x2345678
-------->
Pedro
30
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
If this code will be simulated, we always start simulation in the main method. In lines 17 and 18 we
declared two variables, a primitive variable val and a reference variable per. If we will recall, reference
variables contains references to an object, which means it contains the memory address where the object
resides.
In line 21, we initialized val to 11 and passed the value of val to the method changeInt(int). Since
Java uses pass-by-value, the actual value that your passing to the changeInt(int) method is 11 (the actual
value). So when you print the output in line 25, the printed output will be:
Int value is: 11
In line 28, we've created a new Person object; thus the variable per will have an address as its value.
But in line 30, we passed the value of per (that actually contains a memory address) to the method
changeObjectRef(Person) that accepts a Person object. So, in the method changeObjectRef(Person)
in line 9, we instantiated a new Person object, thus letting the argument ref point to a new object, thus having a
new memory address value to it. So, when you execute line 32, you will have the output:
Person: Jose at age 20
Lastly, in line 36, you passed the per object to the method changeObjectAttr(Person), thus
passing the memory address value of per to the said method. Inside the method
changeObjectAttr(Person) in line 13, you used the variable ref to call a member method setAge(int)
to change the age of the Person object that was passed to the changeObjectAttr(Person) method. Thus
after the method changeObjectAttr(Person) executes, the output of line 38 will be:
Person: Jose at age 30
Page 7 of 9
1
2
3
4
Page 8 of 9
5
6
7
8
9
Within our two constructors and inside the body of our setters, we saw that the parameter names are
identical to our instance variables (attributes); thus we used the keyword this to resolve this ambiguity.
Also, consider the method addAge(int), we pass the current object to our constructor in line 34.
Page 9 of 9