Basic Structure of JAVA β¨
"write once, run anywhere"

Basic of Java
The basics of Java include the following concepts:
Object-Oriented Programming (OOP): Java is an object-oriented programming language, which means that it uses the OOP paradigm to model real-world objects and their interactions. In Java, everything is an object, including variables, methods, and classes.
Case Sensitivity β Java is case sensitive, which means identifier Hello and hello would have different meaning in Java.
Classes and Objects: In Java, a class is a blueprint for creating objects. An object is an instance of a class and is created using the "new" operator. Classes can have variables, called instance variables, and methods, which define the behaviour of objects.
Variables: Variables are used to store data in Java. There are several different types of variables in Java, including primitive types (such as int, double, and char) and reference types (such as arrays and objects).
Methods: Methods are blocks of code that perform specific tasks. In Java, methods are defined within classes and can be called from other parts of the program. Methods can have parameters and can return values.
Control Structures: Java provides several control structures, including if-else statements, loops (for, while, do-while), and switch statements, which allow you to control the flow of your program.
Arrays: Arrays are data structures that allow you to store multiple values of the same type. In Java, arrays are objects and have a length that cannot be changed once the array has been created.
Strings: Strings are sequences of characters in Java and are treated as objects. Java provides several methods for manipulating strings, such as concatenating strings, searching for substrings, and converting strings to other data types.
Packages: Java uses packages to organize classes and provide a namespace. Packages help to prevent naming conflicts between classes and can be used to enforce access restrictions.
Basic Structure of Java
Java or in general, programming languages are composed of a set of rules, symbols, and specific syntax that are used to write computer programs. We write syntax in files, that are known as source files, to create programs. They are essentially a way for humans to communicate with computers and specify what they want the computer to do.
Let's understand the naming convention of a source file in Java.
Java file names are written with .java extension. Programs can be one file or hundreds of files. In Java, we have to name the source file with the same name as the public class out of all the other classes that the source file contains. It is important to note that a single Java source file can contain multiple classes, but only one of these classes can be declared public. The name of the public class should match the name of the source file.
The source code for that class should be in a file named "demo.java". The Java compiler will compile the code in the source file into a class file with the same name, "demo.class".
A .class file in Java contains bytecode which is a machine-independent binary format. This file can be executed by the Java Virtual Machine (JVM). JVM is a virtual machine responsible for executing the bytecode and providing a runtime environment for the program. It also provides services such as memory management, garbage collection, and security.
Syntax of Java π
The boilerplate code in Java refers to the basic code structure that is required to start writing a Java program. The following is an example of a Java boilerplate code:
public class demo {
public static void main(String[] args) {
// Your code goes here
}
}
In this example, the class "demo" contains a method called "main", which is the entry point for all Java applications. We use curly braces to mark the beginning and the end of a class. Syntax inside the curly braces is part of the class. The source code for that class should be in a file named "demo.java".
Inside the class, we have a "main" method, where you will write the code for your program. The method main() in Java is the entry point of a Java program. It is a method that is called when the program is executed. Here also, we use curly braces to mark the beginning and end of a method.
The "public static void" part of the code specifies the accessibility and behaviour of the method.
Note that every Java program must have a "main" method, as it is the starting point for the program. The "String[] args" part of the code represents an array of strings that can be passed to the program as command-line arguments.
This is the basic structure of Java boilerplate code. From here, you can start writing your program by adding code inside the main method.

Print Statement π¨οΈ
public class demo {
public static void main(String[] args) {
System.out.println("Codie's Coder");
}
}
The line of code System.out.println("Codie's Coder"); is a Java statement that outputs a message to the console.
Let's break down the code:
Systemis a pre-defined class in Java that provides access to the system-level resources.outis a static variable in the System class and represents the standard output stream.println()is a method in the print stream class that outputs a message to the console and automatically appends a newline character to the end of the message. We can also useprint(). Both will display same value on the screen.But there's a little difference between both the print statements. Whenever we want the program to create a new line on the screen after it prints some value, we use
println()method. Where,print()outputs everything on the same line and doesn't create a new line.
So, in the code, we are calling the println method on the standard output stream represented by the out variable in the System class, and passing the string "Codie's Coder" as an argument. This will cause the message "Codie's Coder" to be displayed on the console.
The use of System.out.println is very common in Java, and it is used to display messages, output results, and debug programs. The println method can accept any type of argument, including primitive data types, objects, and strings.
In conclusion, the System.out.println("Codie's Coder"); statement is used to output a message to the console and is a fundamental part of Java programming.
Like the example above, we also can use System.out.print("Codie's Coder") to display same value on the screen. But there's a little difference between both the print statements. Whenever we want the program to create a new line on the screen after it prints some value, we use println() method. Where, print() outputs everything on the same line and doesn't create a new line.
Input Statement
The statement input by itself is not a valid Java statement. To get user input in Java, you need to use the Scanner class. Here's an example of how to use the Scanner class to get user input:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = input.nextInt();
System.out.println("You entered: " + number);
input.close();
}
}
Let's break down the above code:
Let's break down the first line of code
import java.util.Scanner;:-The
importstatement is used to make the classes and interfaces of a package available to the code. This allows you to use the classes and interfaces of the package without having to specify the full package name each time.The
java.utilpackage is part of the standard Java libraries and provides a number of utility classes and interfaces for common programming tasks, such as working with collections, calendars, and input/output operations.
Let's break down the code
Scanner input = new Scanner(System.in);:-Scanneris a class in thejava.utilpackage that provides a way to read values from various input sources, including the keyboard.inputis a variable that is being declared and initialized at the same time. The type of the variable isScanner, which means thatinputcan store a reference to an object of theScannerclass.new Scanner(System.in)is a constructor for theScannerclass that creates a new instance of the class. TheSystem.inargument is passed to the constructor to specify that theScannershould read from the standard input stream, which is typically the keyboard.
The line
System.out.print("Enter a number: ");displays a prompt to the user, asking them to enter a number.The line
int number = input.nextInt();calls thenextIntmethod on theScannerobject to get an integer value from the user. The value is then stored in the variablenumber.The line
System.out.println("You entered: " + number);outputs the message "You entered: " followed by the value of thenumbervariable.Finally, the line
input.close();closes theScannerobject to free up resources.
In conclusion, the Scanner class is used to get user input in Java, and it provides a convenient way to read values from the standard input stream.
Conclusion π
In conclusion, the basic structure of a Java program consists of package declarations, import statements, class definitions, fields, and methods, and provides a foundation for writing Java programs.




