Java syntax
Java code contains a file class which name is same as the Java file name, and a main method. From where the Java code start?
Example:
Top level class or file class name should be same as saved the java file name like Main.java
Top level class can not be declare as private or protected but it can be only public, default, abstract, or final.
All the code will be written in between top level class
Top class contain a main method from where program start
About main method of java
Lets explain then main method of java
Here is the sample of basic java code
Example:
import java.lang.* : Important and mandatory (lang -- language) package
HelloWorld: Mainly file class as file name because everything java program is in the form of class
public static void main(String[] args) {... } : It is main method form where the code start. *'
public: Access modifier when javac compiler and generate error free class file after that on the execute the compiled class file JVM call to the main method. So main method should be public
static: When the main method be static then JVM can load the class into memory and call the main method without creating an instance of the class first. Because When the Java program starts, there is no object of the class present.
void: Every Java method must provide the return type. The Java main method return type is void because it doesn’t return anything.
When the main method is finished executing, the Java program terminates, so there is no need for a returned object. In the following example code, the main method attempts to return something when the return type is void: java don't expect "return" types value.
main: The Java main method is always named main. When a Java program starts, it always looks for the main method.
String[] args: Java main method accepts a single argument of type String array.
Because by passing String arrays , we can pass all the necessary parameters like options/arguments related to the program in the form of String easily. There can be several parameters! Also, all the other datatypes can be easily converted from String!
Each string in the array is a command line argument. You can use command line arguments to affect the operation of the program, or to pass information to the program, at runtime. The following example code shows how to print the command line arguments that you enter when you run the program:
System.out.println()
Above line of code use to print the data on the display in main() method and that focous on next line.
If we remove "ln" from the code it will not point to the next line.
Here's an explanation of each part of the method signature:
public: an access modifier that indicates that the method is accessible from anywhere, including outside of the class where it's defined.
static: a keyword that indicates that the method belongs to the class itself, rather than to instances of the class. This means that you can call the method without creating an object of the class first.
void: the return type of the method, which means that the method doesn't return any value.
main: the name of the method. This is a special method name in Java that's recognized as the entry point for the program.
String[] args: a parameter that represents an array of command-line arguments passed to the program. The String[] type means that the array contains elements of type String. You can access the arguments in the array using the index, like args[0].
So, the main method is a special method in Java that's used to start a program, and it takes an array of command-line arguments as input.
Example:
"{}" curly braces marks the beginning and the end of a block of code.
System: Built-in Java class that contains useful members, such as out, which is short for "output".
out: Object
println(): Method, short for "print line", is used to print a value to the screen (or a file).
Each code statement must end with a semicolon ";" to terminate the line
Java Output - print(), println()
println() Method
Use the println() method to print or output values or print text in Java.
Println() add a new line for each method after print.
print() Method
Print() method, which is also use to print but it does not insert a new line at the end of the output
Java Input
To take input from user import util library that provide Scanner class
Here we will a simple example for integer input and after that in the class tutorial we will know more about taking input by user
Example: Take integer value input through user
import java.util.*;
class UserInputDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // System.in is a standard input stream
System.out.print("Enter first number- ");
int a = sc.nextInt();
System.out.println("Your number" + a);
}
}
Quotes, String and number
1. Quotes with string and number
Quotes and string
working with text, it must be wrapped inside double quotations marks "".
It occurs error if miss.
2. Quotes and number
Numerical value not to be within quotes but it if it is, then it will be treated as simple string not a number
Use mathematical operator to print as mathematical calculations
Java Comments
What is comments
Comments can be used to explain Java code,
Comments make code more readable.
It is used to prevent execution when testing alternative code.
Types of comment
1. Sing a line comments
2. Single-line comments start with two forward slashes (//).
Sing line comments in Java
Any text between // and the end of the line is ignored by Java.
Code will not be executed
we use // for short comments
Example: // java comment
Multiline comments in Java
Multi-line comments start with /* and ends with */.
Any text between /* and */ will be ignored by Java.
we use /* */ for longer comment.
Example:
/* java multilane comment
java multilane comment
java multilane comment
*/
---------------------------------------------------------------------------------------------------------
*' [Note: Probably We can execute a java program without a main method by using a static block. Static block in Java is a group of statements that gets executed only once when the class is loaded into the memory by Java ClassLoader, It is also known as a static initialization block.]