Java Fundamentals

JVM and Conditional Statements

Lecture 4

Java Virtual Machine, compilation, execution, and control flow

Summary of the last lecture

  • We began to see how to use Eclipse
  • We programmed our first Java classes
How Java handles the produced code execution?

Java environment: the JVM

  • What is a Virtual Machine?
    A virtual machine is a software engine, that can handle the execution of instructions, like an operating system, to ensure the program global execution.
    It is like a (virtual) independent operating system, running on the real operating system

Java environment: the JVM (2)

Pro and cons of a Virtual Machine system
  • Pro : Code portability the main advantage of using a virtual machine system, is to be portable. When you write your code, you don't have to worry about OS specific operations: the JVM makes the abstraction, and you can focus on your business code.
  • Con : Performance issues the major disadvantage is that the JVM adds a layer to perform the translation of the java code in the OS native instructions. You can have some performance problems in some specific cases. ▸ less sensible since Java 1.6

Java environment: the JVM (3)

From design to compilation

From compiled classes to execution

Summary

  • Java files .java are compiled into .class
  • .class files are interpreted by the JVM into native OS operations
  • The JDK allows us to develop and compile java code. The JDK furnishes a lot of integrated tools to help the Java developer
  • The JRE can only run already compiled java code: .class files
  • The JDK contains the JRE, which itself contains the JVM

Compilation and Execution in IntelliJ IDEA (1)

IntelliJ IDEA handles compilation automatically:
  • IntelliJ builds your project automatically as you type
  • Compilation output is located in the out/ folder
  • You can manually trigger a build with Ctrl+F9 (Cmd+F9 on Mac)
  • Behind the scenes, IntelliJ uses javac (Java compiler):
    javac Identity.java

Compilation and Execution in IntelliJ IDEA (2)

IntelliJ project configuration:
  • File > Project Structure to configure SDK and dependencies
  • Project SDK: Choose your Java version (Java 17+ recommended)
  • Language Level: Select Java language features to use
  • Libraries: Add external JAR files if needed
IntelliJ intelligently manages your classpath and build configuration

Compilation and Execution in IntelliJ IDEA (3)

Running and debugging your code:
  • Run: Click the green play button next to your main method, or press Shift+F10
  • Debug: Click the debug icon (bug symbol) or press Shift+F9
  • IntelliJ automatically detects classes with main methods
  • Behind the scenes, it executes the java command:
    java Identity

Your First Executable Program

In order to be executable, your class must have a static main method
Type main and press Tab in IntelliJ - it will auto-complete!
package 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");
     }
}
        

Console applications : in and out

  • Getting input from the user :
    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");
    }
                
  • Printing in the console
    
    System.out.println("Say something in the console");
                

Exercise: Bank Account System

  • Initialize programmatically a customer, an account, each of them with concrete values.
  • Perform a withdrawal, and a interests calculation,
  • then display the customer and account values (name and balance in the standard output)

Exercise 2: Bank Account System, user input

In the former exercise, replace the programmatic initialization by a user input

Conditional statements in Java : Tests

Conditional statements in Java : Switches

Exercise 3: Calculate the perimeter and the area of different figures

Create methods which get the appropriated parameters to calculate area and perimeter of :
And display those results in the standard output

Slide Overview