Summary of java basic knowledge
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
##Keywords |
Use |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
boolean, byte, char, double, float, int, long, short,void | ##Basic types||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
new, super, this, instanceof, null
| Object creation, reference||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if, else, switch, case, default
| Select statement||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
do, while, for
| Loop statement||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Transfer of control |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Exception handling |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Thread synchronization |
##abstract, final, private, protected, public, static | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Modification instructions |
##class, extends, interface, implements, import, package | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Class, inheritance, interface, package | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
native, transient, volatile |
##Other methods | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
true, false |
Boolean constant |
##Keywords |
Data type |
Bytes occupied |
Default value |
Value range |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
##byte
| Byte type1 | 0 | -2^ 7 ~ 2^7-1 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
short
| Short integer type2 | 0 | -2^15 ~ 2^15-1 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
int
| Integer type4 | 0 | -2^31 ~ 2^31-1 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
##long |
Long | 8 | 0 | -2^ 63 ~ 2^63-1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
float | ##Single precision floating point type4 | 0.0F | ##1.4e^-45 ~ 1.4e^-45-1 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
##Double floating point type |
8 | 0.0D | 4.9e^-324 ~ 1.798e^+308 | char | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Character type |
2 | 0 | 0 ~ 65535 | ##boolean | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Boolean ##1 |
false | true, false |
##Transform Meaning character |
Description |
|||||||||||||||||||||||||||||||||||||||||||||||
\' | ##Single quote character||||||||||||||||||||||||||||||||||||||||||||||||
\''
| Double quote character||||||||||||||||||||||||||||||||||||||||||||||||
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
输入输出标准输出流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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.
