Introduction to Java Flashcards

1
Q

What are the selling points of Java?

A

1️⃣ <b>Write Once, Run Everywhere</b>: Complied binary can be executed across different platforms

2️⃣ Extensive and well <b>documented standard library</b>

3️⃣ “Cleaner” in syntax, <b>less low-level functionality</b>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a Run Cycle?

A

The process of <b>Writing, Compiling and Executing</b> a program.

1️⃣ Writing: Source code - Hello.java

2️⃣ Compiling:
javac Hello.java ➡️ <b style="font-size:130%;">Compiled binary</b> has .class extension

The binary is also known as <b>Java Executable Bytecode</b>

3️⃣ Executing:
Run on a <b>Java Virtual Machine (JVM)</b>
java Hello

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What do you mean “Compile Once, Run Anywhere”?

A

Normal executable are tied to a specific platform (OS + Hardware)

Java overcomes this by running the executable on an <b>uniform hardware environment - JVMt</b> simulated by software.

Only need a specific JVM for a particular platform to execute all Java Bytecodes <b>without recompilation</b>.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to call the Library in Java?

A

Package.

To use a predefined library, you need to “import”

Packages under <b>“java.lang” are imported by default</b>.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

One source code = Once class?

A

NO.
Each source code file contains <b>one or more classes</b>.

Each class will be compiled into a <b>separate XXXX.class bytecode</b>.

So if there are TWO classes in ONE source code, then there will be TWO classes after complie.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Identifier

A

It is a name that we associate with program entity.

Cannot begin with a digit.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Variable

A

It is used to store data in a program.

It must be declared with a <b>specific data type</b>.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the <b>EIGHT primitive data types</b> in Java?

A

byte, short, int, long, float, double

boolean, char

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

X is double. Y is int.

If X + Y, what happen to the data type?

A

When operands of an operation have differing types:

if one of the operands is double, convert the other to double.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Widening

A

When value is assigned to a variable of differing types:

Value has a smaller range compared to the variable
➡️ <b>Converted automatically</b>

e.g. Value 3 -> int
Variable X -> long
✅ X = 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Narrowing

A

When value is assigned to a variable of differing types:

Value has a <b>larger range</b> compared to the variable
➡️ Explicit <b>type casting</b> is needed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Type casting

A
eg. 
double d;
int i;
d = 3.14159;
i = (int) d;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to use “switch”?

A
String str = "two";
switch(str) 
{ 
  case "one": 
     System.out.println("one"); 
      break; 
  case "two": 
     System.out.println("two"); 
     break;
  default: 
     System.out.println("no match"); 
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the logical operators ‘^’?

A

Exclusive-Or

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Static method

A

??

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Parameter Passing - <b>Passed by Value</b>

A

All parameters in Java are passed by value:

A <b>copy of the actual argument</b> is created upon method invocation.

The <b>method parameter</b> and its corresponding <b>actual parameter</b> are <b>two independent variable</b>.

17
Q

How to initialise an Array?

A

1️⃣ int arrayRef = new int [3]
arrayRef [0] = 100;

2️⃣ int[ ] arrayRef = {100, 62, 88};

18
Q

Array is passed “<b>As a parameter</b>”.

A

Array is passed <b>As the reference</b> into a method.

Any modification of the element in the method will <b>affect the actual array</b>.

19
Q

Array: As a return type

A

Array can be returned from a method.