Podcast
Questions and Answers
What is the purpose of Java JRE?
What is the purpose of Java JRE?
To execute Java Applications.
What functionality does Java JDK contain?
What functionality does Java JDK contain?
To start and develop Java programs.
Which of the following are examples of Java primitive data types? (Select all that apply)
Which of the following are examples of Java primitive data types? (Select all that apply)
Java variables must have a data type.
Java variables must have a data type.
Signup and view all the answers
The class containing the main method in Java is also known as the __________ class.
The class containing the main method in Java is also known as the __________ class.
Signup and view all the answers
Match the following terms with their descriptions:
Match the following terms with their descriptions:
Signup and view all the answers
What is the result of -16 % 3?
What is the result of -16 % 3?
Signup and view all the answers
How can you force a formula in Java to not truncate a value?
How can you force a formula in Java to not truncate a value?
Signup and view all the answers
What is the correct syntax to cast a double value to an int?
What is the correct syntax to cast a double value to an int?
Signup and view all the answers
The equals() method in Java should be used when trying to find the lexicographical order of two strings.
The equals() method in Java should be used when trying to find the lexicographical order of two strings.
Signup and view all the answers
The symbol __ is used to concatenate two strings together.
The symbol __ is used to concatenate two strings together.
Signup and view all the answers
Match the following error types to their descriptions:
Match the following error types to their descriptions:
Signup and view all the answers
What keyword can be used to exit a loop at a specified point?
What keyword can be used to exit a loop at a specified point?
Signup and view all the answers
A nested for loop cannot be used to iterate through two-dimensional arrays.
A nested for loop cannot be used to iterate through two-dimensional arrays.
Signup and view all the answers
Study Notes
Java Basics
- Java Runtime Environment (JRE) contains only the necessary functionality to start Java programs.
- Java Development Kit (JDK) contains functionality to start Java programs as well as develop them.
- The Java JRE is required to execute Java Applications.
Creating a Program in Eclipse
- To create a program in Eclipse, follow these steps:
- Create a Project.
- Create a Package (inside the src folder of the project).
- Create Class(es) inside the package.
- At least one of the classes must contain a main method.
- This class is called the Driver class.
- Compile the Java code.
- Run the Java code from the Driver class.
Naming Packages
- Package names should be the same as the project name using lower camel case.
- Camel case is the practice of stringing capitalized words together with no spaces.
The Main Method
- A main method in Java is the method inside a class that runs when the class is compiled and executed.
- The class containing the main method is also referred to as the Driver Class.
Java Class Comments
- Add comments to your code to describe what the lines of code do.
- Comments are ignored by the Java compiler.
- To add a comment, type // in front of the comment for a single line comment.
- To add a block comment (for multiple lines), use /* to start and */ to end.
General Java Program Form
- There are two general forms of Java classes:
- Driver classes.
- Object classes.
- Driver classes:
- Contain a main method.
- The main method may include instances of objects from an object class, variables, loops, conditional statements, and other programming logic.
- Object classes:
- Define objects to be used in a driver class.
- Can be found in the Java API or created by you.
Java Keywords
- All Java programs use Java keywords.
- Examples of Java keywords include class, public, String, int, for, while, and double.
Java Program Naming Conventions
- Naming conventions for a Java program:
- The optional package name is defined before an import statement in lower camel case.
- The optional import statements are defined below the package name.
- The class name is a noun labeled using upper camel case.
- Variable names are short but meaningful in lower camel case.
- Constant names are declared in uppercase letters with the final modifier.
- Constructors are named the same as the class name.
- Methods are verbs named in lower camel case.
Constructors
- A constructor method is unique in Java because:
- The method creates an instance of the class.
- Constructors always have the same name as the class and do not declare a return type.
- Constructors can be with or without parameters.
- Constructors without parameters (default constructors) initialize the numeric class variables to zero, and object variables to null.
Accessor and Mutator Methods
- Accessor methods (getters):
- Methods that return the value of each class variable.
- Mutator methods (setters):
- Methods that change the value of each class variable.
- Examples of accessor and mutator methods:
- getStudentId(), getName(), getSSN(), getGPA().
- setStudentId(), setName(), setSSN(), setGPA().
Code Blocks
- A code block is defined by opening and closing "curly braces" { }.
- Code blocks are used to:
- Enclose class declarations.
- Enclose method declarations.
- Enclose conditional statements (if statements, switch statements).
- Enclose loops.
Variables
- Variables must have a data type for security reasons.
- Primitive data types are used to store data during a program's operation.
- Examples of primitive data types:
- int (integers).
- double (decimals).
- Binary literals can be expressed using the binary system by adding the prefixes 0b or 0B to the number.
Rules for Variable Names
- Do not use a Java keyword or reserved word.
- Do not use a space in the variable name.
- Cannot start with a number.
- The only symbols allowed are the underscore ( _ ) and the dollar sign ($).
Scope and Lifetime
- Scope refers to the block of code where a variable exists in a program.
- Once the final brace of the block } is reached, the variable goes out of scope and is no longer recognized as a declared variable.
Operators
- Types of operators:
- Arithmetic operators.
- Binary operators.
- Assignment operators.
- Relational operators.
- Logical operators.
String References
- In Java, Strings are references to objects in memory.
- String concatenation can be done using the + and += operators.
- Escape sequences in string literals allow the user to add characters that would be misinterpreted by the compiler.
String Methods
- Useful String methods:
- length(): returns the length of the string.
- substring(): returns part of the string.
- indexOf(): returns the index of the first occurrence of a character in the string.
- charAt(): returns the character at a specific index in the string.
- equals(): compares two strings for equality.
- compareTo(): compares two strings lexicographically.
Input and Output
- To read in the user's input, use the Java object Scanner.
- The Scanner method next() reads in the user's input as a String and returns that String.
- The Scanner method nextInt() reads in the user's input as an integer and returns that integer.
Control Structures
- If-else statements:
- Used to evaluate a condition and execute a block of code if the condition is true.
- Can also have an else clause to execute a block of code if the condition is false.
- Switch statements:
- Used to evaluate a variable and execute a block of code based on the value of the variable.
- Can have multiple case statements and a default statement.
Loops
-
Loops are used to repeat a block of code for a specified number of iterations.
-
Types of loops:
- For loops.
- While loops.
- Do-while loops.### Loops
-
A loop allows for a series of inputs with the same code, starting at the beginning, executing the logic, and then returning to the beginning with new input.
-
A loop needs a stopping condition, which can be specified as a set number of times to run the code or a boolean condition that changes in the code to make the loop stop executing.
The While Loop
- The while loop is designed to loop while something remains true.
- The loop parameters can be boolean types or produce a boolean value using conditional statements.
Infinite Loops
- If the condition doesn't change, the loop will run forever as an infinite loop.
The Do-While Loop
- The do-while loop is a post-test loop that runs the program through the loop once before testing the boolean condition.
- It continues until the condition becomes false.
- If the condition doesn't change, the loop will run forever as an infinite loop.
The For Loop
- The for loop tells the loop when to stop by explicitly saying "Stop when the loop has run once for every piece of clothing."
- It is useful when you know exactly when the condition will be false.
Using Break and Continue
- The break keyword is useful for exiting a loop at a specified point.
- It causes the code to exit the loop.
Arrays
- Arrays can contain any data type, including primitive, pre-defined objects, and programmer-defined objects.
- Declaring an array requires specifying the data type, variable name, and array size.
Two-Dimensional Arrays
- A two-dimensional array is an array that stores other arrays.
- The number of arrays contained within the array and the number of items in each internal array are defined upon declaration.
Nested for Loops
- A nested for loop is a for loop inside a for loop.
- It is used to iterate through two-dimensional arrays using the outer for loop counter as the index for the arrays (rows) and the inner for loop counter as the index for the elements of each array (columns).
Types of Errors
- There are three types of errors: syntax errors, logic errors, and exceptions (run-time errors).
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
This quiz covers the key differences between Java Runtime Environment (JRE) and Java Development Kit (JDK) as well as the steps to create a program in Eclipse