0% found this document useful (0 votes)
4 views3 pages

Java Basics: Print, Comments, Classes

This document provides a cheatsheet for learning Java, covering basic concepts such as printing to the console, comments, compiling Java programs, whitespace handling, statements, the main() method, and class structure. It explains how to use System.out.println() for output, the significance of comments for readability, and the compilation process into byte code for execution by the JVM. Additionally, it emphasizes the necessity of a main() method in Java applications and the requirement for class names to match the program filename.

Uploaded by

infix.delete0i
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Java Basics: Print, Comments, Classes

This document provides a cheatsheet for learning Java, covering basic concepts such as printing to the console, comments, compiling Java programs, whitespace handling, statements, the main() method, and class structure. It explains how to use System.out.println() for output, the significance of comments for readability, and the compilation process into byte code for execution by the JVM. Additionally, it emphasizes the necessity of a main() method in Java applications and the requirement for class names to match the program filename.

Uploaded by

infix.delete0i
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Cheatsheets / Learn Java

Hello World

Print Line

[Link]() can print to the [Link]("Hello, world!");


console:
// Output: Hello, world!
System is a class from the core library
provided by Java
out is an object that controls the output
println() is a method associated with
that object that receives a single argument

Comments

Comments are bits of text that are ignored by the // I am a single line comment!
compiler. They are used to increase the readability of a
program.
Single line comments are created by using // . /*
Multi-line comments are created by starting And I am a
with /* and ending with */ .
multi-line comment!
*/

Compiling Java

In Java, when we compile a program, each individual # Compile the class file:
class is converted into a .class file, which is known as
javac [Link]
byte code.
The JVM (Java virtual machine) is used to run the byte
code. # Execute the compiled file:
java hello
Whitespace

Whitespace, including spaces and newlines, between [Link]("Example of a


statements is ignored.
statement");

[Link]("Another statement");

// Output:
// Example of a statement
// Another statement

Statements

In Java, a statement is a line of code that executes a [Link]("Java Programming


task and is terminated with a ; .
☕️ ");

main() Method

In Java, every application must contain a main() public class Person {


method, which is the entry point for the application. All
other methods are invoked from the main()
method. public static void main(String[] args)
The signature of the method is public static {
void main(String[] args) { } . It
accepts a single argument: an array of elements of type
[Link]("Hello, world!");
String .

}
Classes

A class represents a single concept. public class Person {


A Java program must have one class whose name is the
same as the program filename.
In the example, the Person class must be declared public static void main(String[] args)
in a program file named [Link]. {

[Link]("I am a person,
not a computer.");

Print Share

Common questions

Powered by AI

Consistent method signatures ensure that Java programs can be executed and integrated smoothly across various platforms and environments. The 'main' method's standardized signature allows the JVM to identify starting points in programs. Consistent method signatures also promote interoperability and easier maintenance within larger software systems, as other components or APIs can assume certain method characteristics without requiring additional interface definitions .

A complete Java statement consists of an expression or declaration that performs a task, followed by a semicolon to signify its termination. For instance, a function call like 'System.out.println("Hello, world!");' is a statement that prints a line to the console, while 'int x = 5;' is a declaration statement that initializes a variable. Each statement ends with a semicolon, allowing Java to parse and execute statements correctly .

The semicolon (';') in Java syntax serves as a delimiter, marking the end of a statement. Its presence is crucial for program compilation because it allows the Java compiler to parse and differentiate between separate statements within the code. Without semicolons, the compiler cannot distinguish where one statement ends and another begins, which would lead to syntax errors .

The Java compilation process ensures platform independence by converting source code into platform-neutral bytecode, which is stored in .class files. This bytecode can be executed on any machine that has a Java Virtual Machine (JVM) installed, regardless of the underlying hardware or operating system. Thus, Java programs are "write once, run anywhere," as the bytecode is interpreted and executed by the JVM according to the specific machine's environment .

Whitespace, including spaces and newlines, is insignificant in Java as it is ignored by the compiler. This feature allows programmers to format code for readability without affecting how the code executes. Whitespace is often used for indentation and aligning code blocks, making them easier to read and maintain .

Comments improve code readability by providing explanations or annotations that can help other programmers understand the code's purpose and functionality. In Java, single-line comments are created with //, allowing notes to be added to specific lines. Multi-line comments start with /* and end with */, which can be used for longer descriptions or to temporarily disable blocks of code without deleting them .

The Java Virtual Machine (JVM) executes a program by running the bytecode, which is the compiled form of the Java source code. When a Java program is compiled, each class is transformed into a .class file, which contains the bytecode. The JVM interprets this bytecode, allowing the program to run on any machine with a compatible JVM installed .

The 'main' method serves as the entry point for any standalone Java application. It requires a specific signature: 'public static void main(String[] args)', where 'public' allows the method to be accessible from outside the class, 'static' enables it to be called without creating an instance of the class, 'void' indicates it does not return a value, and 'String[] args' allows command-line arguments to be passed to the program. This uniform signature ensures the JVM can correctly locate and execute the entry point of the program .

'System' is a class from Java's core library that serves as a foundation for input and output operations. 'out' is an instance of the PrintStream class, which is a field inside the System class. It controls the output stream to the console. 'println()' is a method of PrintStream that outputs a line of text. Thus, 'System.out.println()' combines a reference to the output object with the method used to execute the output operation .

Having a class name that matches the program filename is crucial because it is a requirement for compiling and running Java programs. If the primary class name differs from the filename, the Java compiler will throw an error. This convention helps maintain organization and consistency within Java projects, making it easier to manage and navigate files within a development environment .

You might also like