Chapter 1 Welcome to Java Flashcards
The Java Development Kit (JDK)
contains the minimum software you need to do Java development.
Key commands include:
-
javac
: compiler, convert source file (.java) into bytecode (.class). -
java
: launcher, launch Java Virtual Machine (JVM), JVM run bytecode on the actual machine. -
jar
: archiver, package files together -
Javadoc
: generate documentation
JVM
Java Virtual Machine (JVM), JVM run bytecode on the actual machine.
JDK
Java Development Kit (JDK)
API
Java comes with a large suite of application programming interfaces (APIs) that you can use.
IDE
integrated development environment (IDE)
Java Release
- Every six months, Oracle releases a new version of Java.
- Java 11 came out in September 2018.
- Java 17 came out in September 2021.
- You should use Java 17 to study with since this is a Java 17 exam. The rules and behavior can change with later versions of Java. You wouldn’t want to get a question wrong because you studied with a different version of Java!
- Every three years, Oracle has a long-term support (LTS) release. LTS releases have patches and upgrades available for at least three years.
- non-LTS versions supported only six months.
Benefits of Java
1. Object Oriented
2. Encapsulation
3. Platform Independent
4. Robust
5. Simple
6. Secure
7. Multithreaded
8. Backward Compatibility
Java has some key benefits that you’ll need to know for the exam.
-
Object Oriented
- Java is an object-oriented language, which means all code is defined in classes, and most of those classes can be instantiated into objects.
- Java allows for functional programming within a class, but object-oriented is still the main organization of code.
-
Encapsulation
- Java supports access modifiers to protect data from unintended access and modification.
-
Platform Independent
- “write once, run everywhere.”
- The portability allows you to easily share pre-compiled pieces of software.
- If you get asked about running Java on different operating systems on the 1Z0-815 exam, the answer is that the same class files run everywhere.
-
Robust
- One of the major advantages of Java over C++ is that it prevents memory leaks.
- Java manages memory on its own and does garbage collection automatically.
- Bad memory management in C++ is a big source of errors in programs.
-
Simple
- Java was intended to be simpler to understand than C++.
- In addition to eliminating pointers, it got rid of operator overloading.
- In C++, you could write a + b and have it mean almost anything.
-
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.
-
Multithreaded
- Java is designed to allow multiple pieces of code to run at the same time. There are also many APIs to facilitate this task.
-
Backward Compatibility
- The Java language architects pay careful attention to making sure old programs will work with later versions of Java. While this doesn’t always occur, changes that will break backward compatibility occur slowly and with notice.
- Deprecation is a technique to accomplish this where code is flagged to indicate it shouldn’t be used. This lets developers know a different approach is preferred so they can start changing the code.
Understanding the Java Class Structure
- Class is the java basic building blocks.
- When defining a class, you describe all the parts and characteristics of one of those building blocks.
- Other building blocks such as interfaces, records, and enums.
- To use most classes, you have to create objects.
- An object is a runtime instance of a class in memory.
- An object is often referred to as an instance since it represents a single representation of the class.
- All the various objects of all the different classes represent the state of your program.
- A reference is a variable that points to an object.
What is class?
Class is the java basic building blocks, a template used to create objects and to define object data types and methods.
A class is the blueprint from which individual objects are created.
A Java class is a blueprint or template from which objects are created. It defines the data and methods that will be present in the objects of that class.
What are the syntax
rules for defining a Java class
?
(WIP)
- Keyword: Start with the keyword ‘class’.
- Class Name: Followed by the name of the class (identifier).
- Class Body: Enclosed within curly braces {}.
- Fields: Define fields (variables) to represent data.
- Constructors: Optionally include constructors to initialize objects.
- Methods: Define methods (functions) to perform operations.
- Access Modifiers: Optionally specify access modifiers (public, private, protected).
- Other Modifiers: Optionally include other modifiers (final, abstract, static).
- Comments: Include comments for code documentation (optional but recommended).
- Semicolon: End the class definition with a semicolon (;) if it is declared within a package and not the main class.
In Java programs, classes are the basic building blocks. When defining a class, you describe all the parts and characteristics of one of those building blocks.
To use most classes, you have to create objects.
An object
is a runtime instance of a class in memory.
An object is often referred to as an instance since it represents a single representation of the class.
All the various objects of all the different classes represent the state of your program.
A reference is a variable that points to an object.
What are Java class primary elements?
- method
-
field
Together these are called themembers of the class
.
FIELDS AND METHODS
Java classes have two primary elements:
methods, often called functions or procedures in other languages, and fields, more generally known as variables.
Together these are called the members of the class.
Variables hold the state of the program, and methods operate on that state.
What is object?
An object is a runtime instance of a class in memory. An object is often referred to as an instance since it represents a single representation of the class.
- A Java object is an instance of a class.
- It is created using the ‘new’ keyword followed by the class name, and it represents a real-world entity or concept in a Java program.
- Objects encapsulate data (fields) and behavior (methods) defined by their class.
- Each object has its own set of instance variables and methods, independent of other objects of the same class.
- Objects interact with each other by invoking methods and accessing each other’s fields.
What is reference?
A reference is a variable that points to an object.
Class declaration
1: public class Animal { 2: }
- Line 1 includes the public keyword, which allows other classes to use it.
- The class keyword indicates you’re defining a class.
- Animal gives the name of the class.
Add variable in class
1: public class Animal { 2: String name; 3: }
- Line 2, we define a variable named name. We also declare the type of that variable to be String.
- A String is a value that we can put text into, such as “this is a string”.
- String is also a class supplied with Java.
Add method in class
1: public class Animal { 2: String name; 3: public String getName() { 4: return name; 5: } 6: public void setName(String newName) { 7: name = newName; 8: } 9: }
- On lines 3–5, we define a method. A method is an operation that can be called.
- Again, public is used to signify that this method may be called from other classes.
- Next comes the return type in this case, the method returns a String.
- On lines 6–8 is another method.
- This one has a special return type called void. The void keyword means that no value at all is returned.
- This method requires that information be supplied to it from the calling method; this information is called a parameter.
- The setName() method has one parameter named newName, and it is of type String.
- This means the caller should pass in one String parameter and expect nothing to be returned.
Class declaration
(WIP)
Class declaration in Java refers to the process of defining a new class. It involves specifying the structure and behavior of the class, including its fields (variables), constructors, and methods. Here’s a breakdown of what class declaration entails:
- Access Modifier: This determines the visibility of the class and its accessibility from other classes. Java provides four access modifiers: public, protected, private, and default (no modifier).
- Keyword: The keyword class is used to declare a new class.
- Class Name: Following the class keyword, you specify the name of the class. This name should be a valid identifier and follow Java’s naming conventions.
- Class Body: Enclosed within curly braces {}, the class body contains all the members of the class, including fields, constructors, and methods.
- Fields (Variables): These are the variables declared within the class to hold data. Fields represent the state of objects created from the class.
- Constructors: Constructors are special methods responsible for initializing objects of the class. They have the same name as the class and may take parameters to set initial values to the object’s state.
- Methods: Methods define the behavior of objects. They encapsulate functionality and can manipulate the object’s state or perform operations.
Example:
public class MyClass {
// Class body containing fields, constructors, and methods
}
Class with field and method
public class Animal { String name; public String getName() { return name; } public void setName(String newName) { name = newName; } }
What is method signature
?
The method name and parameter types are called the method signature
.
Type of Java Comment?
- A single-line comment
// comment until end of line
- **A multiple-line comment (also known as a multiline comment) **
/* Multiple * line comment */
3.** Javadoc comment**:
/** * Javadoc multiple-line comment * @author Jeanne and Scott */
Java Comment
- A single-line comment begins with two slashes. The compiler ignores anything you type after that on the same line.
// comment until end of line
- A multiple-line comment (also known as a multiline comment) includes anything starting from the symbol
/*
until the symbo*/
. People often type an asterisk*
at the beginning of each line of a multiline comment to make it easier to read, but you don’t have to.
/* Multiple * line comment */
- Javadoc comment:
This comment is similar to a multiline comment, except it starts with/**
. This special syntax tells the Javadoc tool to pay attention to the comment. Javadoc comments have a specific structure that the Javadoc tool knows how to read. You probably won’t see a Javadoc comment on the exam. Just remember it exists so you can read up on it online when you start writing programs for others to use.
/** * Javadoc multiple-line comment * @author Jeanne and Scott */
CLASSES VS. FILES
- Most of the time, each Java class is defined in its own .java file.
- Java does not require that the class be public.
- You can even put two classes in the same file.
- Only one of the classes in the file is allowed to be public.
- If you do have a public class, it needs to match the filename.
CLASSES VS. FILES
- Most of the time, each Java class is defined in its own .java file.
- It is usually public, which means any code can call it.
- Interestingly, Java does not require that the class be public.
For example, this class is just fine:
1: class Animal { 2: String name; 3: }
- You can even put two classes in the same file.
- When you do so, at most one of the classes in the file is allowed to be public.
That means a file containing the following is also fine:
1: public class Animal { 2: private String name; 3: } 4: class Animal2 { 5: }
- If you do have a public class, it needs to match the filename.
- The declaration public class Animal2 would not compile in a file named Animal.java.
- In Chapter 7, we will discuss what access options are available other than public.
main()
- A Java program begins execution with its main() method.
- A main() method is the gateway between the startup of a Java process, which is managed by the Java Virtual Machine (JVM), and the beginning of the programmer’s code.
- The JVM calls on the underlying system to allocate memory and CPU time, access files, and so on.
valid main()
public static void main(String []args) { } public static void main(String args[]) { } public static void main(String...args) { } public strictfp static void main(String[] args) { } public static void main(final String[] args) { } final static synchronized strictfp void main(final String[] args) { }