Recap 03062025
Recap 03062025
HelloWorld
Naming convention:
*Variables:
case-sensitive
technically, variable names can begin with: letter | $ | _. But according to convention, always begin
variable names with a letter, not $, _
convention: use full words instead of cryptic abbr when naming variables
*Packages:
3/6/2025 1
prefix of package name is written in all-lowercase, should be top-level domain names: com, edu,
gov,...
*Classes:
avoid acronyms and abbrs (unless some widely used ones: HTML, URL)
*Interfaces:
*Methods:
should be verbs, in mixed case with 1st letter lowercase, with 1st letter of each internal word
capitalized
*Constants:
2. break:
- exit a for, while, do while, or to mark the end of a case block in switch statement
- it always exits the innermost enclosing while, for, do, switch
3. if
- indicates conditional execution of a block
- the condition must be: boolean value
- may have optional else clause
4. const, goto:
- reversed, although they are not currently used
5. public
- an access control modifier
- public class, method, field can be referenced from any other class or package
6. protected
3/6/2025 2
- another access control modifier
7. private
Operators
arithmetic operators: + (can also be used for concatenating 2 strings together), -, *, /, %
Demo operator:
3/6/2025 3
System.out.println(success);// false
System.out.println(!success);// true
int i = 3;
i++;
System.out.println(i);// prints 4
++i;
System.out.println(i); // prints 5
System.out.println(++i);// prints 6
System.out.println(i++);// prints 6
System.out.println(i); // prints 7
//demo equality, relational operators: ==, !=, >, >=, <, <=
int value1 = 1;
int value2 = 2;
if(value1 == value2)
System.out.println("value1 == value2"); //false
if(value1 != value2)
System.out.println("value1 != value2"); //true
if(value1 > value2)
System.out.println("value1 > value2"); //false
if(value1 < value2)
System.out.println("value1 < value2"); //true
if(value1 <= value2)
System.out.println("value1 <= value2"); //true
3/6/2025 4
//demo bitwise operators: &, ^, |, ~
System.out.println(0x000F & 0x2222); //2
System.out.println(0x000F | 0x2222); //8751
System.out.println(0x000F ^ 0x2222); //8749
System.out.println(~0x000F); //-16
}
}
+while: evaluate expression first, then deciding whether to execute the statement in while block or not
based on the value of expression. Continuing this process until the expression evaluates to false
+do-while: the statements within do block are always executed at least once, then evaluating
expression
3/6/2025 5
Exercise 4:
import java.util.Scanner;
3.4. Modifiers
access modifiers for class: public, none
abstract:
static:
3/6/2025 6
methods: can be accessed via class name without needing to instantiate an instance
final:
synchronized:
methods: ensure that only one thread can execute the method at a time
native:
Reflection:
import java.lang.annotation.*;
3/6/2025 7
@MyAnnotation(value = "Hello from method2")
public void method2() {
System.out.println("Executing method2");
}
import java.lang.reflect.Method;
Result:
3/6/2025 8