Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Summary of Java basic Grammar

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "Java basic Grammar Summary". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Identifier

Concept

The sequence of characters used by Java to name various variables, methods, and classes is called identifiers.

Rules

1. Identifiers consist of letters, underscores, dollar signs, or numbers

two。 Identifiers should begin with letters, underscores, and dollar signs

3.Java identifiers are case-sensitive and unlimited in length

Note: the Java identifier should be named and cannot be duplicated with the keyword.

Keyword

Concept

Some special-purpose strings in Java are called keywords. All Java keywords are in lowercase English, and keywords are no longer regarded as keywords in uppercase.

Classification of keywords

Used to control access related to 4: public (public), protected (protected, other classes and subclasses in the same package can access.) , default, private (private, accessible to this class)

Relationship between modifiers and packages and classes, as well as access relationships:

Public protected default private

This category can

Can the same steamed stuffed bun class

Can the non-subclass of the same package

Can different steamed stuffed bun classes

Can different packages and non-subclasses

Class, interface, abstract class related, instantiated object related 6: abstract (abstract) class (define class), interface (define interface) implements (implementation), extends (inheritance) new (create new object)

There are 12 related program controls: if (if), else (otherwise), instanceof (determine whether an object is an instance of a class) switch (switch), case (a condition of switch statement), break (jump out of loop / jump out of switch statement block), default (default branch of switch statement, executed when all case does not meet conditions) for (for loop statement), do (loop statement) The loop body will be executed at least once), while (when … ), continue (end this cycle and continue to the next cycle) return (return... , for jumping out of the method),

Modification methods, variable methods related to 8: static (static), final (final immutable), const (const is a Java reserved keyword for later extension, the usage is similar to final, not commonly used) native (representation method implemented in non-java code) synchronized (thread synchronization) transient (decorate unserialized fields) volatile (marked fields may be accessed by multiple threads at the same time) strictfp (strict / precise)

There are 5 related exception handling: try (catch exception), catch (handle exception), finally (execute with or without exception) throw (throw an exception object), throws (declare that a method may throw an exception)

2 packages related: import (incoming package) package (definition package)

8 related data types: 8 basic data types: byte, short, int, long, float, double, char, boolean

There are three related variable references: super (calling the method / constructor / member variable of the parent class) this (representing the member variable or method calling another overloaded constructor of the current instance) void (no return value)

The other two are related: assert (asserting whether the condition is met, then continuing to execute downwards, and if not, throwing an exception to terminate execution), enum (enumerated type)

Reserved word: goto const false true.

Constant

Constant values of Java are represented by strings of different data types

For example: integer type: 123character constant'a'; logical constant: true; string constant: "Hello World".

Variable

Java variable is the most basic storage unit in the program, and its elements include variable name, variable type and scope.

Each variable in Java belongs to a specific data type, and the variable type must be declared before using it:

Int I = 1

Float f = 12.3f; / / Note float a = (float) 1.3; or float a = 1.3f; (either f or F can be case-insensitive)

Double dong1.3; / / Note the distinction between float and single-precision floating-point numbers

String name = "hello world"

Classification of variables

Divided by declared location

Local variable: a variable defined within a method or statement block

Member variables: variables defined outside the method and inside the class

Data type division

Basic data type variable

Reference data type variable

Local and member variables

Public class Test {

Public int iTERATION / member variables

Public void method () {

Local variables are not allowed to be modified with access modifiers. Int

}}

Conversion of basic data types

Boolean types cannot be converted to other data types

Character and floating point data should be converted to each other in mixed operations according to the following rules

Types with small capacity are automatically converted to data types with large capacity:

Byte,char,short > int > long > float > double

Byte,char,short don't convert to each other. They convert to int type before calculating.

Conversion of large capacity types to capacity knowing that a forced conversion character should be added to the type, which may result in reduced precision or overflow

