Java Eclipse Flashcards
What’s the first thing u do after creating a java project in eclipse
You create a package in src
Classes are created inside that package.
How to get used input in java
Scanner scan = new Scanner(System.in); X = scan.nextInt();
How to create a sub class in java
public class child extends parent {…}
How does JUnit work?
JUnit create a class separate from the class under test (business class). Inside this test class, we instantiate a business class object and specific methods in business class instance are called with known arguments and returned value is compared with known correct value using assrtEquals.
Each test method is annotated with
@Test
In Eclipse, how do u create JUnit test case?
Right click on the class to be tested -> JUnit Test Case
What are some assertion methods used in JUnit?
- assertEquals()
- assertNotEquals()
- assertTrue()
- assertNull()
etc.
What are some annotations used in JUnit?
@BeforeClass: This method is executed before any test is run. Used for setting up. Business class object may be instantiated here and used in other test cases.
@Before: This method is executed before each test case is run.
@Test: This annotation is prefixed before each test method.
@After: This method is executed after each test case is run.
@AfterClass: This method is executed after ALL test cases are run. Typically used to teardown the set up and free all resources.
When is a jar file runnable?
The jar file should contain a class with a main() method.
How to work with jar file?
Create jar file with a bunch of compiled java class files, with following cmd, jar cvf newFileName.jar first.class second.class …
Check contents of jar file
jar tvf filename.jar
To run a jar file, we have to know the class we want to run, say xyz.class Find full path to the class inside jar file
jar tvf filename.jar
Locat the line containing xyz.class, say
aa/bb/cc/xyz.class
run with following cmd,
java -cp /path/to/jar/file.jar aa/bb/cc/xyz
or
java -cp /path/to/jar/file.jar aa.bb.cc.xyz
What is a package in java?
All classes should specifically be placed in named packages.
When you define a class, make first line in the file as
package myOwnPackage;
Then define your class, this will put the class in “myOwnPackage” pkg.
In other files, where that class is needed, put
import myOwnPackage;
import java.util.Date; // wil import Date class
import java.util.*; // Will import all classes in java.util
==> Date x = new Date();
If u don’t want to import entire package, use following,
java.util.Date x = new java.util.Date(); // No need for import
What is java classpath?
When u run a jar file, u have to explicitly specify classpath using -cp, eg.
java -cp /path/to/jar/file.jar aa.bb.cc.xyz
What is the structure of main() function in java?
public static void main(String[] args){
System.out.println(“Test - 1”);
}
What is single import declaration?
When you import only one class from a package.
U explicitly specify packageName and className, like,
import pkgName.className;
How do u import all classes from a package?
import pkgName.;
import java.util.;
This will import all classes from that pkg, BUT NOT FROM sub-packages.
When compiling a class file which specifies a package, how do u create a directory structure at the time of compilation?
With -d option,
javac -d . myClass.java