Home Java javaTutorial Java basic syntax

Java basic syntax

Jul 24, 2017 pm 02:12 PM
Base statement

1.switch: The accepted type is byte short int char (suitable for specific values, but not many values.)
When the condition is established, execute the statement after the case. If no break is encountered after execution; or '}', Then the executable statements will continue to be executed. At this time, the condition of the case will not be judged until break is encountered again; or '}'.
2. Loop statement:

 ① while (conditional expression)
 {
  Loop body (execution statement);
 } ②do

 {
  Loop body (execution statement);
 } while (conditional expression);
 ③ for (initialization expression; loop conditional expression; operation expression after loop)
 {
  Execution statement;
 }

2.1. The difference between while and for about variables:

  Variables have their own scope. For for: If the increment used to control the loop is defined in the for statement, then the variable is only valid within the for statement. After the for statement is executed, the variable is released in memory.

2.2 for and while can be interchanged. If you need to define a loop increment, it is more appropriate to use for.


3. Function: An independent small program with specific functions defined in a class, also called a method.
3.1 Format of function:
                                         int     return return value; The data type of the result after running.
 Parameter type: It is the data type of the formal parameter.
 Formal parameter: It is a variable used to store the actual parameters passed to the function when calling the function.
 Actual parameter: the specific value passed to the formal parameter.
 Return: used to end the function.
 Return value: This value will be returned to the caller. (The keyword void is used when a function returns no specific value.)
3.2. Overloading of functions: In the same class, more than one function with the same name is allowed, as long as their number of parameters or parameter types are different. .
Specificity of overloading: It has nothing to do with the return value type, only the parameter list (it also depends on the parameter order).
Benefits of overloading: easier to read and optimized program design.
  Overload example:
   // Return the sum of two integers
   int add(int x, int y) { return x+y; }
   // Return the sum of three integers
   int add(int x, int y, int z ) { return x+y+z; }
     //Return the sum of two decimals
   double add(double x, double y) { return x+y; }

4. Memory structure:
When the Java program is running, it needs Allocate space in memory. In order to improve computing efficiency, the space is divided into different areas, because each area has a specific way of processing data and memory management.
 Stack memory: used to store local variables. When the data is used up, the space occupied will be automatically released.
Heap memory:

1>. Arrays and objects, and instances created through new are all stored in heap memory.

  2>. Each entity has a memory address value.
  3>.Variables in entities have default initial values.
  4>. The entity is no longer in use and will be recycled by the garbage collector within an uncertain period of time.


5. Array (reference data type): a collection of data of the same type. In fact, an array is a container.
Benefits of arrays: You can automatically number the elements in the array starting from 0, making it easier to operate these elements.
  Format 1:

  Element type [] Array name = new Element type [number of elements or array length];

  eg: int [] arr = new int[5];
 Format 2:
  Element type [] Array name = new element type [] {element, element,....};
  eg: int [] arr = new int[]{1,2,5,3};//Create an entity in the heap memory space and create Each element is assigned a specific value.
  int [] arr = {1,2,5,3};//It can be abbreviated if the data is clear
 The function of new: used to generate a container entity in the heap memory.
 int [] arr: int represents the type of element; [] table array; arr represents the array type. arr is an array variable in the stack memory. What is assigned is actually the address of the array in the heap memory. In addition, arr is stored in the stack memory, and then the container entity address created by new in the heap memory is obtained, which points to the heap memory. array.

The above is the detailed content of Java basic syntax. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP Basics Tutorial: From Beginner to Master PHP Basics Tutorial: From Beginner to Master Jun 18, 2023 am 09:43 AM

PHP is a widely used open source server-side scripting language that can handle all tasks in web development. PHP is widely used in web development, especially for its excellent performance in dynamic data processing, so it is loved and used by many developers. In this article, we will explain the basics of PHP step by step to help beginners from getting started to becoming proficient. 1. Basic syntax PHP is an interpreted language whose code is similar to HTML, CSS and JavaScript. Every PHP statement ends with a semicolon; Note

