2. - Legal Identifiers
- Define Classes
● source file declaration rules,
● java/javac commands,
● main method,
● import statements,
● static import,
● class declaration and modifiers
- Distinguish between object references and primitives
- Declaring and initializing variables
- Understanding variable scope
OCA Java SE 8 Programmer I Exam Guide (Exams 1Z0-808)
AGENDA
3. IDENTIFIERS AND KEYWORDS
Legal identifiers
- Identifiers must start with a letter, a currency character ($), or a connecting
character such as the underscore (_). Identifiers cannot start with a digit!
- 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.
4. DEFINE CLASES
Source file declaration rules
○ Only one public class per source code file.
○ Comments can appear at the beginning or end of any line in the source code file;
○ If there is a public class in a file, the name of the file must match the name of the public
class.
○ If the class is part of a package, the package statement must be the first line in the
source code file, before any import statements that may be present.
○ If there are import statements, they must go between the package statement.
○ Import and package statements apply to all classes within a source code file.
○ A file can have more than one non-public class.
○ Files with no public classes can have a name that does not match any of the classes in
the file.
5. DEFINE CLASES
Using the java and javac commands
● Compiling with javac
The javac command is used to invoke Java’s compiler.
Usage: javac <options> <source files>
Both the [options] and the [source files] are optional parts, and both allow multiple entries.
Sample: javac -help
javac -version Jug.java Bolivia.java
● Launching applications with java
The java command is used to invoke the Java Virtual Machine (JVM).
Usage: java <options> class <args>
The [options] and [args] parts are optional, and they can both have multiple values.
Sample: java -showversion MyClass x 1
6. DEFINE CLASES
Using public static void main(String[ ] args)
main() is the method that the JVM uses to start execution of a Java program.
public static void main(String[] args)
Other versions of main() with other signatures are perfectly legal, but they’re treated as normal
methods.
The order of its modifiers can be altered a little; the String array doesn’t have to be named args; and
it can be declared using var-args syntax.
static public void main(String[] args) {}
public static void main(String... args) {}
public static void main(String[] parameters) {}
7. DEFINE CLASES
Import Statements and the Java API
Sample class: public class ArrayList {
static public void main(String... parameters) {
System.out.println("Fake ArrayList class");
}
}
The fully qualified name
java.util.ArrayList
The import format:
import java.util.ArrayList;
import java.util.*; // wildcard character (*)
8. DEFINE CLASES
Static Import Statements
The syntax MUST be import static followed by the fully qualified name of the static member you
want to import, or a wildcard
import static java.lang.System.out;
import static java.lang.Integer.*;
The wildcard (*) import ALL the static members in the class.
You can do a static import on static object references, constants (remember they’re static and final),
and static methods.
9. DEFINE CLASES
Class Declarations and Modifiers
● Access modifiers (public, protected, private)
● Nonaccess modifiers (including strictfp, final, and abstract)
* The fourth access control level (called default or package access)
Class Access
When we say code from one class (class A) has access to another class (class B), it means class A can
do one of three things:
● Create an instance of class B.
● Extend class B (in other words, become a subclass of class B).
● Access certain methods and variables within class B, depending on the access control of those
methods and variables.
10. DEFINE CLASES
Final Classes
The the final keyword means the class can’t be subclassed. In other words, no other class can ever
extend (inherit from) a final class.
Abstract Classes
An abstract class can never be instantiated. Its sole purpose, mission in life, is to be extended
(subclassed).
11. ASSIGNMENTS
Stack and Heap
Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in
the computer's RAM
● Instance variables and objects live on the heap.
● Local variables live on the stack.
12. ASSIGNMENTS
Literal Values for All Primitive Types
A primitive literal is merely a source code representation of the primitive data types
‘b’ // char literal
42 // int literal
false // boolean literal
7421.777 // float literal
13. ASSIGNMENTS
Numeric Literals with Underscores
As of Java 7, numeric literals can be declared using underscore characters (_), ostensibly to improve
readability.
int pre7 = 1000000 // pre java 7
int with7 = 1_000_000 // much clearer
int a = _1_000_000 // illegal
int w = 10_0000_0 // legal but confusing
The main rule you have to keep track of is that you CANNOT use the underscore literal at the
beginning or end of the literal
14. ASSIGNMENTS
Integer Literals
There are four ways to represent integer numbers in the Java language: decimal (base 10), octal
(base 8), hexadecimal (base 16), and, as of Java 7, binary (base 2).
Decimal Literals
int g = 17;
Binary Literals
Also new to Java 7 is the addition of binary literals. Binary literals can use only the digits 0 and 1.
Binary literals must start with either 0B or 0b, as shown:
int f1 = 0B101010; // set f1 to binary 101010 (decimal 42)
int g2 = 0b00011; // set g2 to 11 (decimal 3)
15. ASSIGNMENTS
Octal Literals
Octal integers use only the digits 0 to 7. In Java, you represent an integer in octal form by placing a
zero in front of the number
int six = 06; // decimal 6
int seven = 07; // decimal 7
int eight = 010; // decimal 8
Hexadecimal Literals
Hexadecimal (hex for short) numbers are constructed using 16 distinct symbols. Prefix 0x (or 0X)
int f = 0X0001;
int g = 0x7fffff;
int fg = 0xDeadCafe;
16. ASSIGNMENTS
All four integer literals (binary, octal, decimal, and hexadecimal) are defined as int by default, but
they may also be specified as long by placing a suffix of L or l after the number:
long slx = 06L;
long deore = 0x7fffffl;
Floating-point Literals
Floating-point numbers are defined as a number, a decimal symbol, and more numbers representing
the fraction. If you want to assign a floating-point literal to a variable of type float (32 bits), you must
attach the suffix F or f to the number. If you don’t do this, the compiler will complain about a possible
loss of precision,
float f = 23.467890 // compiler error, possible loss of precision
float g = 48929379.02989F // OK, has the sufix F
17. ASSIGNMENTS
You may also optionally attach a D or d to double literals, but it is not necessary because this is the
default behavior.
double fab = 23.467890D // optional D, not required
double gab = 4779.029 // NO D sufix, OK
Boolean Literals
A boolean value can be defined only as true or false
boolean a = true;
boolean c = 0; // Compiler error
18. ASSIGNMENTS
Character Literals
A char literal is represented by a single character in single quotes, characters are just 16-bit
unsigned integers under the hood.
char a = ‘a’;
char b = ‘@’;
You can also type in the Unicode value of the character, using the Unicode notation of prefixing the
value with u :
char letterN = ‘u004E’; // The letter ‘N’
You can assign a number literal, assuming it will fit into the unsigned 16-bit range (0 to 65535)
char a = 0x892; // hexadecimal char b = 747; // int literal
char c = (char) 70000 // 70000 is out of range char d = (char) -96;
19. ASSIGNMENTS
You can also use an escape code (the backslash) if you want to represent a character that can’t be
typed in as a literal, including the characters for linefeed, newline, horizontal tab, backspace, and
quotes:
char c = ‘”’;
char d = ‘n’;
char tab = ‘t’;
Literal Values for Strings
A string literal is a source code representation of a value of a String object.
String go = “Hello Jug”;
20. ASSIGNMENTS
Assigning One Primitive Variable to Another Primitive Variable
When you assign one primitive variable to another, the contents of the right-hand variable are
copied
int a = 10;
int b = a;
b = 40;
// a ????
21. ASSIGNMENTS
Assigning One Primitive Variable to Another Primitive Variable
When you assign one primitive variable to another, the contents of the right-hand variable are
copied
int a = 10;
int b = a;
b = 40;
// a ????
22. ASSIGNMENTS
Reference Variable Assignments
You can assign a newly created object to an object reference variable
Button b = new Button();
● Makes a reference variable named b, of type Button
● Creates a new Button object on the heap
● Assigns the newly created Button object to the reference variable b
25. ASSIGNMENTS
Variable Scope
For the purposes of discussing the scope of variables, we can say that there are four basic scopes:
1. Static variables have the longest scope; they are created when the class is loaded, and they
survive as long as the class stays loaded in the Java Virtual Machine (JVM).
2. Instance variables are the next most long-lived; they are created when a new instance is created,
and they live until the instance is removed.
3. Local variables are next; they live as long as their method remains on the stack. As we’ll soon see,
however, local variables can be alive and still be “out of scope.”
4. Block variables live only as long as the code block is executing.