ADVANCED PROGRAMMING
JAVA BASICS
A Simple Java Program
public class FirstSample{
public static void main(String[] args)
{
System.out.println("Hello, World!");
}
}
Java is case sensitive
The keyword public is called an access modifier
The keyword class is there to remind you that everything in a Java program must
be inside a class
The main method in the source file is necessary in order to execute the program
The System.out.println(..) is used to invoke method println of an object
System.out
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013
2/56
Comments
System.out.println("We will not use 'Hello world!'");
// is this too cute?
/*
This is the first sample program
Copyright (C) by Cay Horstmann and Gary Cornell
*/
public class FirstSample {
public static void main(String[] args) {
System.out.println("We will not use 'Hello, World!'");
}
}
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013
3/56
Compiling and executing the program
The java compiler creates a file called 'First.class' that contains the byte codes
To actually run the program, a java interpreter called java is required to execute the code.
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013
4
4/56
Passing Command Line Arguments
class CommLineArg {
public static void main (String [] pargs) {
System.out.println("These are the arguments passed to the main method.");
System.out.println(pargs [0]);
System.out.println(pargs [1]);
System.out.println(pargs [2]);
}
}
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013
5
5/56
Passing Command Line Arguments
Output
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013
6
6/56
Identifiers
Identifiers are:
Text strings that represent variables, methods, classes or labels
Case-sensitive
Characters can be digit, letter, '$' or '_'
Identifiers cannot:
Begin with a digit
Be the same as a reserved word.
An_Identifier
a_2nd_Identifier
Go2
$10
An-Identifier
2nd_Identifier
goto
10$
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013
7
7/56
Legal Identifiers
Identifiers must start with a letter, a currency character ($), or a connecting
character such as the underscore ( _ ). Identifiers cannot start with a
number!
After the first character, identifiers can contain any combination of letters,
currency characters, connecting characters, or numbers.
In practice, there is no limit to the number of characters an identifier can contain.
You can't use a Java keyword as an identifier
Identifiers in Java are case-sensitive; foo and FOO are two different identifiers.
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013
8/56
Complete List of Java Keywords
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013
9/56
Java Code Conventions
Classes: the names should typically be nouns
Dog
Account
PrintWriter
Interfaces: the names should be adjectives
Runnable
Serializable
Methods: the names should be verb-noun pairs.
getBalance
doCalculation
setCustomerName
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013
10/56
Java Code Conventions
Variables: Like methods, starting with a lowercase letter. Sun recommends short,
meaningful names, which sounds good to us. Some examples:
buttonWidth
accountBalance
myString
Constants: Java constants are created by marking variables static and final.
They should be named using uppercase letters with underscore characters as
separators:
MIN_HEIGHT
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013
11/56
The Elements of a class
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013
12/56
Data Types
Java is a strongly typed language. This means that every variable
must have a declared type. There are eight primitive types in Java
Four of them are integer types; two are floating-point number types; one
is the character type char, used for characters in the Unicode encoding,
and one is a boolean type for truth values.
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013
13/56
Variables and Data Types (cont.)
Data types
Primitive types
Reference types
The primitive types are boolean, byte, char, short, int, long, float and double
All non-primitive types are reference types, so classes, which specify the types of
objects, are reference types
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013
14/56
Primitives: Integers
Signed whole numbers
Initialized to zero
Categories:
Categories:
Size:
Size:
a.
a. integer
integer
b.
b. floating
floating
1.
1. byte
byte
2.
2. short
short
Range:
Range:
Size:
Size:
Range:
Range:
11 byte
byte
77
77
-2
-2 22 -- 11
22 bytes
bytes
15
15
15
15
-2
-2 22 -- 11
c.
c. character
character
3.
3. int
int
d.
d. boolean
boolean
Size:
Size:
Range:
Range:
4.
4. long
long
Size:
Size:
Range:
Range:
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013
44 bytes
bytes
31
31
31
31
-2
-2 22 -- 11
88 bytes
bytes
63
63
63
63
-2
-2 22 -- 11
15
15/56
Primitives: Floating Points
"General" numbers
Can have fractional parts
Initialized to zero
Categories:
Categories:
a.
a. integer
integer
b.
b. floating
floating
1.
1. float
float
Size:
Size: 44 bytes
bytes
-45
38
-45
38
Range:
Range: ±1.4
±1.4 xx 10
10 ±3.4
±3.4 xx 10
10
c.
c. character
character
d.
d. boolean
boolean
2.
2. double
double
Size:
Size: 88 bytes
bytes
-324
-324 ±1.8 x 10308
308
Range:
Range: ±4.9
±4.9 xx 10
10
±1.8 x 10
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013
16
16/56
Primitives: Characters
Char is any unsigned Unicode character
Initialized to zero (\u0000)
Categories:
Categories:
a.
a. integer
integer
b.
b. floating
floating
char
char
c.
c. character
character
Size:
Size: 22 bytes
bytes
Range:
Range: \u0000
\u0000 \uFFFF
\uFFFF
d.
d. boolean
boolean
Java Basic
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013
17
17/56
Primitives: Booleans
boolean values are distinct in Java
Can only have a true or false value
An int value can NOT be used in place of a boolean
Initialized to false
Categories:
Categories:
a.
a. integer
integer
b.
b. floating
floating
c.
c. character
character
d.
d. boolean
boolean
boolean
boolean
Size:
Size: 11 byte
byte
Range:
Range: true
true || false
false
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013
18
18/56
Primitive Data Types (summary)
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013
19/56
The Sign bit for a byte
All six number types in Java are signed
Integer literals three way store present integer numbers in the Java language:
decimal(base10), octal(base8), hexadecimal(base16)
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013
20/56
Variables and Data Types (cont.)
Decimal Literals: default
int length = 343;
Octal Literals: represent an integer in octal form by placing a zero in front of the
number
int nine = 011;
Hexadecimal Literals: including the prefix 0x or the optional suffix extension L
int z = 0xDeadCafe;
Note: Java will accept capital or lowercase letters for the extra digits in
hexadecimal
0XCAFE and 0xcafe are both legal
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013
21/56
Variables and Data Types (cont.)
Floating-Point Literals:
Default are defined as double (64 bits)
Attach the suffix F or f to the number if want using floating-point (32
bits)
Example:
float f = 23.467890; // Error
float g = 49837849.029847F;
Boolean Literals
true, false
Character Literals: 16-bit unsigned integer
char letterN = '\u004E'; // The letter 'N‘
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013
22/56
Variables and Data Types (cont.)
Some special characters
\n: Used to denote new line
\r: Used to denote a return
\t: Used to denote a tab
\b: Used to denote a backspace
\f: Used to denote a form feed
\': Used to denote a single quote
\": Used to denote a double quote
\\: Used to denote a backslash
Literal Values for Strings: A string literal is a source code representation of a value
of a String object
String s = "Bill Joy";
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013
23/56
Casting Primitive Types
Casting creates a new value and allows it to be treated as a different type
than its source
Java is a strictly typed language
Assigning the wrong type of value to a variable could result in a compile
error or a JVM exception
The JVM can implicitly promote from a narrower type to a wider type
To change to a narrower type, you must cast explicitly
double
double f;
f;
int
int a,
a, b;
b;
int
int d;
d;
long
long g;
g;
short
short c;
c;
short
short e;
e;
ff == g;
g;
aa == bb ++ c;
c;
ee == (short)d;
(short)d;
gg == f;
f; //error
//error
Java Basic
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013
24
24/56
Implicit vs. Explicit Casting
Implicit casting is automatic when no loss of information is possible.
byte → short → int → long → float → double
An explicit cast required when there is a "potential" loss of accuracy:
long p = (long) 12345.56;
// p == 12345
int g = p; // illegal even though an int
// can hold 12345
char c = 't';
int j = c;
// automatic promotion
short k = c;
// why is this an error?
short k = (short) c;
float f = 12.35;
// explicit cast
// what’s wrong with this?
Java Basic
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013
25
25/56