Java Learning--the first day of basic knowledge--notes

Source: Internet
Author: User
Tags arithmetic operators logical operators modifier

Today's content
Review the contents of the basic class
static statics keyword
code block

Java basic syntax three days before
Operator
Arithmetic operators
+ - * / % ++ --

/: The result of dividing an integer is an integer 3/-2=-1
%: the symbol of the result of the modulo depends on the divisor, and the divisor is irrelevant 3%-2=1-3%2=-1-3%-2=-1

+ + and--before and after questions
Use alone: no difference
Other operations are involved:
+ + before, increment first, and then participate in other operations with the result of self-increment
+ + in the next, the variables are first involved in other operations, and then the self-increment

Assignment operators
= += -= *= /= %=
Relational operators
> < >= <= = = =!
logical operators
& | ^ ! && | |
& and &&amp: False when False, only true if both left and right are true
| and | | : True if true, the result is false only if both the left and right sides are false
^: only if the left and right sides are not the same, one is true and the result is true when false

The difference between & and &&
&& has short-circuit effect, if false on the left of operator, the right side does not execute
& has no short-circuit effect, regardless of whether the operator is false or true on the left, the right side will execute

Ternary operators
Format:
Boolean expression 1: expression 2;


Process Control Statements
Sequential structure: The code executes sequentially from top to bottom

Select structure
If statement
Format one:
if (Boolean expression) {
Statement body;
}
Format two:
if (Boolean expression) {
Statement body 1;
}else{
Statement body 2;
}
Format three:
if (Boolean expression 1) {
Statement body 1;
}else if (Boolean expression 2) {
Statement body 2;
}else if (Boolean expression 3) {
Statement Body 3;
}
...
else{
Statement body n+1;
}

Switch statement
Format:
switch (expression) {
Case value 1:
Statement body 1;
Break
Case Value 2:
Statement body 2;
Break
...
Default
Statement body n+1;
Break
}

Precautions:
(1) The type of data for an expression can only be a byte short int char String enumeration
(2) Case penetrating problem, the break in the box can be omitted, if omitted, will continue to execute the contents of other
You do not need to determine the value of the case until you encounter a break or the closing brace position of the switch statement
(3) Default position problem, the default statement can precede all case statements, or can be located after all case statements
It can also be executed in multiple case statements and can even be omitted. The location of the default statement does not affect the execution process

Loop structure
For loop
Format:
for (initialization expression; Boolean expression; post-loop execution expression) {
Circulation body;
}
While loop
Format:
while (Boolean expression) {
Circulation body;
}

Initialize the expression;
while (Boolean expression) {
Circulation body;
The execution expression after the loop;
}

Do...while Cycle
Format:
do{
Circulation body;
}while (boolean expression);

Jump out of a looping statement
Break
Use scenarios: use in loops and switch statements
Characteristics:
The end of the most recent cycle
Continue
Usage scenarios: can only be used in loops
Characteristics:
End this cycle and continue the next cycle

Scanner class and Random class
Scanner is used for keyboard entry
Random number due to production

Steps:
(1) Guide Package
Import Java.util.Scanner;
Import Java.util.Random;
(2) Create an object
Scanner sc = new Scanner (system.in);
Random r = new Random ();
(3) Calling method
Scanner class
Nextint ()
Nextline ()
Random class
Nextint (int n): The generated random number is an integer in the range of [0,n]

Key words


Identifier
Used to name packages, classes, methods, variables, constants, and so on.

Composition rule:
(1) Number
(2) Letters
(3) Underline (_)
(4) dollar symbol ($)

Naming conventions:


Comments
//

/*
*/

/**
*
*/

Data type

Method
Overview of methods
What is a method: a block of code that implements a function

Advantages:
Improve Code reusability
Format of the method
Modifier returns a value type method name (parameter list) {
Method body;
return value;
}

Modifier: public static
return value type:
And the return value is the corresponding relationship, if the return is 10 such an integer, then the return value type is the int type
The return value type can be a specific type, and if there is no return value then the type must write void and cannot be omitted
Method Name:
Use the legal identifier name, but also in accordance with the norms, see the meaning of known
Parameter list: can have more than one parameter, or can have no parameter
Data type
Variable name
Method body: The body of the function execution
Return value: The result of a method run
Return: Two functions, one is the result of the return method execution, and the other is the End method


