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

What is the basis of Java programming?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article introduces you the basis of Java programming, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Overview of 3.1Java programming

Now you can review the background materials of the Java language. The basic structure of the Java language is like Cmax Candle codes, but anyone who has written a program in a process-oriented language can understand most of the structures.

3.1.1 Program structure

The source code of the Java language consists of one or more compilation units (compilationunit). Each compilation unit can only contain the following (except spaces and comments): * A package statement (packagestatement) * entry statement (importstatements) * Class declaration (classdeclarations) * Interface declaration (interfacedeclarations) each Java compilation unit can contain multiple classes or interfaces, but at most one class or interface per compilation unit can be public. After the source code of Java is compiled, the Java bytecode is generated. The bytecode of Java consists of an instruction that does not depend on the machine, which can be effectively interpreted by Java's operating system (runtimesystem). The running system of Java works like a virtual machine. In the current Java implementation, each compilation unit is a file with a .java suffix. Each compilation unit has several classes, and after compilation, each class generates a .class file. The .class file is code that the Java virtual machine can recognize.

3.1.2 comments

The three types are as follows: / / comment one line / * one or more lines of comments * / * document comments * /

Documentation comments are generally placed before a variable or function definition, indicating that they are called in any automatically generated documentation system. Such comments are descriptions of declaration entries.

3.1.3 Identifier

The names of variables, functions, classes, and objects are identifiers, and identifiers are needed for everything programmers need to identify and use. In the Java language, identifiers begin with characters or _, $, and can be followed by numbers. Identifiers are case-sensitive and have no length limit.

Valid identifier mynameict_networkHello_sys_path$bill

Example: inta_number;char_onechar;float$bill

Keyword abstractcontinuefornewswitch

Booleandefaultgotonullsynchronized

Breakdoifpackagethis

Bytedoubleimplementsprivatethreadsafe

Byvalueelseimportprotectedthrow

Caseextendsinstanceofpublictransient

Catchfalseintreturntrue

Charfinalinterfaceshorttry

Classfinallylongstaticvoid

Constfloatnativesuperwhile

Other reserved words the following words are reserved: castfuturegenericinner

Operatorouterrestvar

3.1.4 data type

Java uses five basic types: integer (integer), floating (floating point number), point (pointer), Boolean (Boolean variable), and CharacterorString (character or character? reg;). Examples of data representations given under integer integers are all integers: 4Jing 15pr 089j0xAD00

Integer length data type representation

8bitsbyte

16bitsshort

32bitsint

64bitslong

The data representations given under floating floating-point numbers are examples of floating-point numbers: 6.37, 3.7E15, 3e8.

Floating point length data type representation

32bitsfloat

64bitsdouble

Below the Boolean Boolean variable are two possible values of the Boolean variable: truefalse

Below the Character characters are examples of characters: a\ t (tab)\ uvial characters? (unicode)

String characters? the following are all examples of characters reg;: "Thisisastringliteral" and "Institute of Computing, Chinese Academy of Sciences".

Array you can define any type of array .chars []; this is a character array; int [] array; is an integer array; you can also define an array .intblock [] [] = newint [2] [3]; array boundaries are detected at run time to avoid stack overflows and memory corruption.

In Java, an array is actually an object, and the array has one member variable: length. You can use this member function to view the length of any array .inta [] [] = newint [10] [3] a.length/*10*/a [0] .length / * 3 length /

Create arrays create arrays in Java, you can use two basic methods? reg; one. Create an empty array: intlist [] = newint [50]; or you can populate the array .Stringnames [] = {"Chenji", "Yuan", "Chun", "Yang"}; equivalent to the following functions: Stringnames []; names=newString [4]; names [0] = newString ("Chenji"); names [1] = newString ("Yuan"); names [2] = newString ("Chun"); names [3] = newString ("Yang")

You cannot create a static array at compile time as in the following example. Intname [50]; / / will generate a compilation error

You can't use the new operation to populate an array of undefined sizes. Intname []; for (inti=0;i > >\ =! = & ^ & & | |?: = op=

