Home > Java > javaTutorial > Summary of java basic knowledge

Summary of java basic knowledge

零下一度
Release: 2017-06-28 09:47:53
Original
1461 people have browsed it

Learn java and write down your own experience or summary.

Java Identifiers

Identifiers consist of letters, underscores (_), dollar signs ($), and numbers.

Identifiers cannot start with a number.

The identifier cannot be the java keyword.

Identifiers are case-sensitive.

Common Java keywords

##Basic typesObject creation, referenceSelect statementLoop statement##break, continue, return ##try, catch, finally, throw, throws, assertsynchronized##abstract, final, private, protected, public, static

Note:

The keywords in java are all lowercase letters, TRUE, NULL, etc. are not keywords.

Although goto and const are used in Java, they are still keywords in Java.

java comment

Single line comment character: //

//这是单行注释符
Copy after login

Multi-line comment: /*……*/

/*这是多行注释符*/
Copy after login

Documentation comment: /**&*/

/**
  * 文档注释能被java文档化工具javadoc识别处理,在自动生成文档是有用  */
Copy after login

Java data types

Java data types include simple data types (basic types) and composite data types (reference types)

Basic types: represent basic data that the language can process. Features: The occupied storage space is fixed.

Integer types: byte, short, int, long

Floating point types: float, double

Character type: char

Boolean type: boolean

Reference type: usually composed of multiple basic data types or reference types.

Class: class

Interface: interface

Array

【 Note】Java's string is actually a constant of the String class, it is not a Java data type.

##Keywords

Use

boolean, byte, char, double, float, int, long, short,void

new, super, this, instanceof, null

if, else, switch, case, default

do, while, for

Transfer of control

Exception handling

Thread synchronization

Modification instructions

##class, extends, interface, implements, import, package

Class, inheritance, interface, package

native, transient, volatile

##Other methods

true, false

Boolean constant

Byte type10-2^ 7 ~ 2^7-1Short integer type20-2^15 ~ 2^15-1Integer type40##Single precision floating point type40.0F##doublechar

Constant

Boolean constant: true, false

Integer constant: There are four types: byte, short, int, and long. There are four representation forms: decimal, octal, hexadecimal, and binary.

Floating point constants: There are float and double types. There are two types: decimal point form and exponential form.

Character constants: A single character enclosed by a pair of single quotes or an escaped character starting with a backslash (\). Such as 'j', '4'.

Commonly used escape characters

##Keywords

Data type

Bytes occupied

Default value

Value range

##byte

short

int

-2^31 ~ 2^31-1

##long

Long

8

0

-2^ 63 ~ 2^63-1

float

##1.4e^-45 ~ 1.4e^-45-1

##Double floating point type

8

0.0D

4.9e^-324 ~ 1.798e^+308

Character type

2

0

0 ~ 65535

##boolean

Boolean

##1

false

true, false

##Single quote characterDouble quote character##\\\r\n\f\t ##\b

##Transform Meaning character

Description

\'

\''

Backslash

Enter

Newline

Paper feed

Horizontal tab

backspace

 

字符串常量:有双引号括起来的由0个或多个字符组成的字符序列。字符串可以包含转义字符。

 

变量的取值范围

/*** Create by libra*/public class VariablesDemo {/**变量的取值范围*/public static void main(String[] args) {
        System.out.println("数据的取值范围:");
        System.out.println("字节型: " + Byte.MIN_VALUE + " ~ " + Byte.MAX_VALUE);
        System.out.println("短整型: " + Short.MIN_VALUE + " ~ " + Short.MAX_VALUE);
        System.out.println("整型型: " + Integer.MIN_VALUE + " ~ " + Integer.MAX_VALUE);
        System.out.println("长整型: " + Long.MIN_VALUE + " ~ " + Long.MAX_VALUE);
        System.out.println("单精度浮点型: " + Float.MIN_VALUE + " ~ " + Float.MAX_VALUE);
        System.out.println("双精度浮点型: " + Double.MIN_VALUE + " ~ " + Double.MAX_VALUE);

    }
}
Copy after login

 

 

输出结果:

强制转换

格式:变量 = (数据类型) 表达式

基本数据类型自动转换顺序:byteàshortàcharàintàlongàfloatàdouble.

【注意】布尔类型不能与其他类型转换。

运算符

运算符(双目单目)稍微提下,位运算符。

算数运算符:

算数运算符

双目运算符

单目运算符

+, -, *, /, %

++, --, -(负号)

Relational operators:

Operator precedence: (==,--) > ~ > ! > Arithmetic operators > Shift operators > Relational operators > &, ^, |, &&, ||, ||

Commonly used mathematical functions Math class

double ceil(double d)double floor(double d)int max(int ​​i1, int i2)int min(int i1, int i2)double random()int round(float f)long round(double d)double sqrt(double a)

##Method

##Function

int abs(int i)

##Find the absolute value of an integer

Find the smallest integer that is not less than d

##Find the largest integer not greater than d

Find the maximum number between two integers

Find the smallest number between two integers

Random number from 0 to 1

Find the integer closest to f

Find the long integer closest to d

Find the square root

double cos(double d)

Find the cosine function of d

double log(double d)

Find the natural logarithm

double exp(double x)

Find the x power of e (e^x)

double pow(double a, double b)

Find the power of a

输入输出

标准输出流System.out提供三种输出:

print():输出后不换行

println():输出后换行

printf():类似于c语言中的printf()用法

 

