Introduction to Java Flashcards
What are the selling points of Java?
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>
What is a Run Cycle?
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
What do you mean “Compile Once, Run Anywhere”?
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 to call the Library in Java?
Package.
To use a predefined library, you need to “import”
Packages under <b>“java.lang” are imported by default</b>.
One source code = Once class?
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.
Identifier
It is a name that we associate with program entity.
Cannot begin with a digit.
Variable
It is used to store data in a program.
It must be declared with a <b>specific data type</b>.
What are the <b>EIGHT primitive data types</b> in Java?
byte, short, int, long, float, double
boolean, char
X is double. Y is int.
If X + Y, what happen to the data type?
When operands of an operation have differing types:
if one of the operands is double, convert the other to double.
Widening
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
Narrowing
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
Type casting
eg. double d; int i; d = 3.14159; i = (int) d;
How to use “switch”?
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"); }
What is the logical operators ‘^’?
Exclusive-Or
Static method
??
Parameter Passing - <b>Passed by Value</b>
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>.
How to initialise an Array?
1️⃣ int arrayRef = new int [3]
arrayRef [0] = 100;
2️⃣ int[ ] arrayRef = {100, 62, 88};
Array is passed “<b>As a parameter</b>”.
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>.
Array: As a return type
Array can be returned from a method.