Java SE Basics (1)

Source: Internet
Author: User
Tags bitwise operators java se

Constant:

A constant is an identifier whose value is constant during run time, and constants can only be referenced in the program and cannot be re-assigned.

Naming rules for constants:

1. In Java, adding the final keyword to a variable declaration represents a constant, and adding the static keyword represents a class variable. The General union declares a variable;

2. Try to use the represented meaning intuitive to represent those numbers or strings that will appear multiple times in the program;

public static final Double PI = 3.1415926;

Declaring a variable in the Java interface is automatically added to the public static final modifier at compile time, and is automatically declared as a global variable, so the interface in Java is the best place to hold the constant.

Variable:

The most basic storage unit in Java program, its features include variable name, variable type and scope;

Each variable belongs to a specific data type and must be declared before it is used;

In essence, a variable is actually a small chunk of memory in memory, using the variable name to access the area;

Therefore, each variable must be declared before it is used, and then it must be assigned (populated) before it can be used.

Local variables and member variables:

Local variables: Variables defined inside a method or statement block

public void AddData () {

int a = 10; Variable A is a local variable, and a is not available outside the curly braces of the method.

}

Member variable: A variable that is defined internally by a method outside of a class;

public class testadd{

static final int B = 10; Variable B is a member variable that can be used within the entire class.

}

Member variables can also be divided into: class variables and instance variables

Class variables: Also known as static variables, with static decoration, it can be called directly with the class name, can also be called by the object, and all objects with a class variable is to share the same block of memory space. The static final variable must be initialized at the time of declaration or in a static block.

Instance variables: You can only call through an object without static adornments, and the same instance variable for all objects is using a different memory space.

Note : If the name of a local variable in a method is the same as the name of a global variable, the global variable is temporarily invalidated in this method.

Logic type:

Boolean data only allows values of true or FALSE, cannot replace true and false with integers of 0 or not 0, and must be lowercase.

Character type:

Char is 2 bytes in Java, Java uses unicode,2 bytes (16 bits) to represent one character, and Unicode for a Chinese character is 2 bytes.

Note: Char types are not recommended for storing character data in Java, typically using string or StringBuffer storage.

Integer type:

Range of values:

The larger the range that can be represented, the larger the memory footprint, and the choice of the appropriate type to define the integer.

Floating point type:

Float is a single-precision type, occupying 32-bit memory space, while Double is a dual-type, occupying 64-bit memory space.

Float can be accurate to 7 significant digits, double can be accurate to 16 digits of valid digits,

Access rights:

Use the access control to control access to classes, variables, methods, and construction methods, with the following four method permissions:

The variables in the interface are implicitly declared as public static final, and the methods in the interface are public by default

Notice the inheritance rule:

The parent class is public and the child class must be public

The parent class is protected, and subclasses can only be protected and public

The parent class is a private method and cannot be inherited.

That is, the access level of the subclass is equal to or higher than the level of the parent class.

Bitwise operators:

<< bitwise left shift operator, which is equivalent to multiplying by 2

>> bitwise Right SHIFT operator, shift one equivalent to divide by 2

Switch Branch:

The syntax format is as follows;

Variable type in switch: (Byte, short, int), String type, enum type, can be automatically converted to an integer type

Break control:

Mainly used in the Loop statement or switch statement, to jump out of the entire statement block;

Jumps out of the innermost loop, and continues execution of the statement below the loop.

Continue control:

Let the program immediately jump to the next cycle;

Break is the next line of code that jumps out of the current loop, executes the loop, continue stops the current loop, enters the next loop, and does not jump out of the loop.

StringBuffer and StringBuilder classes:

Their objects can be modified multiple times and do not produce new unused objects;

StringBuilder is not thread-safe (cannot be accessed synchronously);

Since StringBuilder has a speed advantage over StringBuffer, StringBuilder is recommended in most cases and requires the use of StringBuffer classes when thread safety is required;

String and StringBuffer

Simply put, there is a relationship between a variable and a constant.

The contents of the StringBuffer object can be modified, and the String object cannot be modified once it has been created, and the re-assignment is actually two objects.

The internal implementation of StringBuffer is different from string, andStringBuffer does not generate new objects when string processing, but is better than string in memory usage. Class. So in the actual use, if you often need to modify a string, such as inserting, deleting and so on, use stringbuffer to be more appropriate.

String: string defines strings with a great advantage: The compiler can set strings to be shared.

StringBuffer: Avoid adding multiple characters to the string to reallocate memory.

stringbuffer is thread-safe and slow to perform.

Java SE Basics (1)

Related Article

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.