JAVA tutorial | basic syntax
Java basic syntax
A Java program can be thought of as a collection of objects, and these objects work together by calling each other's methods. The following briefly introduces the concepts of classes, objects, methods and instance variables.
##Object: An object is an instance of a class and has state and behavior. For example, a dog is an object. Its status includes: color, name, and breed; its behaviors include: wagging its tail, barking, eating, etc.
- Class: A class is a template that describes the behavior and status of a type of object.
- Method: Method is behavior, and a class can have many methods. Logical operations, data modification, and all actions are completed in methods.
- Instance variables: Each object has unique instance variables, and the state of the object is determined by the values of these instance variables.
The first Java program
Let’s look at a simple Java program. Will print the stringHello World .
public class HelloWorld { /* 第一个Java程序 * 它将打印字符串 Hello World */ public static void main(String []args) { System.out.println("Hello World"); // 打印 Hello World } }
- ##Open Notepad and add the above code;
- Save the file name as: HelloWorld.java;
- Open the cmd command window , enter the location of the target file, assuming it is C:\
- Type javac HelloWorld.java in the command line window and press the enter key to compile the code. If there are no errors in the code, the cmd command prompt will go to the next line. (Assuming the environment variables are all set).
- Type java HelloWorld and press Enter to run the program
C : > javac HelloWorld.java C : > java HelloWorld Hello World
When writing Java programs, you should pay attention to the following points:
##Case Sensitive: Java is case sensitive, which means that the identifiers Hello and hello are different.
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, for example, MyFirstJavaClass .
Method name: All method names should start with a lowercase letter. If the method name contains several words, the first letter of each subsequent word is capitalized.
Source file name: The source file name must be the same as the class name. When saving the file, you should use the class name as the filename (remember Java is case-sensitive) and the filename suffix .java. (If the file name and class name are different, a compilation error will occur).
Main method entrance: All Java programs consist of public static void main(String []args)method Begin execution.
##Java identifier
All components of Java need names.
Class names, variable names, and method names are all called identifiers.
## Regarding Java identifiers, there are the following points to note:
- All identifiers should start with letters (A-Z or a-z), dollar signs ($), or underscores (_)Start
-
## Examples of illegal identifiers: 123abc, -salary
(scope)
Like other languages, Java can use modifiers to modify methods and properties in a class. There are two main types of modifiers:
访问控制修饰符 : default, public , protected, private
非访问控制修饰符 : final, abstract, strictfp
Java变量
Java中主要有如下几种类型的变量
局部变量
类变量(静态变量)
成员变量(非静态变量)
Java数组
数组是储存在堆上的对象,可以保存多个同类型变量。在后面的章节中,我们将会学到如何声明、构造以及初始化一个数组。
Java枚举
Java 5.0引入了枚举,枚举限制变量只能是预先设定好的值。使用枚举可以减少代码中的bug。
例如,我们为果汁店设计一个程序,它将限制果汁为小杯、中杯、大杯。这就意味着它不允许顾客点除了这三种尺寸外的果汁。
注意:枚举可以单独声明或者声明在类里面。方法、变量、构造函数也可以在枚举中定义。
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 ; } }
##Java keyword
Java reserved words are listed below. These reserved words cannot be used in the names of constants, variables, and any identifiers.
Keyword Description abstract Abstract method, abstract class modifier assert Assert whether the condition is met boolean Boolean data type break Break out of the loop or label code segment byte 8-bit signed data type case A condition of the switch statement catch Combined with try to capture exception information char 16-bit Unicode character data type class Definition class const Unused continue Do not execute the remainder of the loop default Default branch in switch statement do Loop statement, the loop body will be executed at least once double 64-bit double precision floating point number else The branch executed when the condition is not true enum Enumeration type extends Indicates that one class 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 subclassesfinally Designed to complete the executed code, mainly for the robustness and stability of the program Integrity, code is executed regardless of whether an exception occurs. float 32-bit single precision floating point number for for loop statement goto Not used if Conditional statement implements Indicates that a class implements the interface import Import class instanceof Test whether an object is an instance of a class int 32-bit integer interface Interface, an abstract type with only definitions of methods and constants long 64-bit integer native Indicates that the method is implemented in non-java code new Allocate a new class instance package A series of related classes form a package private indicates that private fields, methods, etc. can only be accessed from within the class protected indicates that the field can only be accessed from within the class Access via class or its subclass
Subclasses or other classes in the same packagepublic represents shared properties or methods return Method return value short 16-digit number static means in Class level definition, shared by all instances strictfp Floating point comparison uses strict rules super Indicates the base class switch Select statement synchronized Indicates only the same time A block of code that can be accessed by a thread this represents a call to the current instance
Or call another constructorthrow throws exception throws define method possible Thrown exception transient Modify fields not to be serialized try Indicates the code If the block needs to handle exceptions or cooperate with finally to indicate whether an exception is thrown, the code in finally will be executed void The marked method does not return any value volatile Marked fields may be accessed by multiple threads at the same time without synchronization while while loop
#Java Notes
in comments will be ignored by the Java compiler
## Similar to C/C++ , Java also supports single-line and multi-line comments. The characters.
##Blank lines or lines with comments will be ignored by the Java compiler.
#Inheritance
#In Java, a class Can be derived from other classes. If you are creating a class and there is already a class that has the properties or methods you need, then you can inherit the newly created class from that class.
Using inherited methods, you can reuse methods and properties of existing classes , without having to rewrite these codes. The inherited class is called a super class, and the derived class is called a subclass.
interfaceIn Java, an interface can be understood as a protocol for communication between objects. Interfaces play a very important role in inheritance.##
The interface only defines the methods to be used by the derived class, but the specific implementation of the method completely depends on the derived class.
#Java source program and compilation The difference in operation
## is shown in the figure below:
![]()
# The above is the content of JAVA entry tutorial | basic syntax. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip
