mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4
486 words
2 minutes
Core Java Study Day 01
2022-07-08

Java study and review. This article mainly references Java Core Technologies Volume as the object of study.

Chapter 1: Java Overview#

Java White Paper

Simplicity, Object-oriented, Distributed, Robust, Secure, Architecture-neutral, Portable, Interpreted, High performance, Multithreading, Dynamism

  • Java applet

A Java program that runs in a web page: applet

  • JavaScript and Java

Apart from the name, the two are not related. Java is strongly typed and catches errors more effectively than JavaScript

Chapter 2: Java Environment#

  • Java Terminology
TermAbbreviationExplanation
Java Development Kit (JDK)JDKSoftware used by programmers to write Java programs
Java Runtime Environment (JRE)JRESoftware used by users to run Java programs
Server JRE (Server Runtime)Software for running Java programs on a server
Standard Edition (SE)SEJava platform for desktop or simple server applications
Enterprise Edition (EE)EEJava platform for complex server applications
Micro Edition (ME)MEJava platform for small devices
Java FXAn alternative toolkit for graphical user interfaces, provided in some Java SE releases prior to Java 11
OpenJDKA free open-source implementation of Java SE
Java2J2An obsolete term used to describe Java versions between 1998 and 2006
Software Development Kit (SDK)SDKAn obsolete term used to describe the JDK between 1998 and 2006
UpdateuOracle’s term, indicating bug-fix releases prior to Java 8
NetBeansOracle’s integrated development environment
  • Java Installation & Compilation
# 配置环境变量
javac --version # 查看java版本
# 命令行编译
javac welcome.java
java welcome
  • Integrated Development Environments

Eclipse | IntelliJ IDEA | NetBeans

  • JShell

JShell: Java’s interactive shell

Chapter 3: Java Basic Programs#

  • CamelCase Naming

myClass

  • Comments
//注释
/*注释*/
/**
*自动生成文档
*注释
*/

1 Data Types#

  1. Integers
int4 bytes-2^32 ~ 2^32 - 1
short2 bytes-2^16 ~ 2^16 - 1
long8 bytes-2^64 ~ 2^64 - 1
byte1 byte-2^8 ~ 2^8 - 1

1L/1l Long integer

0x Hexadecimal 0 Octal 0b/0B Binary

  1. Floating-point
float4 bytesApproximately ±3.40282347E+38F 6~7 digits
double8 bytesApproximately ±1.79769313486231570E+308 15 digits

float 1f/1F

double 1D/1d

NaN <= 0/0 || sqrt(-n)

1356cde21b065d583b5134f9365d4fd4.png

  1. char type
\\b 退格 \\t 制表 \\n 换行 \\r 回车 \\" 双引号 \\' 单引号 \\\\ 反斜杠
  • Unicode

16-bit: early era

Code point U+ 16-bit

Planes 2–17 (including supplementary characters U+10000 ~ U+10FFF)

  1. boolean type

Boolean type false || true Logical evaluation

2 Variables#

  • Initialization

After defining, you must explicitly initialize before you can use it

final constants enum types

3 Operators#

+ - * / %

  • Math

sqrt pow floorMod

Trigonometric functions sin cos tan atan atan2

Logarithms exp log log10

Pi and e approximations Math.PI Math.E

You can import static java.lang.Math.* to use them directly

  • Type conversion

Low precision -> high precision is lossless

High precision -> low precision incurs loss - explicit casting (int) | …

+= *= %= ...
k++ ++k
== != > < >= <=
&& ||
x?a:b //(true:false)
& | ^ ~ >> <<

Operator precedence

596cc3093bc2ab5fbb56aba8146403af.png

4 Strings#

"...".substring(l,r) //[l,r)
"a"+"b"
"a".repeat(3) //ans = "aaa"

Strings are immutable

  • equals and ==

== checks whether strings are in the same location, and can only reliably compare shared literals; for non-shared concatenations or substring there can be errors

Use a.equals(b) or a.compareTo(b) for comparing strings

  • Empty strings and null strings

"" null

  • Code points and code units
int index = a.offsetByCodePoints(0,i); //position of ith code point
int cp = a.codePointAt(index); //get the ith code point
//UTF-16 surrogate pairs require two code units; cannot use charAt(pos)
  • Building strings
StringBuilder builder = new StringBuilder(); //String builder
builder.append('a');
builder.append(b);
String res = builder.toString(); //generate string

5 Input/Output#

  • Input