multi-catch statement in PHP8.0 multi-catch statement in PHP8.0 May 14, 2023 pm 01:51 PM

With the development of web applications, PHP language has been widely used in web development. In the PHP8.0 version, a new language feature was introduced - the multi-catch statement. What is a multi-catch statement? In previous PHP versions, developers needed to write multiple catch statements to handle multiple exception types. For example, the following code block shows the handling of two different exceptions: try{//Somecodethatmay

How to implement the statement of inserting data in MySQL? How to implement the statement of inserting data in MySQL? Nov 08, 2023 am 11:48 AM

How to implement the statement of inserting data in MySQL? When using a MySQL database, inserting data is a very basic and common operation. By inserting data, new records can be added to database tables to provide support for business operations. This article will introduce how to use the INSERT statement in MySQL to implement data insertion operations, and provide specific code examples. The INSERT statement in MySQL is used to insert new records into the database table. Its basic syntax format is as follows: INSERTINTOt

Can I learn Linux from scratch? What do I need to learn? Can I learn Linux from scratch? What do I need to learn? Feb 19, 2024 pm 12:57 PM

If you want to work in the IT industry, but do you want to learn programming, which technology should you choose? Of course it is Linux operation and maintenance. Linux is a very popular technology on the market, with a wide range of applications and good employment prospects, and is liked by many people. So the question is, can I learn Linux operation and maintenance with zero basic knowledge? In the server market, Linux system has a market share of up to 80% because of its advantages such as stability, security, free open source, efficiency and convenience. From this, it can be seen that Linux applications are very popular. Extensive. Whether now or in the future, learning Linux is a very good choice. As for whether it is possible to learn from scratch? My answer is of course. Oldboy Education Linux face-to-face class is specially designed for people with zero basic knowledge

Introduction to PHP Basics: How to use the echo function to output text content Introduction to PHP Basics: How to use the echo function to output text content Jul 30, 2023 pm 05:38 PM

Basic introduction to PHP: How to use the echo function to output text content. In PHP programming, you often need to output some text content to a web page. In this case, you can use the echo function. This article will introduce how to use the echo function to output text content and provide some sample code. Before starting, first make sure you have installed PHP and configured the running environment. If PHP is not installed yet, you can download the latest stable version from the PHP official website (https://www.php.net).

Learn the basics of Go language variables Learn the basics of Go language variables Mar 22, 2024 pm 09:39 PM

Go language is a statically typed, compiled language developed by Google. Its concise and efficient features have attracted widespread attention and love from developers. In the process of learning the Go language, mastering the basic knowledge of variables is a crucial step. This article will explain basic knowledge such as the definition, assignment, and type inference of variables in the Go language through specific code examples to help readers better understand and master these knowledge points. In the Go language, you can use the keyword var to define a variable, which is the format of the var variable name variable type.

To understand flow control statements in Python, you need to master several situations To understand flow control statements in Python, you need to master several situations Jan 20, 2024 am 08:06 AM

Python is a widely used high-level programming language. It is easy to learn, efficient and flexible, and is deeply loved by developers. In Python, flow control statements are an important part of implementing program logic. This article will introduce commonly used flow control statements in Python and provide code examples to deepen understanding. In Python, common flow control statements include conditional statements and loop statements. Conditional statements execute different code blocks based on the true or false condition and are used to determine and select execution branches. The loop statement is used to repeat

How to implement the statement to unlock the table in MySQL? How to implement the statement to unlock the table in MySQL? Nov 08, 2023 pm 06:28 PM

How to implement the statement to unlock the table in MySQL? In MySQL, table locks are a commonly used locking mechanism used to protect data integrity and consistency. When a transaction is reading and writing to a table, other transactions cannot modify the table. This locking mechanism ensures data consistency to a certain extent, but may also cause other transactions to be blocked. Therefore, if a transaction cannot continue for some reason, we need to manually unlock the table so that other transactions can continue. MySQL provides a variety of

See all articles