chapter 1_JavaBuildingBlocks Flashcards
- What are the primary elements in Java Class?
a. methods (functions)
b. fields, (variables)
- what are the 3 types of comments?
a. A single-line comment -> //
b. A multiple-line comment
- > /* Multiple
* line comment
* /
c. Javadoc comment
- > * Javadoc multiple-line comment
* @author Jeanne and Scott
* /
- what command need to run java
- javac .java
- java
- what java not import
- It doesn’t import child packages, fi elds, or methods; it imports only classes.
- what is special package that java automatically import
- java.lang.
- how many import that redundant?
1: import java.lang.System;
2: import java.lang.;
3: import java.util.Random;
4: import java.util.;
5: public class ImportExample {
6: public static void main(String[] args) {
7: Random r = new Random();
8: System.out.println(r.nextInt(10));
9: }
10: }
- three of the imports are redundant. Lines 1 and 2 are redundant because everything in java, Line 4 is also redundant in this example because Random is already imported from java.util.Random.
- What is constructor look like? can you give some example?
public class Chick { public Chick() { System.out.println("in constructor"); } }
- What is instance look like? can you give some example?
Random r = new Random();
- what is read and write variables is known as?
Reading a variable is known as getting it. Writing to a variable is known as setting it.
- What is ‘instace initializers’?
Code blocks that appear outside a method.
- Explain order of initialization
- 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.
- how many built-in data types or Java primitive types, and state it.
Eight, which are boolean, byte, short, int, long, float, double and char
- Can you state all keyword and type contains in Java primitive type
a. boolean - true or false
b. byte - 8-bit integral value
c. short - 16-bit integral value
d. int - 32-bit integral value
e. long - 64-bit integral value
f. float - 32-bit floating-point value
g. double - 64-bit floating-point value
h. char - 16-bit Unicode value
- how many base can write in java, and how to write it
a. base 10 - System.out.println(56); // 56
b. binary - System.out.println(0b11); // 3
c. octal - System.out.println(017); // 15
d. hexadecimal - System.out.println(0x1F); // 31
- explain about numeric literals
can have underscores in numbers to make them easier to read, You can add
underscores anywhere except at the beginning of a literal, the end of a literal, right before a
decimal point, or right after a decimal point.
- How many ways value can be assigned to reference?
Two
a. A reference can be assigned to another object of the same type.
b. A reference can be assigned to a new object using the new keyword.
- state few key difference between primitives and reference type.
a. Reference types can assigned to null, while primitive will give compiler error if assigned to null.
b. Reference types can used to call method when not point to null. While primitives do not have method.
c. all the primitive types have lowercase type names
18. Fiure out which of the following are legal declarations. boolean b1, b2; String s1 = "1", s2; double d1, double d2; int i1; int i2; int i3; i4;
- The fi rst statement is legal. It declares two variables without initializing them.
- The second statement is also legal. It declares two variables and initializes only one of them.
- The third statement is not legal. Java does not allow you to declare two different types in the same statement.
- The fourth statement is legal. Although int does appear twice, each one is in a separate statement.
- The fi fth statement is not legal. Again, we have two completely different statements on the same line. The second one is not a valid declaration because it omits the type.
- What is rules about identifier names.
a. The name must begin with a letter or the symbol $ or _.
b. Subsequent characters may also be numbers.
c. cannot use the same name as a Java reserved word, but you can use versions of the keywords that only differ in case.
- can you explain about local variables.
a. A local variable is a variable defi ned within a method.
b. Local variables must be initialized before use.
c. They do not have a default value and contain garbage data until initialized.
d. The compiler will not let you read an uninitialized value.
- explain about Instance and Class variable.
a. variables that are not local variables
b. Class variables are shared across multiple objects. We can tell its as class variable because it has static keyword
c. not require to declare, it will give default value
- state the Default initialization value by type.
Variable type - Default initialization value
a. boolean - false
b. byte, short, int, long - 0 (in the type’s bit-length)
c. float, double - 0.0 (in the type’s bit-length)
d. char - ‘\u0000’ (NUL)
e. All object references (everything else) - null
- state the rules on variables scope.
a. Local variables—in scope from declaration to end of block
b. Instance variables—in scope from declaration until object garbage collected
c. Class variables—in scope from declaration until program ends
- whats the functions of garbage collector?
process of automatically freeing memory on the heap by deleting objects that are no longer reachable in your program.