Getting Started with the IDE and main methods
Learning Objectives
Why Java and How It Differs from JavaScript
Before diving in, let’s set the stage. Java and other programming languages such as JavaScript share some conceptual similarities—e.g. the use of loops, conditionals, and functions—but Java has its own syntax, type systems, and execution models. Java is a statically typed, compiled language, which means you’ll encounter concepts like compilation errors and method signatures that don’t exist in scripting languages like JavaScript. This sprint will help you make that mental shift.
Overview
An IDE (Integrated Development Environment) is your main tool for Java development. It simplifies tasks like writing code, compiling, debugging, and managing dependencies. Every Java application starts with a main method. Understanding its role and how methods are structured is key to writing functional programs. The main method is the entry point of every Java application.
Self Study
As you read through the resources below try to answer the following questions:
- You’ve probably used an IDE before when coding in other languages - what makes an IDE particularly useful when coding in Java?
- What is a Java main method for?
- What are some differences between how a program is run in Java vs other languages you’ve seen before?
Reading material
- Introduction to Java
- Interpreted vs Compiled Programming Languages
- Getting started with IntelliJ IDEA
- Top 5 reasons you need an IDE
- Java main() Method Explained
✍️Exercise 1.1
Goal: Get comfortable with IntelliJ and running Java code.
- Install IntelliJ IDEA and create a new Java project.
- Create a main method and add logic so that it prints: “Hello, Java!”
- You’ll need to Google for how to print to the console in Java!
- Run the program from the IDE.
Reflections
Think about the following questions, make notes and be prepared to talk through your thoughts on the Saturday.
- Share three useful Intellij keyboard shortcuts
- Getting familiar with keyboard shortcuts will save you lots of time and improve your flow when writing code - it’s well worth taking the time to learn!
- If you rename your main method will it still run?
- What do the keywords public, static and void mean?
Java keywords and syntax
Learning Objectives
Java has strict syntax rules and reserved keywords that define its structure. Understanding these early will prevent common errors.
Compilation is the process of converting the Java code you write into a format that your computer can execute. Compile time is when Java checks your code for errors before running your program, which is different from languages like Python or JavaScript that check for errors while the program is actually running. This provides us with the significant benefit of being able to spot (some!) errors without running the code. It also means Java IDEs come with a set of powerful tools to aid development.
Java has two types of data type: primitive and reference. Without going into too much detail right now, primitive types are usually much faster for the computer to deal with and should be used in situations where performance is important. Moreover, the primitive types are the basic ‘building blocks’ of Java and all reference types are ultimately made up of primitive types. You can identify primitive types because they are always in lowercase, unlike reference types which are in UpperCamelCase. The primitive types are defined by the Java language itself and we cannot create any new ones ourselves or change any existing ones. We’ll focus on the following primitive types for now:
- int
- long
- double
- boolean
- char Java is a statically typed language, which means that variable types are known at compile time. As such, the type must be provided by the programmer when writing the code. For example, if you are to define a string, you must explicitly state that the variable is of type string:
String myVariable = "hello world";Self Study
As you read through the resources below try to answer the following questions:
- What is a keyword in java?
- Choose 3 keywords and explain what they represent
- What is meant by the term ‘primitive type’?
Reading material
- Dynamic vs. static typing
- Java keywords with explanations
- Java Primitives
- Java For Loop
- Decision Making in Java - Conditional Statements
✍️Exercise 2.1
Goal: Practice basic syntax and keywords.
Create a program that:
- Declares variables of different primitive types (int, double, boolean, char).
- Prints their values to the console.
- Add comments explaining what each keyword does (e.g., public, static, void).
✍️Exercise 2.2
Goal: Identify and fix compilation errors.
- Remove a semicolon or misspell a keyword in your code.
- Observe the compilation error in IntelliJ.
- Fix the error and re-run the program
✍️Exercise 2.3
Goal: Compare Java control flow with JavaScript.
Write a program that:
- Uses a for loop to print numbers 1 to 10.
- Uses an if-else statement to check if a number is even or odd.
Bonus: Rewrite the same logic in JavaScript and note the differences in syntax.
✍️Exercise 2.4
- Write a program that uses a switch statement to print the name of the day based on an integer (1 = Monday, etc.).
- Explain why switch in Java differs from JavaScript (e.g. type safety).
Reflections
Think about the following questions, make notes and be prepared to talk through your thoughts in the workshop.
- List the primitive types you’ve seen and explain what they represent
- What is an enhanced for loop in Java? Would an enhanced for loop be useful for this exercise? Why? When would an enhanced for loop be useful?
- Did you encounter any compilation errors when writing your code? How did you fix them? Did the IDE help?
Method Signatures
Learning Objectives
Understanding method signatures is essential for ensuring the code is readable & maintainable. When writing code, we should expect that someone else will need to understand and adapt it - we work in teams, not in isolation. Therefore, it’s important to write code that is clear and understandable. It’s important when writing Java code to document classes and methods so others understand the intended purpose of the code, this can been done with JavaDoc - have a read through how to format different types of JavaDoc.
Self Study
As you read through the resources below try to answer the following questions:
- What are the components of a method signature? Are any of them optional?
- What is Javadoc? Who is it for and why is it useful?
Reading material
✍️Exercise 3.1
Goal: Write a method signature which takes parameters.
- Create a private method called greetUser that:
- Accepts a String name as a parameter.
- Returns a greeting message like “Hello, Alice!”.
- Call this method from a main method and print the result.
- Explain each part of the method signature for the name method.
✍️Exercise 3.2
- Create a method calculateSum that takes two integers and returns their sum.
- Add JavaDoc comments explaining the method.
Reflections
Think about the following questions, make notes and be prepared to talk through your thoughts in the workshop.
- Why are private methods useful? Why not just have everything in the main method?
- Javadoc should describe the why not the what of your code - why?
Debugging Java Code Using the IDE
Learning Objectives
Debugging is an essential skill for any developer. IntelliJ provides powerful tools to help you find and fix issues quickly. In your IDE, there are two ways to run a program:
- Run
- Debug
If you use the debug option, you will be able to set breakpoints at which the program will pause execution. When the program stops on a breakpoint, you can:
- View the value of all variables that are in scope
- View the current execution stack
- Evaluate a specific piece of code
- Step line-by-line through the code
When stepping through code, you can:
- Step over: steps over the next single line
- Step into: step into a method that is being invoked on the current line
- Run to cursor: continues execution until it reaches the cursor
- Evaluate: evaluates a selected portion of code. You can also write arbitrary code to evaluate.
Self Study
As you read through the resources below try to answer the following questions:
- What are some Intellij shortcuts to help with debugging?
- How does debugging in Java compare to debugging you’ve seen in other languages?
Reading material
- Introduction to debugging in Java
- Debugging a Java application with IntelliJ
- Understanding compilation
✍️Exercise 4.1
Goal: Learn to use breakpoints and inspect variables.
- Write a program that calculates the sum of numbers from 1 to 100 and prints the result
- Introduce a bug (e.g., start the loop at -1 instead of 1).
- Use IntelliJ’s debugger to:
- Set a breakpoint inside the loop.
- Inspect the value of the sum variable and the result variable, seeing how they change as you step through.
- Step through the code to find the error.
Reflections
Think about the following questions, make notes and be prepared to talk through your thoughts in the workshop.
- How might you have found the issue if you didn’t have a debugger?
- What was the process you followed when debugging code? e.g. how did you decide where to put breakpoints?
Prepare for the day
Read the day plan and make sure you are ready to start the day. What will you be working on? What does the workshop cover and expect you to know already? Professionals prepare for their work, so take a moment to get ready.
- You have read the day plan for this sprint
- You have covered the prerequisites for any workshop that has been planned
- You have started your backlog tasks
- You have organised childcare if you need it
- You know how to get to class
- You have read and understood the success criteria