Two clear
return value type
Parameter list

Invocation of the method
Methods that have return values
(1) Separate calls
(2) Output call
(3) Assignment invocation
Method with no return value
(1) Separate calls

Overloading of methods
Conditions:
(1) In the same class, the method name is the same
(2) The parameter list is different (the number is different, the type is different, the order is different)

The parameter passing problem of the method
Parameter passing of the base data type: Pass the value, do not change the original value
Parameter passing of the reference data type: The address value is passed and the original value is changed

Array
An overview of arrays

Features of the array
(1) is a container that can store multiple "same-type" data
(2) You can store either the base data type or the data of the reference data type
(3) fixed length once defined
(4) has an integer index, the length of the range in the 0~ array-1

Initialization of the array
Dynamic initialization: The length of the given array, the default initial value assigned by the system

data type [] Array name = new data type [length];
int[] arr = new INT[5];

Static initialization: The value of the given element, the length of the array determined by the system

data type [] Array name = new data type []{element 1, element 2,...};
int[] arr = new int[]{1,2,3,1};

data type [] Array name = {element 1, element 2,...};
Int[] arr = {1,2,3,1};

Memory diagram of an array
int[] arr = new INT[5];
int[] arr2 = arr;

Array values and assignments
Value:
Array name [index]
ARR[1]

Assignment value:
Array name [index] = new value;
ARR[1] = 10;

Index: The length of the range in the 0~ array-1
Length: Length property gets the lengths of the array

Operation of the array
Iterate: Remove the elements in the array sequentially
Max: Gets the maximum and minimum values in the array
Find: Given the specified element, query its first occurrence in the array index
Invert: The elements in the array are reversed, the elements of the No. 0 index are exchanged with the elements of the arr.length-1 index, the elements of the 1th index are exchanged with the elements of the arr.length-2 index, and so on
Sort (select sort, bubble sort): Rearrange elements in an array in some order (small to large, or large to small)

Array sorting:
Int[] arr = {2,1,9,5,8};//{1,2,5,8,9}
Bubble Sort: Compare the previous element with the next one, and if the previous element is larger than the back, swap the position, and all the elements can be sorted in ascending order.

Select sort: Select one of the elements, and then compare the other elements in the array

Defining the standard Student class
Member variables
*private Retouching
Construction method

Non-parametric construction method
public class name () {}
A method of *this construction
public class name (argument list) {}

Member Methods

Getters and Setters Methods *this
public void Method Name () {method Body}



Static keyword
Static modifiers, which can be decorated with member variables and member methods, and cannot be decorated with construction methods

The characteristics of static:
(1) A modified member is not part of a specific object, but belongs to a class and is shared by all objects.
(2) can be called directly by the class name.
(3) loaded with the load of the class, appearing before the Main method, appearing before the object

Precautions:
Static methods can only call static member variables and member methods, and cannot call non-static member variables and member methods
A non-static method can call either a static member or a non-static member

Is there a static method in this? No

The advantages and disadvantages of static
Advantages:
Saves memory space
Call is more convenient, directly with the class name.

Disadvantages:
limitations, static only calls static members

Static selection:
Member variables:
When a member variable is shared by all objects, it can be modified with static
When a static member method needs to call this member variable, the member variable can also be used as static because static can only be called
Member Methods:
(1) The general method must not use the static decoration, in the tool class often for the convenience of method calls will be static decoration method
(2) If the method needs to be called directly by the main method, then the method can be modified with static


Tool class
Characteristics:
(1) The construction method is modified by private
(2) methods are static modified


Block of code (4)
Local code block
Location: In the method
Format:
{
}
Function: Scope of the qualifying variable
Building code Blocks
Location: Outside of method in class
Format: +
{
}
Function: Extract the common content in the construction method
Characteristics:
Each time an object is created, it executes
Before the construction method executes
Static code block
Location: Outside of method in class
Format:
static{

}
Role: To do some preliminary data preparation or initialization of the operation
Characteristics:
Executes as the class is loaded and executes only once
Precedence over construction code block execution
takes precedence over the main method execution

Synchronizing code blocks (multithreaded learning)

Java Learning--the first day of basic knowledge--notes

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.