Part 01 - Java Basics 1 - Variable
Part 01 - Java Basics 1 - Variable
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
/*
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
4
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013 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]);
}
}
5
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013 5/56
Passing Command Line Arguments
Output
6
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013 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
An_Identifier
a_2nd_Identifier
Go2
An-Identifier
2nd_Identifier
goto
$10 10$
7
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013 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
Categories:
Size: 1 byte
a. integer 1. byte Range: -27 27 - 1
b. floating 2. short
Size: 2 bytes
Range: -215 215 - 1
c. character
3. int Size: 4 bytes
d. boolean Range: -231 231 - 1
15
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013 15/56
Primitives: Floating Points
"General" numbers
Can have fractional parts
Initialized to zero
Categories:
a. integer
Size: 4 bytes
b. floating 1. float Range: ±1.4 x 10-45 ±3.4 x 1038
c. character
2. double Size: 8 bytes
d. boolean Range: ±4.9 x 10-324 ±1.8 x 10308
16
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013 16/56
Primitives: Characters
Categories:
a. integer
b. floating
char Size: 2 bytes
c. character Range: \u0000 \uFFFF
d. boolean
Java Basic 17
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013 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:
a. integer
b. floating
c. character
Size: 1 byte
d. boolean boolean Range: true | false
18
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013 18/56
Primitive Data Types (summary)
Keyword Description Size/Range
Integers
byte Byte-length
1 byte : –128 to 127
integer
short Short integer 2 bytes : –32 768 to 32 767
int Integer 4 bytes : –2 147 483 648 to 2 147 483 647
char A Unicode
2 bytes
character
boolean A boolean value true or false
float g = 49837849.029847F;
Boolean Literals
true, false
double f;
int a, b; int d; long g;
short c; short e; f = g;
a = b + c; e = (short)d; g = f; //error
Java Basic 24
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013 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:
x = y;
int total;
int a = 1;
for (int b = 0; b < 3; b++){
int c = 1;
for (int d = 0; d <3; d++){
if (c < 3) c++;
}
abcd
System.out.print(c);
System.out.println(b); abc
}
a = c; // ERROR! c is out of scope a
Java Basic 29
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2013 29/56
A variable's scope
public static void main(String[] args)
{
int n;
...
{
int k;
...
} // k is only defined up to here
}
Note:
only the Member variables acquire the default
//real numbers
float largestFloat = Float.MAX_VALUE;
double largestDouble =
Double.MAX_VALUE;
scores[0] = 75;
class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
//populate matrix
for (int i = 0; i < aMatrix.length; i++) {
aMatrix[i] = new int[5]; //create sub-array
for (int j = 0; j < aMatrix[i].length; j++) {
aMatrix[i][j] = i + j;
}
}
//print matrix
for (int i = 0; i < aMatrix.length; i++) {
for (int j = 0; j < aMatrix[i].length; j++) {
System.out.print(aMatrix[i][j] + " ");
}
System.out.println();
}
}
}