Java environment, compilation, execution | logic and operations in Java
.java
are compiled into .class
.class
files are interpreted by the JVM into native OS operations.class
filesjavac
command line tooljavac Identity.java
java
command line tooljava Identity.java
static main
methodpackage fr.tbr.exercises;
public class JavaSyntaxDemo {
private String demoVersion = "1.0_DEV";
//Constructor
public JavaSyntaxDemo() {
}
public String getDemoVersion(){
return this.demoVersion;
}
public static void main(String[] args){
System.out.println("=> Begin execution");
JavaSyntaxDemo demo = new JavaSyntaxDemo();
System.out.println("Working with version : " + demo.getDemoVersion());
System.out.println("<= Exit");
}
}
in
and out
Scanner scanner = new Scanner(System.in);
try { //this is a try-catch block, we will discuss it further
int num1 = scanner.nextInt();
System.out.println("Input 1 accepted");
int num2 = scanner.nextInt();
System.out.println("Input 2 accepted");
} catch (InputMismatchException e) {
System.out.println("Invalid Entry");
}
System.out.println("Say something in the console");
if(...){ }else{ }
statementif
parenthesis, must be a boolean// A boolean to do the demo
final boolean tester = true;
final boolean anotherTester = false;
/* The if instruction is followed by a parenthesis block, allowing to
* put the condition. If the condition is matched, then the curly
* bracket is executed. Else the "else" block is performed */
if (tester) {
// do something
} else {
// do something else
}
switch - case
statement
int toBeTested = 2;
// Switches can be based on integer values, chars, or enums. Since java
// 7, it can even be based on Strings
switch (toBeTested) {
case 1:
// treat case one;
break; // put this break so the case stops
case 2:
// in this example, this the case we pass into
break;
default:
// this is the instruction to perform if no cases were satisfied
break;
}