Java Basic notes (1)

Source: Internet
Author: User

Jdk:java Development Kit
Jre:java Run-time environment
Jvm:java virtual Machines
Javac Demo01.java->demo01.class bytecode file –> class loader –> run Java Demo01

Identifiers: Alphanumeric _ $ composition, where numbers can not start, cannot be keywords, can be Chinese
Class Name: Capitalize first letter
Variable name and method name: Hump naming method MyName Showinfo () addlist ()
Constant name: All uppercase letters made up
Package name: Lowercase make up domain name inverted Com.baidu Com.qianfeng
Constants: Final data type constant name = value, value cannot be changed.
Variable: can change
Data type variable name = value;
Data type:
Basic data type:
Integer: Byte (1 bytes -128~127 byte) Short (2 bytes -32768~32767 short) int (4 byte -2147483648~2147483647 Integer) long (8 bytes 0L long)
Float type: Float (4-byte float 0.0f) double (8-byte double)
Character type: char (2 bytes Character)
Boolean: Boolean (True false Boolen)
Reference data type:
Classes Class
Arrays Array
Interface interface
SYSTEM.OUT.PRINTLN ("int max:" +integer.max_value);
SYSTEM.OUT.PRINTLN ("int max:" +integer.min_value);

Operator:
1) Arithmetic operator: +-*/% ++–
int t = a++; (1) t = A (2) a = A+1
int j = ++a; (1) a= a+1 (2) j = A

  2) assignment Operator: = + = = *=/=%= 3) comparison operator (Result of Boolean type): > < = = <= >=!=4) logical operator: && ;(logic and) | |    (Logical OR)! (logic not) & | ^ XOR A&&b (Short circuit): If A is false, the result is false. B will not be judged a&b: If A is false, the result is false. B will make a judgment.    Both A and B are executed. a| | B (Short circuit): If A is true, the result is true. B will not be judged a|b: If A is true, the result is true. B will make a judgment.    Both A and B are executed. A^b: The same is false, and the difference is true. 5) Conditional operator: Three mesh operator? : conditional expression? Statement 1: Statement 2;//When the expression is true, the result is statement 1, otherwise statement 26) string connector +7) bitwise operator: & (Bitwise AND) | (bitwise OR) ^ (bitwise XOR) ~ (bitwise reversed) >> (right Shift) < < (shift left) >>> (unsigned right shift) byte B = 1;        0000 0001 binary: binary decimal octal hex symbol binary 0 18 binary 0-7 each three bit into 1-bit decimal 0-9 Hex 0-9 a-f every four bits into one binary conversion: decimal---Binary: Except 2 take the remainder 10--"10,102" decimal: Bit right * symbol ^ (n-1) (1010) Two----> (0*2^0+1*2^1+0*2^2+1*2^3) = 108 binary---binary 1) by decimal conversion 2) 7,738--111 111 112  

Use of the Arrays tool class
Common methods: Fill ([], value) sort ([]) toString ([]) ...

Method overloads (compilation error if not an overload): In the same class, the method name is the same, the parameter list (number, type, order) is different, regardless of the return value.

JVM memory typically has five zones:
Method area Stack Local method area register
Method Area: Information of the stored class, constant pool, static method area
Stack (value type): The local variable that holds the calling method
Variables stored in the stack, scope end immediately disappear
Heap (reference type): Save array or Reference object
Characteristics:
1 Allocating memory first address
2 has a default value
3 GC (garbage Collection)
Local Method Area: Implementing a call to a class library

    注意:在常量池中,java默认创建-128-127之间的常量对象    对于字符串常量会首先去常量池查找,如果不存在就创建字符串常量值类型和引用类型参数的区别:两个数的互换如果参数为值类型:传递的是值的副本,形参不能改变实参的值如果参数类型为引用类型:传递的是地址,形参可以改变实参的值

/*
String constant object: persisted in string constant pool
Summary: The string is saved in a constant pool (whether it existed previously or was newly created through string str = new String ("string").
*/
public static void Main (string[] args)
{
String str = "Hello";
String str1 = "Hello";
System.out.println (STR==STR1); Result is True

    show(str);//将str变量的内容“你好”传递给方法    System.out.println(str);  //你好//下面这条语句创建了几个对象?此时只会创建一个对象,因为”你好“已经在常量池中存在    String str3 = new String("你好");    String str4 = str3;    str3 = "hi";  //“hi”字符串在常量池不存在,则会在常量池中创建一个对象,str3就指向该对象    System.out.println(str4); //你好}static  void  show(String str){    str = "hello";  //方法中重新赋值}

System.arraycopy (Object src,int srcpos,object dest,int destpos,int length)
SRC: Source Array
Srcpos: Start copying from the subscript of the source array
Dest: Target Array
Destpos: The location of the copy of the destination array
Length: The lengths of the copied array elements

The difference between a local variable and a global variable (member variable):
Local variables: Initialized as the method is stacked, and the method goes out of the stack
No default value, must be declared after assignment
Save in the stack
The scope is the declared position to the end of the structure where it resides

成员变量:随着初始化对象而创建,会随着对象的回收而消失        有默认值的        保存在堆中        作用域在整个类中注意:当局部变量和成员变量重名时,局部变量会覆盖全局变量

Class Person
{
Member variables: Global variables
String name;
int age;
public void Say () {
int a = 10; Local variables
int age = 60; Duplicate name, local variable overrides global variable
System.out.println ("age=" +age+ ", a=" +a);
}
}

Encapsulation: Encapsulates internal details, allowing properties to be privately accessed through non-private methods.
Read:
Data Type GetXXX ()
Write:
void setxxx (data type parameter)
Access modifiers:
Private: Only the internal of the current class can be accessed
[Default]: defaults, can be accessed in the same package
Protected: Protected, can be accessed in the same package or in a subclass of a different package
Public: Publicly available, all classes are accessible
This: the current object. Use this to access member variables when local variables and member variables have duplicate names

Static keyword:
1 Static member variables:
Characteristics:
1) and class loading at the same time, will open up space in the method area, initialize the data
2) Access by class name. Class name. static member Variable
3) commonly used to store shared data for all classes

