Chapter 1 - Java building blocks Flashcards
How many public classes a file can have?
- Each file can contain only one public class
2. The file name must match the class name, including case, and have a .java extension.
What is the main method structure?
public static void main (String[ ] args) {
}
Where:
public is the access modifier. It declares this method’s level of exposure to potencial callers in the program.
static binds a method to its class, so it can be called by just the class name. An object does not need to be created to call the method.
void represents the return type. A method that returns no data.
main method: represented as an array of java.lang.String objects.
Valid sintax:
String[ ] args
String args [ ]
String… args
What are imports needed for?
Imports are needed to be able to reference an external class.
What are wildcards used for?
Wildcards (*) are use to import all the classes from the same package. Importing all the classes in a package vs just the class that is going to be used does not slow down the program. Listing each of the classes makes the code easy to read. Using the wildcard can shorten the import list.
Is java.lang package imported automatically?
Yes, Java.lang package is imported automatically, so no import statements need to be used for classes in the lang package. I.e. java.lang.System.
Below are examples of redundants imports:
Import java.lang.System
Import java.lang.*
Import java.util.Random
Import java.util.*
What are the classes import rules?
- A wild card only matches class names.
- Just one wildcard must be present and it should be at the end.
- Just classes can be imported. Methods cannot be imported.
Examples of bad importers:
import java.nio.*;
import java.nio..;
import java.nio.files.Paths.*;
What is the code needed to compile a Java program from the command prompt?
To Compile:
javac package/Class.java
What is the code needed to run a Java program from command prompt?
java package.class
In Java you don’t need to pass the extension when running a program. Example:
java package.ClassB
Instead of java package.ClassB.class
What is the purpose of a constructor?
The purpose of the constructor is to initialize fields, although you can put any code in there.
How a constructor should be defined?
- The name of the constructor should match the name of the class.
- There is no return type in the constructors.
What is a default constructor?
default constructor is the one that is not provided by the user but by the jvm. This may be an exam question.
Where are instance initializers located at?
instance initializers blocks are outside a method.
How is the initialization order?
- 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.
What is a literal?
When a number is present in the code, it is called Literal.
Can underscores be included in numbers? Where they can go?
Yes, since Java 7, underscores can be included in numbers to make them easier to read.
- Underscores can be added anywhere EXCEPT at the beginning of a literal, the end of a literal, right before a decimal point, or right after a decimal point.
What is a reference type?
A reference type refers to an object (instance of a class).
What a primitive type holds?
Primitive types hold their values in memory where the variable is allocated.
What a pointer is?
references do not hold the value of the object they refer to. Instead they point to an object by storing the memory address where the object is located at. This concept is referred to as a pointer.
Can a reference type be asigned a null?
Yes, Reference types can be assigned null, which means they do not currently refer to an object.
Can a primitive type be assigned a null?
No, Primitive types will give you a compiler error if you attempt to assign them null.
Can reference types call methods?
Yes, reference types can be used to call methods when they do not point to null.
Do primitives have methods declared on them?
Primitives do not have methods declared on them.
What is the purpose of a constructor?
The purpose of the constructor is to initialize fields, although you can put any code in there.
How a constructor should be defined?
- The name of the constructor should match the name of the class.
- There is no return type in the constructors.
What is a default constructor?
default constructor is the one that is not provided by the user but by the jvm. This may be an exam question.
Where are instance initializers located at?
instance initializers blocks are outside a method.
How is the initialization order?
- 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.
What is a literal?
When a number is present in the code, it is called Literal.
Can underscores be included in numbers? Where they can go?
Yes, since Java 7, underscores can be included in numbers to make them easier to read.
- Underscores can be added anywhere EXCEPT at the beginning of a literal, the end of a literal, right before a decimal point, or right after a decimal point.
What is a reference type?
A reference type refers to an object (instance of a class).
What a primitive type holds?
Primitive types hold their values in memory where the variable is allocated.
What a pointer is?
references do not hold the value of the object they refer to. Instead they point to an object by storing the memory address where the object is located at. This concept is referred to as a pointer.
Can a reference type be asigned a null?
Yes, Reference types can be assigned null, which means they do not currently refer to an object.
Can a primitive type be assigned a null?
No, Primitive types will give you a compiler error if you attempt to assign them null.
Can reference types call methods?
Yes, reference types can be used to call methods when they do not point to null.
Do primitives have methods declared on them?
Primitives do not have methods declared on them.
What are the identifiers definition rules?
- The name must begin with a letter or the symbol $ or _
- Subsequent characters may also be numbers.
- Java reserved words cannot be used.
- dots are nor allowed.
What is the capitalization that needs to be used while defining variables and methods?
Method and variables names begin with lowercase letter followed by CamelCase.
What is the capitalization that needs to be used while defining a class?
Class names begin with an uppercase letter followed by CamelCase. Don’t start any identifiers with $. The compiler uses this symbol for some files.
Which types of variables does Java have?
Local variables Instance and class variables
Where a local variable should be defined and when does it need to be initialized?
Local variables are defined within a method.
Must be initialized before use. They do not have a default value
Where instance variables are defined and when do they need to be initialized?
Instance and Class Variables are not local variables. Instance variables are also called fields.
A class variable can be recognized because it has the static keyword in its declaration.
Instance and class variables do not require to be initialized. As soon as they are declared, they are given a default value.
What is the default value assigned to Java objects?
For objects java assigns NULL
What are the default values assigned to primitives?
For primitives java assigns 0 or false
boolean: false
byte, short, int, long: 0
float, double: 0.0
char: ‘\u0000’ (NUL)
What is the scope of a local variable?
Local variables - in scope from declaration to end of block.
What is the scope of an instance variable?
Instance variables - in scope from declaration until object garbage collected.
What is the scope of a class variable?
Class variables - in scope from declaration until program ends.
What garbage collection refers to?
Refers to a process of automatically freeing memory on the heap by deleting objects that are no longer reachable in the program.
When an object is no longer reacheable?
- The object no longer has any references pointing to it.
- All references to the object have gone out of scope.
How many times can the finalize() method run?
- finalize() : the call to this method could run zero or one time.
In which line should be placed a chainned constructor call?
- When channing constructors, the chainned constructor line call should be the first inside the other constructor.
Can chainned constructors be placed inside methods?
No, Chainned constructors cannot be placed inside a method. An error will be thrown.
What are the 3 flavors of operators available in Java?
three flavors of operators are available in Java: unary, binary and ternary.
Arithmetic operators should be applied to? And EXCEPT to?
All of the arithmetic operators may be applied to any Java primitives. EXCEPT boolean and String.
Which aritmetic operators can be applied to String?
Only the addition operators + and += may be applied to String values, which result in String concatenation.
If two values have different data types in an operation, what Java does?
If two values have different data types, Java will automatically promote one of the values to the larger of the two data types
To which data type are promoted small data types such as byte, short and char while used with Java binary arithmetic operators?
Smaller data types, namely byte, short and char, are first promoted to int any time they’re used with Java binary arithmetic operator, even if neither of the operands is int. **important: unary operators are excluded from this rule. I.e: applying ++ to a short value results in a short value.
Which data type will have a resulting value after all promotion has occur?
After all promotion has occurred and the operands have the same data type, the resulting value will have the same data type as its promoted operands.
Can logical inversion (!) be applied to numeric values?
No, Logical inversion cannot be performed on a numeric value.
i.e. int x = !5;
Can numerical negation be applied to booleans?
No, numerical negation cannot be applied to booleans.
i.e. boolean y = -true
What is done by Java to go from larger numerical data types to smaller data types?
Casting can be used to avoid compiler issues everytime you are going from larger numerical data type to a smaller numerical data type, or converting from floating-point number to an integer value.
Examples:
int x = (int)1.0
Short y = (short)1921222
int z = (int)9f
long t = 192301398193810323L
A rule for a compound assignment operator is?
The left-hand side of the compound operator can only be applied to a variable that is already defined and cannot be used to declare a new variable. For example:
int x = 2, z = 3;
x *= z
x should be defined first. If x was not already defined a compiler error will happen.
Which are the 3 logical operators use in Java?
Logical operators are: &, |, and ^
& aka AND
| aka exclusive OR
^ aka inclusive OR
When the logical operator & is true?
AND is only true if both operators are true.
When the inclusive OR operator is false?
Inclusive OR is only false if both operands are false.
When the exclusive OR operator is true?
Exclusive OR is only true if the operands are different.
What are the 2 types of conditional operators?
Conditional operators are && and ||
What is the main important characteristic of conditional operators && and || while examining an expression?
Conditional operators are nearly identical to the logical operators except that the right-hand side of the expression may never be evaluated if the final result can be determined by the left-hand side of the expression.