#3 First Code in Java
Setting up the Environment
In this section, we will set up the development environment and write our first Java code using VS Code.
Opening VS Code
- Open VS Code by typing "code" in the terminal.
- Choose a color theme preference (dark is recommended for development).
Exploring VS Code
- The left-hand side of VS Code displays folders and files.
- Use the Explorer to navigate through files and folders.
- Search for specific items using the search option.
- Extensions can be added later but are not necessary at this point.
Creating a Project Folder
- Click on "Open Folder" to create a new project folder.
- Save the folder on your desktop or preferred location.
- Zoom in to view everything clearly within VS Code.
Configuring Preferences
- Uncheck the option to show welcome screen on startup.
- Close the welcome page.
Creating the First File
- Click on "New File" to create a new file in the project folder.
- Name the file as "Hello.java" (Java files have a .java extension).
Writing Our First Java Code
In this section, we will write our first Java code that prints "Hello World".
Checking JDK Version
java version
javac -version
- Verify that JDK is installed and check its version.
Writing Java Code in VS Code
public class Hello
public static void main(String[] args)
System.out.println("Hello World");
- Write a simple Java program that prints "Hello World".
Introduction to JShell
In this section, the speaker introduces JShell, a feature introduced in Java 9 that allows for experimenting and testing code. JShell provides a prompt where code can be written and executed.
Using JShell for Code Experimentation
- JShell is a concept introduced in Java 9.
- It allows for writing and executing code in one line.
- The speaker demonstrates how to open the terminal and access the JShell prompt.
- With JShell, you can experiment with code and quickly see the output.
- Examples of simple operations like addition and subtraction are shown using JShell.
Working with Methods in Java
This section focuses on methods in Java and their importance when performing actions or tasks. The speaker explains that methods are used to solve problems or perform specific actions.
Understanding Methods in Java
- Methods are used to perform actions or solve problems in Java.
- When working with methods, values need to be passed as arguments.
- The speaker introduces the
printmethod, an built-in method in Java used for printing output.
- To use the
printmethod, you need to specify what should be printed within parentheses after calling the method.
- A semicolon needs to be added at the end of each statement in Java.
Printing Output with System.out.println()
This section covers how to print output using System.out.println() statement. The speaker explains that this statement is used to print text or values on the console.
Printing Text and Values
- To print something on the console, use
System.out.println().
- Within parentheses after
println(), specify what should be printed (e.g., text or values).
- Use double quotes when printing text. For example:
"Hello World".
- Numbers can be printed without quotes.
- The
System.out.println()statement is essential for printing output in Java.
Limitations of JShell and Compiling Java Files
This section discusses the limitations of using JShell for building projects and introduces the process of compiling Java files. The speaker explains that JShell is primarily used for experimentation, while Java files are used for building applications.
Limitations of JShell
- JShell is not suitable for building projects or writing code in Java files.
- When working with Java files, a different approach is required.
- To compile a Java file, use the
javaccommand followed by the file name.
- Compilation errors may occur if there are syntax or logical issues in the code.
Conclusion
In this final section, the speaker concludes by summarizing the key points discussed throughout the video.
Key Takeaways
- JShell is a useful tool for experimenting and testing code in Java.
- Methods play a crucial role in performing actions and solving problems in Java.
- Printing output can be done using
System.out.println()statement.
- JShell has limitations when it comes to building projects; instead, Java files should be used.
- Compiling a Java file involves using the
javaccommand followed by the file name.
The transcript provided does not cover all parts of the video.