Part 1 Flashcards
What is the minimum that java program should contain, so it can compile by the javac command without any error?
The program .java can contain any number of classes, but only one public class to be compiled with javac commnad line. If the program/file contains public class, the file should be called the same name. For example if the public class is called ExampleName, the file needs to be called the same ExampleName.java If the program/file doesn’t contain public class, it can be called by any name.
public class Main { } / java program that can be compiled should contain at least one class (with allowed access modifiers)
It will compiled by: public, default, final, abstract, strictfp
But it will not be compiled with private, protected, static, native and synchronised if it contain only one class
What is the minimum that java program should contain to be executable by the java command?
Only class that contain main method can be executed by java command. When we will run class with java command, it will failed with error if it doesn't contain the main method.
public class Main { public static void main(String[] args) {
}
}
class Main { public static void main(String[] args) {
}
}
What is required rule to name java class if it is public class?
If java class is declared as public it has to be named the same as class:
The file that contain ‘public class Main { }’ should be Main.java
What is required rule to name java class if it is not public class?
If java class is not declared as public (it has default or final or abstract access modifier), the file can be called by any name.
How we can include class from different place into the actual program?
If we want to include class from different place into the actual program/file, we can do it by two ways:
- Reference each class with the whole name with package path in time of usage like
java. util.ArrayList lits = new java.util.ArrayList(); - Use import statement in the beginning of the file, rigth after the package statement and before class declaration like: import java.util.ArrayList; In this case we can use simple class name ArrayList in program afterwards.
What kind of import statements we can use in our java program?
There are also two ways how to use import statement: explicit and implicit import
Explicit import: use the whole reference package path to the class: import java.util.ArrayList;
Implicit import: use more general reference to whole package that will include all classes inside stated package path: import java.util.*;
But this statement will not include classes in other subpackages, for example java.util package contains regex subpackage that have two classes Pattern and Matcher, if you want include this classes with implicit import statement, you need to declare like: import java.util.regex.*;
Explicitly it would be: import java.util.regex.Pattern;
How we can include the classes that are part of subpackage of the more general package? Show the example of such packages.
Package statment doesn’t include reference to its packages, therefore we need include each subpackage of that package separately.
For example java.util package contains regex subpackage that have two classes Pattern and Matcher, if you want include this classes with implicit import statement, you need to declare like: import java.util.regex.*;
Explicitly it would be: import java.util.regex.Pattern;
Why we should use rather explicit import over the implicit import?
The best way is to use explicit import, because we can overuse the implicit imports and it could cause umbiguity of class reference: For example import java.util.*; and import java.sql.*; both contains class named Date, and if we will reference this class as Date in program, it will fail because it will not know what class Date it should use.
How compiler will look for classes based on import statement?
There are some order how java compiler looking for classes:
- it will look for based on explicit import statement
- it will take classes from the same directory
- it will look for based on implicit import statement
What is the difference between general import and static import?
normal imports start with: Math; util.; // we are looking for classes
static imports start with: Math.sqrt; Math.; // we are looking for members
We can use normal imports to import classes and interfaces of a package. Whenever we are using normal import we can access class and interface directly by their short name. We can use static import to import static members of a particular class.
import static java.lang.Math.*; is correct
import java.lang.Math; is correct
import static java.lang.Math; is incorrect
import static java.lang.Math.sqrt; is correct
How compiler will look for static members that should be included to program? (precedence order)
While resolving static members compiler will give the precedence in the following order:
- Current class static members
- Explicit static import
- Implicit static import
Why we are using the package statement in java program (name 4 objections at least)?
It is an encapsulation mechanism to group related classes and interfaces into a single module.
The main objectives of packages are:
- to resolve name conflicts
- to improve modularity of the application
- to provide security
- there is one universally accepted naming convention that is to use internet domain name in reverse
Where the compiler will create a .class file if the compiled file doesn’t contain the package statement in it? So we will use single command javac without any argument.
If you will type only javac Test.java the compiler will create class file inside the current working directory. But if you want to create class file inside the same package as for program/file, use the javac -d argument: javac -d . Test.java
When you want to create class file inside the same package as the program has stated with package statement in it, what command line argument will you use?
But if you want to create class file inside the same package as for program/file, use the javac -d argument: javac -d . Test.java it will go to package stated in program file with package statement . [dot] means current working directory
You can use also direct statement to path, for example use E: root
javac -d E: Test.java
Then if you want to execute/run it with java command, use whole package path for execution:
java com.durgasoft.ocja.Test
How many package statements can program file .java contains and where you should place it inside that program file?
0 or 1 package is allowed for one program/file
First (at the beginning) statement in file should be package, if it is stated after import statement compilation will failed. Order is important: package -> import -> class|interface|enum