Public class TestConvert {

Public static void main (String arg []) {

Int i1 = 123

Int i2 = 456

Double D1 = (i1+i2) 1.2 double / the system will convert to double operations because small types are automatically converted to large data types 1.2 by default

Float F1 = (float) ((i1+i2) 1.2); / / the conversion character (i1+i2) 1.2is of type double, and cast is required if accepted with float because the double type is greater than float

Byte b1 = 67

Byte b2 = 89

Byte b3 = (byte) (b1+b2); / / the system will be converted to int operation, which requires a forced conversion character

System.out.println (b3)

Double D2 = 1e200

Float f2 = (float) d2rampact / will cause overflow

System.out.println (f2)

Float f3 = 1.23f / must add f

Long L1 = 123

Long L2 = 3000000000L / must add l

Float f = L1 calculation, L2 calculation, f3 balance / system will be converted to float calculation

Long l = (long) f bank / cast will round off the decimal part (not rounded)

}}

Java operator classification

Arithmetic operator: +-/% +--

Relational operator: >

< = == != 逻辑运算符:! & | ^ && || 位运算符:& | ^ ~ >

>

Assignment operator: =

Extended assignment operator: + =-= / = * =

String concatenation operator: +

Statement

Conditional statement

If / / execute if the condition is true

If...else / / if the if condition is true to execute the if statement, otherwise execute the else statement

If...else if / / ditto

If...else if...else if...else / / if there is an execution, it will no longer judge the subsequent Tianjian.

Switch

Swtich () / / variable types can only be int, short, char, byte, and enum types. When making a case judgment, JVM automatically scans from top to small to look for a matching case.

Loop statement

For

While

Do while

A break statement that terminates the execution of a block of statements. In a loop statement system, you can forcibly exit a loop.

Continue statement to skip this loop and move on to the next loop

Array

Define

DataType [] arrayVar

DataType arrayVar []

Description: first, dataType it can be a basic data type such as int array []; it can also be a reference type such as Object [].

Method

A method is a collection of statements that can implement a function, such as the println () method, which implements printout. In java, a method is contained in a class or object, and the method is created in the program and referenced elsewhere.

Define a method

Modifier returns value type method name (parameter type parameter name) {

...

Method body

...

Return return value

}

/ / example

Public int addNumber (int a, int b) {

Int Xero0

X=a+b

Retutn x

}

Define rules

1. Naming rules. Method names usually start with a lowercase letter, followed by an uppercase first letter.

2. The type of the value followed by return must be the same as the return value type, except for void, because void does not return a value.

Analytical method

Modifier: modifier, which is optional, tells the compiler how to call the method. Defines the access type of the method.

Return value type: the method may return a value. ReturnValueType is the data type of the value returned by the method. Some methods perform the required operation but do not return a value. In this case, returnValueType is the keyword void.

Method name: is the actual name of the method. The method name and the parameter table together constitute the method signature.

Parameter type: the parameter looks like a placeholder. When the method is called, the value is passed to the parameter. This value is called an argument or variable. The parameter list refers to the parameter type, order, and number of parameters of the method. Parameters are optional, and methods can contain no parameters.

Method body: the method body contains specific statements that define the function of the method, such as int x=a+b.

Main method

The main method is called by JVM, and otherwise, the main method is no different from other methods. The header of the main method is immutable, as shown below, with modifiers public and static, returns the void type value, the method name is main, and takes a String [] type parameter. String [] indicates that the argument is an array of strings.

Public class TestMax {

/ * main method /

Public static void main (String [] args) {

Int I = 5

Int j = 2

Int k = max (I, j)

System.out.println (I + "and" + j + "compare, the maximum value is:" + k)

}

/ * returns the larger value of two integer variables /

Public static int max (int num1, int num2) {

Return num1 > num2? Num1:num2

}

}

Construction method

