Java syntax basics and data types

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators case statement what bit

One, the package

1, package<top_pkg_name>[.<sub_pkg_mame>];

2 . Import the package. Use ". * " To import all classes in the package. The default import java.lang.*.

3. Static import: use static import to make all the static and static methods that are imported visible directly in the current class, using these static members without giving their class names.

For example:

Import Static Java.lang.Math.pow;  Public Static void Main (string[] args) {    double D=pow (10,1);}

Ii. declarations of classes, attributes, constructors, member methods

class declaration:

< scopes > Classes < class name >{

< attribute declarations >

< constructor declarations >

< method declaration >

}

Method:< method scope > < return type >< method Name > (< parameter list >) {}

Variable parameters: (Below is a function that closes one or more streams)

 Public Static void CloseAll (closeable ...    io) {for(closeable temp:io) {        try  {             if (null ! = Temp) {
Temp.close (); } catch (Exception e) {}}

Note

1, the construction method and the class name are the same, there is no return value. The constructor method can also invoke other construction methods, which are implemented with this, and this parameter indicates which construction method to invoke.

2, if a class does not have a constructor, then the virtual machine will automatically give the Class A default constructor (no parameter constructs). If you construct a constructor with parameters in this class, the virtual machine will provide a default constructor, and of course, it cannot be constructed by default.

3. Variables within a method cannot have static and access (SCOPE) keyword adornments, note that they are local variables. They can be final modified.

Third, operators and precedence

Multiplication Plus and minus are the same as those in mathematical formulas, with parentheses in the precedence calculation

1. Precedence comparison of arithmetic operators and parentheses

From high to Low

()

+,-(positive, negative)

*,/,%

+,-(plus, minus)

Sibling

Sibling

Sibling

Monocular operator

Binocular operator

2. Relational operations:

Priority level

Same priority level (high)

Same priority (Low)

Relational operators

>

Greater than

>=

(greater than or equal to)

The

Less than

<=

(less than equals)

= =

Equal to

! =

(Not equal to)

3. Logical operators

Priority level

High and low

logical operators

! (logical "non")

&& (logical "and")

|| (logical "or")

(1) Peer computing is "left-associative" or "right-associative", not the same level of view

(2) in relational operators, arithmetic operators, assignment operators, and logical operations, the priority from high to Low is:! (logical "non"), arithmetic operators, relational operators,&&(and),| | (or), assignment operator

4.

Bit operation meaning and function

Bitwise operators

Meanings and functions

Priority level

~

Bitwise negation

Highest (Monocular)

<<

Move left

1

> >

Move right

1

&

Bitwise-AND

2

^

Bitwise XOR OR

3

|

Bitwise OR

Minimum

NOTE: Bitwise negation operators are single-mesh operators with the highest precedence, binding right-to-left, and binocular operators, which are associative from left to right. A bitwise operand can only be an integer or character-type data.

Note:i++ and ++i

int j=0;  for (int i=1; i<10; i++) {j=j++;} System.out.println (j);

The result above is 0. Perform the assignment first, then perform the + operation, when the loop enters the first time, J is performed +1 operation, but finally because the value before the assignment is 0 is assigned to J, so the value of each loop J has not changed, so the print value is 0.

Below:

1  Public Static voidMain (string[] args) {2     intI=3;3i=i++ + i++; System.out.println (i);4I=3;5i=i++ + (+ +)i); System.out.println (i);6I=3;7I=++i + i++; System.out.println (i);8I=3;9I=++i + (+ +)i); System.out.println (i);Ten}

The result above is 7, 8,8,9.

Iv. Process Control

If,else,switch,for, while, do, break,continue

Note Item:

1, break with the help of marking the outer loop can be interrupted (it can not only interrupt the current cycle, the label is a legal identifier)

For example:

 Public Static void Main (string[] args) {here    :        when (true) {            for (int I=1; i<10; i++) {                System.out.println (i);                 if (i==5)                      Break Here ;            }            System.out.println ("can ' t do");        }    System.out.println ("Go to Here");}

2. Continue: ends the current loop, and the statement after continue in the Loop statement will no longer execute.

3,switch ( expression ) {case:break; default:}

1), type of expression: (1), data that can be automatically converted to the basic type of int : Byte, Short,char,int (long,double, float,boolean not possible). (2), string. (3), enum type

2), penetrating phenomenon, if there is no break ; The next case statement will continue to execute .

3), default, the last remaining situation

Five, data type:

Basic data types

Type

Number of digits

Default value

Logic type

Boolean

1bit

False

Text type

Char

16bit (2byte)

' \u0000 '

Integer type

Byte

8bit (1byte)

0

Short

16bit (2byte)

0

Int

32bit (4byte)

0

Long

64bit (8byte)

0

Floating point Type

Double

64bit (8byte)

0.0

Foalt

32bit (8byte)

0.0

Notice What bit,byte,' \u0000 ' is going on:

1,1byte=8bit,1bit is a 1 -bit binary number, thesize of the Boolean 1bit, that is, only store 0 or 1, which corresponds to false or true.

2,' \uxxxx ' in \ is the escape character, U is Unicode, followed by a hexadecimal number, by this hexadecimal number to specify a character

Note

1, automatic placement/automatic removal (autoboxing/unboxing) (Automatic Boxing, unpacking): Java basic data types have their own corresponding wrapper class.

 Public Static void Main (string[] args) {    integerage =new Integer;    Integernow =age+1; // reference types are calculated directly from the base type     System.out.println (now);} // Integer class constructor source code:  Public Integer (int  value) {    this. Value = value;} 

Enum type

  

 Public enum color{    red,white,blue;}  for (Color c:color.values ()) {    System.out.println (c);}

Reference type

A reference can be interpreted as a pointer in C, but it cannot be evaluated as a C language. Before using a reference variable, you must allocate the actual storage space for him, implemented with the keyword new .

Array type

The Java programming language does not provide multidimensional arrays, but you can create arrays of arrays to implement multidimensional arrays. For example, two-dimensional arrays can also be defined like this

int a[][]=newint [3][4]; int b[]={1,2,3,4};a[0]=b;

For-each Traversal of arrays

 1  public  static  void   Main (string[] args) { 2  int  sum=0;  3  }; 4   int   i:a) { 5  sum+=i;  6   7   System.out.println (sum);  8 } 

Collection type

1.List:

LinkedList

ArrayList

2.Set:

HashSet

Linkedhashset

3.Map

HashMap

SortedMap

Key words

This: Refers to the current object. 1. Reference member variable. A local variable with the same name can be distinguished. 2, reference construction method. Must be the first executable statement that constructs a method class.

Super: Refers directly to the parent class object of its immediate parent within the subclass object. You can access the methods or properties covered by the quilt class in the parent class through Super. When you inherit a construct, you can only be the first executable statement that constructs the method, indicating which constructor method is called. Calling its properties does not have to be the first statement.

Static: The object used to declare that a member belongs to a class, not a class. Static variables (class variables) can be shared by all objects. Can be called directly through the class name. The life cycle of a static variable (method) is the entire program.

Final: A decorated class that cannot be inherited (an end-state class, such as a string). Modified method, the method cannot be overwritten. The modified variable, which is a constant. Final maintenance of security.

Java syntax basics and data types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.