-
Hello WorldJava 2022. 1. 30. 17:58
Java언어에 대한 기본 문법 및 개념을 정리하였습니다.
< Introduction to Java >
Programming languages enable humans to write instructions that a computer can perform.
Java is known for being simple, portable, secure, and robust. Though it was released over twenty years ago, Java remains one of the most popular programming languages today.
One reason people love Java is the Java Virtual Machine, which ensures the same Java code can be run on different operating systems and platforms. Sun Microsystems’ slogan for Java was “write once, run everywhere”.
< Hello Java File! >
Java files have a .java extension.
Each file has one primary class named after the file and main( ) method. Class word is capitalized.- Each class represents one real-world idea.
- The main( ) method runs the tasks of the program.
Example : HelloWorld.js file
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } }
1. Our class name: HelloWorld and our file name: HelloWorld.
2. We marked the domain of the HelloWorld class using curly braces: { }. Syntax inside the curly braces is part of the class.
3. Inside the class we had a main( ) method which lists our program tasks
4. String[ ] args is a placeholder for information we want to pass into our program.
5. System.out.println("Hello World"); displayed the text "Hello World" on the screen.
< Print Statements >
Print statements output information to the screen (also referred to as the output terminal).
< Syntax >
System.out.println("Hello World");
- System is a built-in Java class that contains useful tools for our programs.
- out is short for “output”.
- println is short for “print line”.
We can use the System.out.println( ) whenever we want the program to create a new line on the screen after outputting a value :
System.out.println("Hello World"); System.out.println("Today is a great day to code!"); // Hello World // Today is a great day to code!
We also can output information using System.out.print( ). Unlike System.out.println( ), this type of print statement outputs everything on the same line :
System.out.print("Hello "); System.out.print("World"); // Hello World
< Commenting Code >
Want our intentions to be clear to humans using comments.
These comments are not executed, so there’s no need for valid syntax within a comment.< Syntax >
When comments are short, use the single-line syntax: //
// short comment
When comments are long, use the multi-line syntax: /* and */
/* Loooong Comments Loooong Comments Loooong Comments */
Another type of commenting option is the Javadoc comment : /** and *//** * The following class accomplishes the following task... */
- Javadoc comments are used to create documentation for APIs (Application Programming Interfaces).
- When writing Javadoc comments, remember that they will eventually be used in the documentation that your users might read, so make sure to be especially thoughtful when writing these comments.Example : Javadoc comments
/** * The following class shows what a comment would look like in a program. */ public class CommentExample { // I'm a comment inside the class public static void main(String[] args) { // I'm a comment inside a method System.out.println("This program has comments!"); } }
: Javadoc comments are typically written before the declaration of fields, methods, and classes
< >
Reading code is just as important as writing code.
To make code easier for people to read, use Semicolon and Whitespace.- Java does not interpret whitespace, the areas of the code without syntax, but humans use whitespace to read code without difficulty.
- Java does interpret semicolons. Semicolons are used to mark the end of a statement, one line of code that performs a single task.
Example : these two code samples are identical
System.out.println("Java");System.out.println("Lava");System.out.println("Guava");
System.out.println("Java"); System.out.println("Lava"); System.out.println("Guava");
+. Note : Let’s contrast statements with the curly brace, { }. Curly braces mark the scope of our classes and methods. There are no semicolons at the end of a curly brace.
< Compilation: Catching Errors >
Java is a compiled programming language, meaning the code we write in a .java file is transformed into byte code by a compiler before it is executed by the Java Virtual Machine on your computer.
A compiler is a program that translates human-friendly programming languages into other programming languages that computers can execute.- The compiling process catches mistakes before the computer runs our code.
- The Java compiler runs a series of checks while it transforms the code. Code that does not pass these checks will not be compiled.
- This exercise use an interactive terminal.
Example : compile Plankton.java file.
1. compile it with the terminal command:
javac Plankton.java
2-1. A successful compilation produces a .class file: Plankton.class, that we execute with the terminal command:
java Plankton
2-2. An unsuccessful compilation produces a list of errors. No .class file is made until the errors are corrected and the compile command is run again.
< Compilation: Creating Executables >
If the file compiles successfully, compile command produces an executable class: FileName.class.
Executable means we can run this program from the terminal.- We run the executable with the command:
java FileName
Example : compile Welcome.java and execute Welcome class
1. Welcome.java file
// within the file: Welcome.java public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Codecademy's Java course!"); } }
2. compile Welcome.java file
javac Welcome.java
now, we have two files:
- Welcome.java, our original file with Java syntax.
- Welcome.class, our compiled file with Java bytecode, ready to be executed by the Java Virtual Machine.
3. execute the compiled class
java Welcome
then, print to the screen:
Welcome to Codecademy's Java course!
'Java' 카테고리의 다른 글
Class : Method (0) 2022.02.04 Class : Introduction (0) 2022.02.03 Variables : Manipulating (0) 2022.02.01 Variables (0) 2022.01.31 IDE (0) 2022.01.30