Curso Java desde cero #2 | Indentado, Compilación y Ejecución del código
Introduction to Java Programming
Course Overview
- The video welcomes viewers to the second part of a Java programming course, providing a link in the description for newcomers to access the first part.
- Viewers are instructed to open both Notepad and Command Prompt, tools previously covered in the last video.
Understanding Classes in Java
- A class in Java is defined as a template where code is written; it serves as a structure for creating programs.
- Emphasis on reading code from top to bottom and left to right, which is crucial for understanding how computers interpret code.
Writing Your First Class
- The first line of code must declare the class name; this example uses "public class" followed by the chosen name (e.g., "Ernesto").
- After naming the class, an opening brace
should be added, highlighting that every opening brace must have a corresponding closing brace.
Structuring Code Properly
Indentation and Readability
- The importance of indentation is discussed; it helps maintain clean and readable code.
- The main method (
public static void main(String[] args)) is introduced as essential for starting any Java program.
Implementing Indentation
- Indentation allows programmers to create visually appealing and organized code. It involves adding spaces after opening braces.
- When writing subsequent lines of code after an opening brace, two spaces should be added for proper indentation.
Creating the Main Method
Syntax of Main Method
- The syntax for declaring the main method includes specific keywords:
public,static,void, followed bymainwith parameters(String[] args)enclosed in parentheses.
- After writing this line, another opening brace `` should follow, indicating where the main method's body begins.
Understanding the Java Main Method and Program Structure
The Importance of the Main Method
- The
Mainmethod is essential for starting a Java program; without it, the code cannot be executed.
- The class name and
Mainmethod are defined to identify and initiate the program's execution.
Writing Instructions in Java
- Indentation in Java begins after the nearest opening brace, which helps structure the code visually.
- To print messages to the screen, use
System.out.printLn()with text enclosed in double quotes.
Syntax Rules for Java Code
- Each instruction must end with a semicolon (
;) to signify completion.
- Text strings must always be enclosed in quotes when outputting messages.
Creating Your First Program
- A common first message printed in programming is "Hello World," serving as a standard introduction to coding.
- After writing your code, it's crucial to save your work to prevent data loss.
Saving and Naming Your Java File
- Save your file with a
.txtextension initially but remember that this does not make it a runnable Java program yet.
- To create an executable Java file, save it with a
.javaextension using the same name as your class (e.g.,Ernesto.java).
Navigating Windows Command Line
- Understanding how to navigate through directories using command line commands like
diris vital for accessing files on your system.
How to Compile and Run a Java Program
Navigating the Desktop and Listing Files
- The process begins by accessing the desktop folder, where the command
diris used to list all files present.
- The user identifies two key files: a Java file named
Ernesto.javaand a text file containing code snippets calledpráctica 1.txt.
Understanding Java Compilation
- It’s crucial to note that Java requires a compiler for communication; this compiler is known as
javac.
- The compilation process translates human-readable code into bytecode, which can be understood by the computer.
Compiling the Java File
- To compile, the command
javac Ernesto.javais executed. If successful, it generates an additional file namedErnesto.class.
- In case of errors during compilation, it indicates issues in the written code that need correction before proceeding.
Executing the Compiled Program
- Once compiled without errors, execution of the program is done using the command
java Ernesto, which runs the program.
- The output of this simple program displays "Hola mundo," confirming successful execution.
Troubleshooting Compilation Errors
- If errors occur during compilation, users should compare their code with provided examples to identify discrepancies.
- After correcting any mistakes, recompile using
javac Ernesto.java, followed by executing withjava Ernestoto see results.
Conclusion and Encouragement