A Java program can be thought of as a collection of objects that work together by calling each other's methods. The following is a brief introduction to the concepts of classes, objects, methods, and instance variables.
Object: An object is an instance of a class that has state and behavior. For example, a dog is an object whose state is: color, name, breed; behavior: WAG tail, bark, eat, etc.
Class: A class is a template that describes the behavior and state of a class of objects.
Method: The method is behavior, a class can have many methods. Logical operations, data modifications, and all actions are done in the method.
Instance variables: Each object has a unique instance variable, and the state of the object is determined by the value of those instance variables.
First Java program
Let's look at a simple Java program that will print the string Hello World
Instance
public class HelloWorld {
/* First Java program
* It will print a string Hello world
*/
public static void Main (String []args) {
System.out.println ("Hello World"); Print Hello World
}
}
The following step-by-step article describes how to save, compile, and run this program:
Open Notepad and add the above code;
Save the filename as: Helloworld.java;
Open the CMD Command window and enter the location of the target file, assuming it is c:\
In the Command Line window, type Javac Helloworld.java press ENTER to compile the code. If there is no error in the code, the cmd command prompt goes to the next line. (Assuming the environment variables are set up).
Then type the Java HelloWorld Press ENTER to run the program.
You'll see Hello World at the window
C: > Javac Helloworld.java
C: > Java HelloWorld
Hello World
Basic syntax
When writing Java programs, be aware of the following points:
Case sensitive: Java is case sensitive, which means that the identifier Hello is different from hello.
Class name: For all classes, the first letter of the class name should be capitalized. If the class name consists of several words, the first letter of each word should be capitalized, such as Myfirstjavaclass.
Method Name: All method names should begin with lowercase letters. If the method name contains several words, the first letter of each subsequent word is capitalized.
SOURCE filename: The source filename must be the same as the class name. When saving files, you should use the class name to save the filename (remember that Java is case-sensitive), and the filename suffix is. java. (If the filename and class name are not the same, a compilation error is caused).
Main Method Portal: All Java programs are executed by the public static void main (String []args) method.
Java identifiers
All parts of Java need names. Class names, variable names, and method names are called identifiers.
There are several things to note about Java identifiers:
All identifiers should start with a letter (A-Z or a-Z), a dollar ($), or an underscore (_).
The first character can be followed by a letter (A-Z or a-Z), a dollar ($), an underscore (_), or any combination of characters of a number
Keyword cannot be used as an identifier
The identifier is case sensitive
Examples of valid identifiers: Age, $salary, _value, __1_value
Illegal identifiers Example: 123ABC,-salary
Java modifiers
Like other languages, Java can use modifiers to decorate methods and properties in a class. There are two main types of modifiers:
Access control modifiers: default, public, protected, private
Non-access control modifiers: final, abstract, STRICTFP
In later chapters we'll delve into the Java modifiers.
Java variables
There are several main types of variables in Java
Local variables
Class variable (static variable)
Member variable (non-static variable)
Java arrays
An array is an object that is stored on a heap and can hold multiple variables of the same type. In later chapters, we'll learn how to declare, construct, and initialize an array.
Java Enumeration
Java 5.0 introduces enumerations, which can only be preset values for enumeration restriction variables. Use enumerations to reduce bugs in your code.
For example, we design a program for the juice shop that will limit the juices to small, medium, and large cups. This means that it does not allow customers to spot the juice except for these three sizes.
Instance
Class Freshjuice {
Enum freshjuicesize{SMALL, MEDIUM, LARGE}
Freshjuicesize size;
}
public class Freshjuicetest {
public static void Main (String []args) {
Freshjuice juice = new Freshjuice ();
Juice.size = Freshjuice. Freshjuicesize.medium;
}
}
Note: Enumerations can be declared or declared in a class individually. Methods, variables, constructors can also be defined in enumerations.
Java keywords
The following is a list of Java reserved words. These reserved words cannot be used for constants, variables, and the names of any identifiers.
| Key words |
Describe |
| Abstract |
Abstract methods, modifiers for abstract classes |
| Assert |
Assert whether the condition satisfies |
| Boolean |
Boolean data type |
| Break |
Jump out of loops or label code snippets |
| Byte |
8-bit Signed data type |
| Case |
A condition of a switch statement |
| Catch |
Catch exception information with try Collocation |
| Char |
16-bit Unicode character data type |
| Class |
Defining classes |
| Const |
Not used |
| Continue |
Do not execute the remainder of the loop body |
| Default |
Default branch in a switch statement |
| Todo |
Loop statement, the loop body executes at least once |
| Double |
64-bit double-precision floating point numbers |
| Else |
Branch to execute if condition is not established |
| Enum |
Enum type |
| Extends |
Represents a class that is a subclass of another class |
| Final |
Indicates that a value cannot be changed after initialization. |
| Indicates that the method cannot be overridden, or a class cannot have subclasses |
| Finally |
Designed to accomplish code execution, it is primarily for the robustness and integrity of the program to execute code regardless of any anomalies. |
| Float |
32-bit single-precision floating-point numbers |
| For |
For Loop statement |
| Goto |