2 静态成员方法:特点:1)静态方法中只能访问静态成员(不能使用this)2)非静态方法中可以访问静态成员也可以访问非静态成员静态成员变量和非静态成员变量区别:1)生命周期    静态成员变量随着类的加载而加载,在方法区初始化,在程序结束即消失    非静态成员变量随着对象的初始化,在堆中创建,在对象被垃圾回收时而消失2)存储    静态成员变量通常保存对象共享的数据    非静态成员变量是保存某一个对象的数据3)访问方式    静态成员变量通过类名直接访问,也可以通过对象访问    非静态成员变量只能通过对象访问

Building blocks of code: Precedence over construction method execution, primarily for initializing member variables
Each time the object is created, it executes
Static code block: static{}, used to initialize static member variables. Executes as the class is loaded, only once;//non-static members cannot be directly referenced in static code blocks
Note: In program optimization, excessive use of static is not recommended because it remains in memory for long periods of time
{
Normal code block: scope
int i= 6;
System.out.println ("i–>" +i);
}//i Scope disappears

Object Initialization process:
1 load the class's bytecode file into the JVM's method area
2 Open memory space for static variable in static area, assign initial value
3 loading static code blocks, initializing static member variables
4 Open heap Space (member variable), initialize the member variable
5 Loading Construction Code blocks
6 Loading Construction methods
7 Assigning the first address of a heap space to a reference to an object in the stack

Design Pattern: Single case
Problem solving: A class has only one object in memory
1 Construction Method Privatization
2 construction method After privatization, an object cannot be created, only the class itself provides the object
A hungry man: instantiating an object when declaring a class reference
Lazy: In the method of getting a class, first determine whether the object is empty, if NULL to instantiate
3 provides a way for the outside world to gain access to the object

懒汉式(线程不安全的)饿汉式(线程安全的)

A Hungry man type
Class single
{

    //构造方法私有化:不能在本类的外部通过new创建对象    private Single(){    }    //定义一个本类的对象    private static Single single = new Single();    //向外界提供可访问的方法,返回当前类的对象    public static Single getInstance(){        return single;    }

}

Lazy type
Class Single2
{
Private Single2 () {}

private static Single2 single ;public static Single2 getInstance(){    if(single == null)        single  = new Single2();    return single;}

}

When you create a subclass object, you first call the parent class's parameterless constructor, and then call the corresponding constructor method of the subclass
When a subclass is called by Super () to call the parent class with a parameter constructor, the child class object is not called, and the parent class does not have an argument constructed method
Super: Parent Class object

Child class object initialization process:
1 Parent class static code block
2 sub-class static code block
3 parent class building blocks of code
4 How to construct a parent class
5 Subclass Construction Code block
6 sub-class construction methods

Class Parent
{
private String name;
public int age;
static{
System.out.println ("Static code block of the parent class");
}
{
SYSTEM.OUT.PRINTLN ("Construction code block of the parent class");
}
Parent () {
System.out.println ("Construction method of the parent class");
}
public void Say () {
System.out.println ("Paternal Age:" +age);
}
}

Class Child extends Parent
{
private int age;
static{
System.out.println ("Subclass Static code block");
}
{
SYSTEM.OUT.PRINTLN ("Subclass Construction Code block");
}
Child () {
System.out.println ("Sub-Class construction method");
}
public void Sayhi () {
Super.age = 100;
This.age = 50;
System.out.println ("Subclass Age:" +age);
Say ();
}
}

this和super:this:代表当前对象。this.成员。       this()本类的构造方法,必须在第一行super:代表父类对象,并不是一个引用,没有父类的指向(因此super不能作为参数传递)        可以通过super.父类成员来访问父类的成员        也可以通过super()调用父类的构造方法,必须在第一行this()和super()不能同时出现this和super关键字不能出现在静态方法中

Child class Object name = new Subclass ();
The methods that an object can invoke are:
Methods inherited by the parent class
Subclasses overriding methods of the parent class
New ways to subclass subclasses
Parent class Object name = new Subclass ();
The methods that an object can invoke are:
Methods inherited by the parent class
Subclasses overriding methods of the parent class
Override (Overwrite): precondition must inherit
Attention:
1 private methods of the parent class cannot be overridden
2 Subclasses overriding method permissions for a parent class must be greater than or equal to the method permission in the parent class
3 static methods static only static override of –> parent class cannot be overridden
Parent class: public static void Gg () {}
You cannot override the GG () method in a subclass to define your own GG () method
4 when a method in a parent class has a return type, the subclass overrides the parent class method, and the return type can be either a parent class type or a subclass of the parent class

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Basic notes (1)

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.