Class Student {

Int id;//ID

String name;// name

Int age;// age

Student (String n) {/ / has a construction method of one parameter

Name = n

}

Construction method of Student (int I, String n) {/ / with two parameters

Id = I

Name = n

}

Student (int I, String n, int a) {/ / Construction method with three parameters

Id = I

Name = n

Age = a

}

Public void show () {/ / Common method

System.out.println (id + "" + name + "" + age)

}

Public static void main (String args []) {Student S1 = new Student ("Xiao Hong"); / / create a little red Student S2 = new Student (250,18); / / create a s1.show (); / / object call method s2.show ();}

}

Java files are compiled into bytecode files by JVM, namely .class files. When bytecode files are run in different operating systems, the operating system compiles bytecode files into machine code files. This is Java cross-platform.

First of all, it is clear that java's GC collection is completely automatic. No manual api collection is provided. All memory allocation and collection permissions are in jvm. There is no absolute mandatory garbage collection method in the hands of developers, but you can do this:

For an object that is no longer referenced, assign its reference to null in time. Obj = null

If memory is really tight, call the System.gc () method to advise the garbage collector to start collecting garbage and tell GC to run, but the Java language specification does not guarantee that GC will be executed.

Default values and range of values for java basic types

Integer type byte (1 byte) short (2 bytes) int (4 bytes) long (8 bytes)

Character type char (2 bytes)

Floating point type float (4 bytes) double (8 bytes)

The ASCII code values for common characters are as follows: the ASCII code values for spaces are 32; the ASCII values for the numbers 0 to 9 are 48 to 57; the ASCII values for uppercase letters "A" to "Z" are 65 to 90; and the ASCII values for lowercase letters "a" to "z" are 97 to 122.

Java identifiers have the following naming rules:

Consists of 26 uppercase and lowercase letters, the number: 0-9 symbol: $

Identifiers should start with the letters, and $.

The identifier cannot be a keyword.

Abstract classes and interfaces

About Abstract CLA

Prior to JDK 1.8, the default access permission for methods of abstract classes was protected

When JDK 1.8, the default access permissions for methods of abstract classes change to default

About Interfac

Prior to JDK 1.8, the methods in the interface must be public

When JDK 1.8, the methods in the interface can be public or default

When JDK 1. 9, the methods in the interface can be private

Packing and unpacking

The conversion of basic data types to wrapper classes is boxed (for example: int-- > Integer).

The conversion of a wrapper class to a basic data type is unboxed (for example: Integer-- > int).

The wrapper class is the reference type, and the basic data type is the value type.

Through boxing and unboxing operations, the XM agent applies for www.kaifx.cn/broker/xm.html, which can bridge the gap between value types and reference types. In other words, it is easy to convert value types to reference types, boxing and unboxing can uniformly examine the system, and values of any type can eventually be processed according to objects.

Serialization and deserialization

Java does not instantiate static variables and transient-decorated variables when serializing, because static represents a member of a class, transient represents temporary data of an object, and it is declared that these two types of data members cannot be serialized.

Java has two ways of passing, value passing and reference passing. Basic type and string str = "aaa"; what is created in this way is value passing, object creation and array are reference passing, so special attention should be paid to the judgment of formal parameters in the function.

Java garbage collection mechanism

Garbage collection is mainly aimed at the collection of the heap area, because the memory of the stack area is released with the thread. The stack area is divided into three areas: the younger generation (Young Generation), the older generation (Old Generation) and the permanent generation (Permanent Generation, that is, the method area).

Younger generation: objects that are created (new) are usually placed in Young (except for some objects that occupy a large amount of memory), and objects that are still alive after a certain amount of Minor GC (memory collection for the younger generation) will be moved to the older generation (some specific movement details are omitted).

Older generation: it is the above-mentioned younger generation that has moved over and some larger objects. Major GC (FullGC) is for the older generation of recycling.

Permanent generation: final constants, static variables, constant pools are stored.

This is the end of the summary of Java basic Grammar. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report