Java Building Blocks
Exam objectives
Java Basics
Define the scope of variables
Define the structure of a Java class
Create executable Java applications with a main method, run a Java program from the command line, including console output
Import other Java packages to make them accessible in your code
Compare and contrast the features and components of Java such as platforms independence, object orientation, encapsulation, etc.
Working with Java Data Types
Declare and initialize variables (including casting or primitive types)
Differentiate between object reference variables and primitive variables
Know how to read or write to object fields
Explain an Object's Lifecycle (creation, "dereference by reassignment" and garbage collection)
Notes:
Methods and Fields
Object is a runtime instance of a class in memory.
Java classes have two primary element: methods and fields.
Example class:
Method: access modifier, static (binds method to the class), return type, method name, parameters.
A file can have 2 or more classes but just one of them is a public class and have the name matches the file's name.
main()
class is a gate way between the startup of a Java process managed by JVM and the beginning of the programmer's code.
All command line arguments are String objects.
Packages and Imports
Java puts classes in packages (which are folders).
Import statement tells the compiler which package to look in to find a class.
Wildcards
Class in the same package are often imported together.
Including wildcards DO NOT slow the program down, it also help shorten the import list.
Redundant Imports
java.lang
is automatically imported.Redundant Imports example:
or
not
Naming conflicts
Different packages' classes can have the same name
Using 2 wildcard import for packages that have same used class ends up compiler error
ie:
If You Really Need to Use Two Classes with the Same Name…
Code Formatting on the Exam
If the code begins with line 1 or not?
Creating objects
Constructors
To create an object declare the type + variable name = new constructor;
constructor is to initialize fields, 2 ways of doing this:
Reading and Writing Object Fields
Instance Initializer Blocks
The code between the braces({}) is called a code block.
Other times, code blocks appear outside a method. These are called instance initializers.
Order of Initialization
Fields and instance initializer blocks are run in the order in which they appear in the file.
The constructor runs after all fields and instance initializer blocks have run.
Distinguishing Between Object References and Primitives
2 types of data:
primitive types
reference types
Primitive Types
float and double are used for floating-point (decimal) values.
A float requires the letter f following the number so Java knows it is a float.
byte, short, int, and long are used for numbers without decimal points.
Each numeric type uses twice as many bits as the smaller similar type. For example, short uses twice as many bits as byte does.
number is called literal.
Java allows you to specify digits in several other formats:
octal (digits 0–7), which uses the number 0 as a prefix—for example, 017
hexadecimal (digits 0–9 and letters A–F), which uses the number 0 followed by x or X as a prefix—for example, 0xFF
binary (digits 0–1), which uses the number 0 followed by b or B as a prefix—for example, 0b10
Can have underscore to make them easier to read:
Be careful
Reference Types
Referenced object is not stored on memory, we use pointer to address them.
A value is assigned to a reference in one of two ways:
A reference can be assigned to another object of the same type.
A reference can be assigned to a new object using the new keyword.
Key Differences
Reference types can be assigned null, which means they do not currently refer
to an object. Primitive types will give you a compiler error if you attempt to assign them null.
Reference types can be used to call methods when they do not point to null.
Primitives do not have methods declared on them. Primitives do not have methods.
All the primitive types have lowercase type names. All classes that
come with Java begin with uppercase.
Declaring and Initializing Variables
Declaring Multiple Variables
Identifiers
There are only three rules to remember for legal identifiers:
The name must begin with a letter or the symbol $ or _.
Subsequent characters may also be numbers.
You cannot use the same name as a Java reserved word. Java is case sensitive, so you can use versions of the keywords that only differ in case. Please don’t, though.
Reserved words are
abstract assert boolean break byte case catch char class const continue default do double else enum extends false final finally float for goto if implements import instanceof int interface long native new null package private protected public return short static strictfp super switch synchronized this throw throws transient true try void volatile while
ie:
The following examples are legal:
okidentifier
$OK2Identifier
_alsoOK1d3ntifi3r
__SStillOkbutKnotsonice$
These examples are not legal:
3DPointClass // identifiers cannot begin with a number
hollywood@vine // @ is not a letter, digit, $ or _
*$coffee // * is not a letter, digit, $ or _
public // public is a reserved word
Most Java developers follow these conventions for identifier names:
Method and variables names begin with a lowercase letter followed by CamelCase.
Class names begin with an uppercase letter followed by CamelCase. Don’t start any identifiers with $. The compiler uses this symbol for some files.
Understanding Default Initialization of Variables
Local Variables
A local variable is a variable defined within a method. Local variables must be initialized before use.
Instance and Class Variables
Variables that are not local variables are known as instance variables or class variables. Instance variables are also called fields.
You can tell a variable is a class variable because it has the keyword static before it.
Instance and class variables do not require you to initialize them. As soon as you declare these variables, they are given a default value.
Understanding Variable Scope
These smaller contained blocks can reference variables defined in the larger scoped blocks, but not vice versa.
Local variables - in scope from declaration to end of block
Instance variables - in scope from declaration until object garbage collected
Class variables - in scope from declaration until program ends
ie:
Block for scope
Ordering Elements in a Class
Multi classes can be in a file but just one class can be public
Destroying Objects
Java objects are stored in your program memory's heap. The heap is quite large but there is always a limit to its size.
Garbage Collection
Refers to the process of automatically freeing memory on the heap by deleting objects that are no longer reachable in your program.
Java provides a method called
System.gc()
. Now might be a good time for Java to kick off a garbage collection run. Java is free to ignore the request.An object will remain on the heap until it is no longer reachable. An object is no longer reachable when one of two situations occurs:
The object no longer has any references pointing to it.
All references to the object have gone out of scope.
finalize()
This method gets called if the garbage collector tries to collect the object. If the garbage collector doesn’t run, the method doesn’t get called.
The lesson is that the finalize() call could run zero or one time.
Benefits of Java
Object Oriented: all code is defined in classes and most of those classes can be instantiated into objects. Java allows functional programming within a class, but objects oriented is still the main organization of code.
Encapsulation: Java support access modifier to protect data from unintended access and modification.
Platform Independent: Java is an interpreted language because it gets compiled to bytecode. This is known as "write once, run everywhere".
Robust: Java over C++ in preventing memory leaks. Java manages memory on its own and does garbage collection automatically.
Simple: Java was intended to be simpler than C++. In addition to eliminating pointers, it got rid of operator overloading.
Secure: Java code runs inside the JVM. This creates a sandbox that makes it hard for Java code to do evil things to the computer it is running on.
Summary
In this chapter, you saw that Java classes consist of members called fields and methods. An object is an instance of a Java class. There are three styles of comment: a single-line comment (//), a multiline comment (/* \*/), and a Javadoc comment (/** \*/).
Java begins program execution with a main() method. The most common signature for this method run from the command line is
public static void main(String[] args)
. Arguments are passed in after the class name, as in java NameOfClass firstArgument. Arguments are indexed starting with 0.Java code is organized into folders called packages. To reference classes in other packages, you use an import statement. A wildcard ending an import statement means you want to import all classes in that package. It does not include packages that are inside that one.
java.lang is a special package that does not need to be imported.
Constructors create Java objects. A constructor is a method matching the class name and omitting the return type. When an object is instantiated, fields and blocks of code are initialized first. Then the constructor is run. Primitive types are the basic building blocks of Java types. They are assembled into reference types. Reference types can have methods and be assigned to null. In addition to “normal” numbers, numeric literals are allowed to begin with 0 (octal), 0x (hex), 0X (hex), 0b (binary), or 0B (binary). Numeric literals are also allowed to contain underscores as long as they are directly between two other numbers.
Declaring a variable involves stating the data type and giving the variable a name. Variables that represent fields in a class are automatically initialized to their corresponding “zero” or null value during object instantiation. Local variables must be specifically initialized. Identifiers may contain letters, numbers, $, or _. Identifiers may not begin with numbers.
Scope refers to that portion of code where a variable can be accessed. There are three kinds of variables in Java, depending on their scope: instance variables, class variables, and local variables. Instance variables are the non-static fields of your class. Class variables are the static fields within a class. Local variables are declared within a method.
For some class elements, order matters within the file. The package statement comes first if present. Then comes the import statements if present. Then comes the class declaration. Fields and methods are allowed to be in any order within the class.
Garbage collection is responsible for removing objects from memory when they can never be used again. An object becomes eligible for garbage collection when there are no more references to it or its references have all gone out of scope. The finalize() method will run once for each object if/when it is first garbage collected.
Java code is object oriented, meaning all code is defined in classes. Access modifiers allow classes to encapsulate data. Java is platform independent, compiling to bytecode. It is robust and simple by not providing pointers or operator overloading. Finally, Java is secure because it runs inside a virtual machine.
Exam Essentials
Be able to write code using a
main()
method.Understand the effect of using packages and Imports.
Be able to recognize a constructor.
Be able to identify legal and illegal declarations and initialization.
Be able to determine where variables go into and out of scope.
Be able to recognize misplaced statements in a class.
Know how to identify when an object is eligible for garbage collection.
Last updated