Scanner in = new Scanner(System.in);
in.nextLine(); //read a line
in.next(); //read with space delimiter
in.nextInt(); //read int
in.nextDouble(); //read Double
in.hasNext(); //check if there is more input
  • Output
System.out.println();
System.out.printf(“%8.2f",x); //similar to printf in C
// d decimal x hexadecimal o octal f fixed-point e exponential
// s string c character b boolean h hash code
  • File Input/Output
Scanner in = new Scanner(Path.of(""),StandardCharsets.UTF_8);
PrintWriter out = new PrintWriter("",StandardCharsets.UTF_8);

6 Control Flow#

// 块作用域
{}
// 条件语句
if(){
}else if(){
}else {}
// 循环
while(...){} // Evaluate condition first, then execute
do ... while(...); // Execute first, then evaluate
for(int i = ..;i <= .. ; i++) {}
switch (...){
case ...://Label can be char byte short int enum constants string literals
...
break;
...
default:
...
break;//Will stop only when break is encountered
}
//break continue goto
//break tag; Similar to goto jumping to tag:
  • Large numbers

BigInteger BigDecimal

Use valueOf(x) to convert x

7 Arrays#

//数组定义
int[] a = new int[len];
int[] b = {1,2,3,4};
new Type[0] || new Type[] {} //Length-0 arrays, different from null
//for each loop
for(int i:a) ... //process array or other element collections
//- Array copying
a = b;
a = Array.copyOf(b,copyLen);
//Array sorting
Arrays.sort(a); //Quicksort Math.random() -> [0,1)
//Multidimensional arrays
int[][] a = new int[lenA][lenB];
a = {
{...},
{...}
};
for(int[] i:a)
for(int j:i)
...
//Jagged arrays
int[][] a = new int[N][]; //Array of N references
a = {...};
Share

If this article helped you, please share it with others!

Core Java Study Day 01
https://dreaife.tokyo/en/posts/java-core-tech-day1/
Author
dreaife
Published at
2022-07-08
License
CC BY-NC-SA 4.0

Some information may be outdated

Related Posts Smart
1
Core Java Study Day 02
cs-base This article mainly discusses core Java topics including the basic concepts of object-oriented programming, class definition and usage, operations on predefined classes such as LocalDate, construction and encapsulation of custom classes, static methods and fields, package management, creation and use of JAR files, and techniques for writing documentation comments. It emphasizes important design practices such as data encapsulation, initialization, and class design principles.
2
Java IO
cs-base Java IO covers the basic concepts of input and output streams, including the classification of byte streams and character streams and their common classes such as InputStream, OutputStream, Reader, and Writer. Byte streams are used for raw byte data, while character streams are used for character data. Buffered streams improve performance by reducing the number of IO operations. Adapter and decorator patterns are widely used in Java IO streams to enhance functionality and coordinate different interfaces. Java IO models include synchronous blocking IO, non-blocking IO, and asynchronous IO, each suited to different scenarios.
3
Java Atomic Classes and Common Concurrent Containers
cs-base This article introduces Java atomic classes and common concurrent containers, including atomic classes for primitive types (such as AtomicInteger, AtomicLong, and AtomicBoolean), atomic array classes, atomic reference classes, and atomic field updater classes. It also explains the features and usage scenarios of concurrent containers such as ConcurrentHashMap, CopyOnWriteArrayList, ConcurrentLinkedQueue, BlockingQueue, and ConcurrentSkipListMap.
4
Java threadLocal
cs-base ThreadLocal provides thread-local variables, allowing each thread to have an independent copy and avoid interference with other threads. Key points include the data structure of ThreadLocalMap, the hash algorithm, conflict resolution, cleanup of expired keys, and the implementation principles of the set and get methods. InheritableThreadLocal can share parent thread local variables with child threads to solve data propagation in asynchronous scenarios. In practice, ThreadLocal can be used for logging and traceId propagation in distributed systems.
5
Java Concurrent Programming
cs-base This article introduces the basics of Java concurrent programming, including the definitions of threads and processes, Java thread implementation mechanisms, thread life cycle, the differences between concurrency and parallelism, the concepts of synchronous and asynchronous execution, and the advantages and disadvantages of multithreading. It also discusses thread safety, deadlocks and how to avoid them, the use of the volatile keyword, the difference between optimistic and pessimistic locking, and how to use thread pools and Future to improve execution efficiency. Finally, it introduces the application scenarios and principles of tools such as CyclicBarrier and CountDownLatch.

Table of Contents