Вы находитесь на странице: 1из 9

Chapter 3: Identify it....identifiers, keywords and Java data types.

(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.
/*
*/

Multi Line Comment

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.
/**

Java Documentation Comment

*/
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

Prepared by: Lawrence G. Decamora III, scjp

10
11
12
13
14
15
16
17
18

public void doSomething()


{
int x;

if (true)
{
int x;
}

// compilation error, duplicate variable

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

public void doSomething()


{
// int x;

if (true)
{
int x;
}
int x;

// compilation ok!!, no duplicate variable

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

Prepared by: Lawrence G. Decamora III, scjp

int, 123, ---, gross-Pay, over time pay


Java Keywords:
abstract

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

Use one statement per line


Use two to four spaces for indentions
Use comments to document your codes

Prepared by: Lawrence G. Decamora III, scjp

Two (2) Special keywords.


const and goto are considered as special keywords. These are Java keywords, but were not allowed
to use them. There are no implementations of these keywords in the Java Programming Language but the Java
creators decided to consider them as reserve words so that in the future releases of Java or other similar
technologies, const and goto will not be used anymore.
In J2SE 1.4, there were only 49 keywords. In J2SE 5.0, Java SE 6.0 and Java SE 7 there are now 50
keywords with the addition of the enum keyword.
Two (2) Java Data Types
Primitive Types and Reference Types.
There are eight (8) Java Primitive Data Types. If there are data types outside of the eight, they are
considered as Reference Types.
1. Primitive Data Types
1. Integral contains whole number values, may have both negative and positive values
including zero (0).
1. byte an 8 bit data type, legal values may range between -27 to +27 - 1
2. short a 16 bit data type, legal values may range between -215 to +215 - 1
3. int a 32 bit data type, legal values may range between -231 to +231 - 1
4. long a 64 bit data type, legal values may range between -263 to +263 1
The default number format is base 10 (decimal). For other number format, the following syntax
must be followed:
Base 10
Base 8
Base 16
Base 2

Decimal
Octal
Hexadecimal
Binary

-9,876, 8,762, 0
077, -012
0xABCD, -0x1234
0Bx0100_1110_0101_0011

New features in Java SE 7:


the underscore (_) can now be used as a part of the literal value as of Java SE 7.
the binary (base 2) format was just introduced in Java SE 7.
2. Floating Points
1. float 32 bits
2. double 64 bits
3. Textual
1. char 16 bits
4. Logical
1. boolean
2. Reference Types Beyond all primitive types are reference types. A reference variable
contains the address of the object.
Example:
public class Person {
public String name = noname;
public int age = 0;
public Person(String name, int age) {
Page 4 of 9

Prepared by: Lawrence G. Decamora III, scjp

this.name = name;
this.age = age;
}

public String toString() {


return name + " at age "+ age;
}

public class TestPerson {


public static void main(String args[]) {
Person p1 = new Person("Jose", 20);
}
}
This what really happens:
Person p1 = new Person("Jose", 20);
p1 -->

?????

Person p1 = new Person("Jose", 20);


p1 -->

?????

name -->

null

age -->

Person p1 = new Person("Jose", 20);


p1 -->

?????

name -->

noname

age -->

Person p1 = new Person("Jose", 20);


p1 -->

?????

name -->

Jose

age -->

20

Person p1 = new Person("Jose", 20);

Page 5 of 9

Prepared by: Lawrence G. Decamora III, scjp

p1 -->

0x12345678

name -->

Jose

age -->

20

Assigning References to Variables


In Lines 1 and 2, both x and y are primitive values. It means that their actual values on the stack memory
is 10. While in Line 3, you've created a new Person object having Jose as the value of its name and 20 for
its age. Since p1 is a reference variable, the actual value of p1 is the address where the object resides.
1
2
3
4

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

p2 = new Person(Pedro, 30);


p1

0x1234567

-------->

Jose

20

p2

0x2345678

-------->

Pedro

30

In a single Virtual Machine, everything in Java is Pass by Value.


In a single Virtual Machine, we can pass values to a method, and those values are called arguments.
Arguments can be the actual value of a variable or they can be the address of an object. Here's a sample code
that will demonstrate the Pass-by-Value rule.
1
2
3
4
5
6
7
8
9
10
11
Page 6 of 9

public class PassTest {


// Methods to change the current values
public static void changeInt(int value) {
value = 55;
}
public static void changeObjectRef(Person ref) {
ref = new Person(Pedro, 30);
}

Prepared by: Lawrence G. Decamora III, scjp

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

public static void changeObjectAttr(Person ref) {


ref.setAge(30);
}
public static void main(String args[]) {
Person per;
int val;
// Assign the int
val = 11;
// Try to change it
changeInt(val);
// What is the current value?
System.out.println("Int value is: " + val);
// Assign the person
per = new Person(Jose, 20);
// Try to change it
changeObjectRef(per);
// What is the current value?
System.out.println("Person: " + per);

// Now change the day attribute


// through the object reference
changeObjectAttr(per);
// What is the current value?
System.out.println("Person: " + per);

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

Prepared by: Lawrence G. Decamora III, scjp

The this Reference.


The keyword this can be used to:
1. Resolve ambiguity between instance variables and parameters.
2. Pass the current object as a parameter to a method, or to another constructor.
Consider the given sample code:
1
2
3
4
5
6
7
8
9
10
11
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
41
42

public class Person


{
private String name = "noname";
private int age = 0;

1
2
3
4

public class TestPerson


{
public static void main(String args[])
{

Page 8 of 9

public Person(String name, int age)


{
this.name = name;
this.age = age;
}
public Person(Person per)
{
this.name = per.name;
this.age = per.age;
}
public void setName(String name)
{
this.name = name;
}
public void setAge(int age)
{
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public Person addAge(int moreAge)
{
Person newPerson = new Person(this);
newPerson.age = newPerson.age + moreAge;
return newPerson;
}
public String toString()
{
return name + " at age "+ age;
}
}

Prepared by: Lawrence G. Decamora III, scjp

5
6
7
8
9

Person p1 = new Person("Jose", 20);


Person olderPerson = p1.addAge(10);
System.out.println(olderPerson);

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

Prepared by: Lawrence G. Decamora III, scjp

Вам также может понравиться