When integer operators operate on integers, if the Operand is of type long, the result is of type long, otherwise it is of type int and will never be of type byte,short or char. This way, if the variable I is declared as short or byte,i+1, the result will be int. If the result exceeds the value range of the type, the module is taken according to the maximum value of the type. The unary integer operators are:

Operator operation-monomial non-bit complement + + plus 1mura-minus 1

The + + operator is used to represent a direct plus 1 operation. Incremental operations can also be done indirectly with addition operators and assignment operations. + + lvalue (the left value? copy; means lvalue+=1,++lvalue also means lvalue=lvalue+1 (as long as lvalue has no side effects). -- the operator is used to indicate a minus 1 operation. The + and-- operators can be used either as prefix operators or as suffix operators. The binocular integer operators are:

Operator operation * * + add-minus * multiply / divide% take module & bit and | bit or ^ bit XOR right shift (signed) > > add zero right shift

Integer division is rounded by zero. Division and modularization obey the following equation: (a) b) * b + (a% b) = = a the abnormality of integer arithmetic operation is caused by division by zero or modulus by zero. It will throw an arithmetic exception. The underflow produces zero, and the overflow leads to crossing the line. For example, plus 1 exceeds the maximum value of the integer and becomes the minimum value after taking the module. An op= assignment operator, combined with the binocular integer operators in the table above, forms an expression. The integer relational operators, =, =, and! = produce data of type boolean.

A combination of Boolean operator boolean variables or expressions can produce new Boolean values. Unary operator! It's Burfield. The binocular operators &, |, and ^ are logical AND,OR and XOR operators that force two operands to Boolean. To avoid redundant evaluation of the right Operand, users can use the short circuit evaluation operators & & and | |. Users can use = and! =, and assignment operators can also use & =, | =, ^ =. Ternary conditional operator?: is the same as in C language.

Floating-point operators can use a combination of regular operators, such as monocular operators +,--, binocular operators +, -, *, and /, and assignment operators + =,-=, * =, and / =. In addition, there are modular operations:% and% = can also act on floating-point numbers, for example: a% b and a-((int) (a _ (b)) * b) have the same semantics. This means that the result of a% b is the floating point part left after division. Only the floating-point expression of a single-precision Operand is evaluated according to a single-precision operation, resulting in a single-precision result. If the floating-point expression contains one or more double-precision operands, the result is a double-precision floating-point number.

Array operators take the following form: [] gives the value of an element in the array. The legal range is from 0 to the length of the array minus 1. The check of the value range is real copy; only at run time.

The reg; operator reg; is implemented as a String object. The operator "+" completes the merge reg; operation, automatically converting operands to string if necessary. If the Operand is an object, it can define the String way in which a method toString () returns the object, such as floata=1.0print ("Thevalueofais" + a + "\ n"); the example Strings= "a =" + a position = operator on? reg; used by the + operator can also be used for String. Note that the left side (S1 in the following example) is evaluated only once. S1string string is automatically converted to string if it is not string.

The object operator binocular operator instanceof tests whether an object is an instance of the specified class or its subclasses. For example: if (myObjectinstanceofMyClass) {MyClassanothermyObject= (MyClass) myObject; … } is to determine whether myObject is an instance of MyClass or an instance of its subclass.

Force and convert the Java language and interpreter restrict the use of enforcement and conversion to prevent errors from causing the system to crash. Integers and floating point reg; can be cast back and forth, but integers cannot be cast to arrays or objects. Objects cannot be forced to be primitive types.

3.1.6Java flow control

The following control structures are borrowed from the C language.

Branching structure

If/else branching structure

If (Boolean) {statemanets;} else {statements;}

Switch branching structure

Switch (expr1) {caseexpr2:statements;break;caseexpr3:statements;break;default:statements;break;}

Cyclic structure for cyclic structure

For (initexpr1;testexpr2;incrementexpr3) {statements;}

While cycle structure

While (Boolean) {statements;}

Do cycle structure

Do {statements;} while (Boolean)

General sequence control

Break[label] continue[label] reutrnexpr;label:statement

For loop example below is an example of a program that draws several lines in red, green, and blue colors, which may be part of the Java function:

Intcount;for (count=1;count

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

Servers

Wechat

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

12
Report