Lecture1b- Introduction to Java Flashcards
What are the three levels of programming languages?
1️⃣ Machine Language – Binary instructions the CPU understands.
2️⃣ Assembly Language – Unique to a specific computer
3️⃣ High-Level Language – Uses human-readable syntax, translated into machine code.
What are key features of Java?
✅ High-level – Easy to read and write.
✅ Object-oriented – Everything is an object.
✅ Platform-independent – Runs on multiple systems.
✅ Multithreaded – Can handle multiple tasks at once.
✅ Secure – Designed for safety.
✅ Distributed – Used in network-based applications.
Java is NOT JavaScript!
What are the main components of the Java ecosystem?
✔ Java SE (Standard Edition) – Core Java features.
✔ Java EE (Enterprise Edition) – Used for large-scale applications.
✔ Java ME (Micro Edition) – Used in embedded systems.
🔹 Java Runtime Environment (JRE) – Runs Java programs.
🔹 Java Development Kit (JDK) – Includes JRE + compilers/debuggers.
🔹 Java Virtual Machine (JVM) – Translates bytecode to machine code.
What is object-oriented programming?
- a style of programming focused on the design of creation of virtual objects
- can be thought of as solving a problem by modeling it as software objects
- objects are the things being modeled
How does Java execute a program?
1️⃣ Write code → Save as .java file.
2️⃣ Compile → Use javac filename.java (produces .class file).
3️⃣ Run → Execute with java filename (no .class extension needed).
Java compiles to bytecode, which the JVM interprets.
What is the difference between syntax and semantics?
✔ Syntax – Rules for writing valid Java code (e.g., ; at the end of statements).
✔ Semantics – The meaning of the code (e.g., 5 + 5 makes sense, but “Java is blue” does not).
What are the three types of errors in Java?
1️⃣ Syntax Errors – Violates Java’s rules (e.g., missing ;).
2️⃣ Runtime Errors – Occurs during execution (e.g., division by zero).
3️⃣ Logic Errors – Wrong program behavior (e.g., using + instead of -).
What are Java’s naming conventions?
✔ Classes: TitleCase → MyClass
✔ Methods & Variables: camelCase → myVariable
✔ Constants: ALL_CAPS → MAX_VALUE
Java is case-sensitive: total, Total, and TOTAL are different!
What are the main components of a Java program?
✔ Classes – Define objects and methods.
✔ Methods – Contain code logic (main() method is required).
✔ Statements – Executable commands.
Example Java Program:
```java
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello, World!”);
}
}
~~~
What does public static void main(String[] args) mean?
✔ public – Accessible anywhere.
✔ static – No need to create an object.
✔ void – No return value.
✔ main – Java’s entry point.
✔ (String[] args) – Takes command-line arguments.
Must be written exactly like this or Java won’t run the program!
What are the different types of comments in Java?
✔ Single-line: // This is a comment
✔ Multi-line: /* This is a multi-line comment */
✔ Javadoc: /** Used for documentation */
Comments improve code readability but don’t affect execution.
Why is white space important in Java?
✔ Improves readability.
✔ Java ignores extra spaces, tabs, and line breaks.
✔ Use consistent indentation (2-4 spaces per level).
Example:
```java
if (x > 0) {
System.out.println(“Positive number”);
}
~~~
How does Java handle the + operator?
✔ Numbers: Performs addition → 5 + 5 = 10
✔ Strings: Performs concatenation → “Hello “ + “World” → “Hello World”
Order matters! Example:
```java
System.out.println(“24 and 45 concatenated: “ + 24 + 45); // “24 and 45”
System.out.println(“24 and 45 added: “ + (24 + 45)); // “24 and 45 added: 69”
~~~
What are escape sequences in Java?
Special characters written with \.
Sequence Meaning
\b Backspace
\t Tab
\n Newline
\r Carriage return
"\tDouble quote
'\tSingle quote
\\tBackslash
Example:
```java
System.out.println(“I said "Hello" to you.”); // Output: I said “Hello” to you.
~~~
What does “Write Once, Run Anywhere” (WORA) mean in Java?
Java programs do not depend on a specific operating system or hardware.
Compiled Java code (bytecode) runs on any JVM, making Java platform-independent.
Unlike C/C++, Java does not need separate compilation for different OS types.
Java’s portability is a major advantage!
How do you compile and run a Java program using the command line?
✔ Compiling:
```sh
javac MyProgram.java
~~~
✔ Running the compiled class file:
```sh
java MyProgram
~~~
Do NOT include .class when running the program!
What are the two main types of data in Java?
✔ Primitive Data Types:
Directly store values (int, double, char, boolean, etc.).
✔ Reference Data Types:
Store references to objects (String, Scanner, Arrays, etc.).
Example:
```java
int a = 10; // Primitive type (stores 10 directly)
String s = “Hello”; // Reference type (stores memory location of “Hello”)
~~~
What is Java bytecode?
The intermediate form called bytecode that is compiled into class files.
✔ Bytecode is not machine code but runs on the JVM (Java Virtual Machine).
✔ This makes Java platform-independent, as any device with a JVM can run Java code.
Unlike compiled C/C++ programs, Java bytecode is interpreted at runtime by the JVM.
What is printf() in Java, and how is it used?
It formats output precisely, unlike println().
✔ Example – Formatting Decimal Places:
```java
double price = 12.997;
System.out.printf(“Total price: $%.2f\n”, price);
~~~
Output:
Total price: $12.99
✔ Common Format Specifiers:
Format Code Meaning Example Output
%d Integer 5
%f Floating-point 3.1416
%s String “Java”
%c Character ‘A’
Use printf() when precision matters (e.g., displaying money values).
what are the valid ways to declare main string?
write “public static “ or “static public” – both valid
* We can declare “String[]” in any valid form:
main(String[] args)
main(String… args)
main(String []args)
main(String args[])
What is the order of the compilation process?
- Java Source Code (Program.java)
- Java Compiler (Program.class)
- Translation into bytecode
- Send to Java Virtual Machine (JVM)
- Run Program in OS + Hardware
What is the order of the programming flowchart?
- edit and save program
- compile program
- execute program and evaluate results
- if there are any errors in any step of the process, go back to step 1
what is the main method to memorize?
public static void main(String[] args)
static public also acceptable