标准输入流System.in提供read()等方法。写一个程序便于说明及理解。

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Input {public static void main(String[] args) throws IOException {
        System.out.println("==============字符==============");char ch = (char) System.in.read();
        System.out.println("读入的字符为:" + ch);      
        System.out.println("==============字符串==============");
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String s = in.readLine();
        System.out.println("读入的字符串为:" + s);    
        System.out.println("==============数字==============");
        String x = "123";int m = Integer.parseInt(x);
        String y = "123.123";float n = Float.parseFloat(y);
        System.out.println("x = " + x);
        System.out.println("y = " + y);
    }
}
Copy after login

输出结果:

还可以使用java.util.Scanner类输入:

import java.util.Scanner;public class Input {public static void main(String[] args) throws IOException {
        Scanner s = new Scanner(System.in);       
        System.out.println("请输入数据:");
        System.out.println("请输入数据:");short a = s.nextShort();    //输入短整数int b = s.nextInt();    //输入整数long c = s.nextLong();    //输入长整数float d = s.nextFloat();    //输入单精度浮点型double e = s.nextDouble();    //输入双精度浮点型String f = s.nextLine();    //输入字符串  System.out.println("a = " + a);
        System.out.println("b = " + b);
        System.out.println("c = " + c);
        System.out.println("d = " + d);
        System.out.println("e = " + e);
        System.out.println("f = " + f);
    }
}
Copy after login

输出结果:

在Scanner中还有很多输入。

 

流程控制语句和c/c++没有太大区别。

【注意】循环结构体中可以用break 标号名; 和continue 标号名; 来跳出循环。

数组

一维数组声明有两种格式:

1.数组元素类型 数组名[];

2.数组元素类型[] 数组名;

建议用第二种声明方式

【建议】二维数组及多维数组声明方式类似于一维数组,不过建议大家用第二种数组声明方式,[]也是数组声明的组成部分。

【注意】

1.java数组和c/c++数组不同,java数组理解为数组的数组,数组的每一维的大小不一定相同。

例如:

int[][] a = new int[2][];
a[0] = new int[3];
a[1] = new int[4];
Copy after login

 

上面这段代码声明了一个二维数组,但是第0维和第1维的大小不相同。

2.求一维数组a长度经常用到的方法是a.length。若是多维数组也可以这样用:a[i].length,a[i][j].length ……。

 

对象

对象的初始化和构造方法以及变量的作用域在一个例子中说明。

public class Scope {//成员变量的作用域是整个类体int x;int y;/*对象的初始化*/{
        x = 2;
        y = 1;
    }/*对象的构造方法*/public Scope {
        x = 2;
        y = 1;
    }//方法参数a的作用域是整个方法public void method(int a) {int x = 5;for (int i = 1; i < a; i++) {//局部变量i的作用域是for循环内x = x + i;
        }
        System.out.println("x = " + x + ", y = " + y + ", a = " + a);
    } public static void main(String[] args) {//局部变量x的作用域从它的声明点扩展到它被定义的代码块结束Scope x = new Scope();
        x.method(6);
    }
}
Copy after login

【注意】构造方法的名称必须与类名相同;构造方法无返回类型;一个类可以提供多个构造方法,系统自动调用参数匹配的构造方法。

静态变量和静态方法

用static修饰的成员变量叫静态变量,也叫类变量。

访问:

在本类中直接访问。

通过类名访问。

通过类的一个对象访问。

【注意】静态变量的在存储上归属类空间,但是不依赖任何对象;(通过对象访问静态变量实质上还是访问类空间的变量)

【建议】在类外访问静态变量时通过类名访问以防混淆。

赋初值:即可以通过下面代码块赋初值,也可在定义时赋初值。

static {//code}
Copy after login

 

【注意】静态代码块只能访问静态变量;对象的创建不会执行static代码块。注意区分静态空间和对象空间。

class TalkPlace {static String talkArea = "";
}public class User {static int count = 0;
    String username;int age;    public User(String name, int yourage) {
        username = name;
        age = yourage;
    } void Login() {//直接访问同一类中的静变量count++;
        System.out.println("you are no " + count + " user");
    }void Speak(String words) {//访问其他类的类变量,通过类名访问类变量TalkPlace.talkArea = TalkPlace.talkArea + username + "说: " + words + "\n";
    }public static void main(String[] args) {
        User x1 = new User("张三", 20);
        x1.Login();
        x1.Speak("hello");       
        User x2 = new User("李四", 16);
        x2.Login();
        x2.Speak("good morning");
        x1.Speak("bye");
        System.out.println("-----讨论区内容如下:");
        System.out.println(TalkPlace.talkArea);
    }
}
Copy after login

用static修饰的方法叫静态方法,也叫类方法。

调用:

一般用类名做前缀调用。

通过对象调用

【注意】静态方法也不依赖任何对象;静态方法中只能处理静态变量和其他静态方法,绝不能访问任何归属对象空间的变量和方法。

public class FindPrime2 {public static boolean prime(int n) {for (int k = 2; k <= Math.sqrt(n); k++) {           if (n % k == 0) {return false;
            }
        }return true;
    }public static void main(String[] args) {// FindPrime2 a = new FindPrime2();for (int m = 10; m <= 100; m++) {//prime为静态方法,则可直接调用if (prime(m)) {
                   System.out.print(m + " ");
            }
        }
    }
}
Copy after login

The above is the detailed content of Summary of java basic knowledge. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Previous article:Usage of JDK Timer timer Next article:Share the source code of several common methods in ArrayList
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template