Scott の 博客 Scott の 博客
首页
  • Data Structure and Algorithm
  • Java
  • 面试
  • Drafts
  • C++
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Scott

恋爱中
首页
  • Data Structure and Algorithm
  • Java
  • 面试
  • Drafts
  • C++
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Data Structure and Algorithm

  • Java

    • Java基础知识&面试题总结
    • Java
      • Java Function in a class
      • How Java code gets executed
      • Types
        • Variables and Constants
        • Primitive and Reference Types
        • Casting
        • Numbers, Strings and Arrays
        • Formatting Numbers
        • Read Input
    • Control Flow
    • Clean Coding
    • Debugging and Deployment
    • Untitled
    • Refactor towards OOSD
    • Inheritance
    • Untitled
    • Exceptions
    • Generics
    • Collections
    • Lambda-Expression
    • Streams
    • Concurrency and Multi-threading
    • The Executive Framework
    • 4
    • 1
  • c++

  • 面试

  • Bilibili_Java

  • Python

  • All kinds of Drafts

  • High Integrity Information System

  • 左神算法课

  • 个人笔记
  • Java
Scott
2021-11-23
目录

Java

  1. Java Function in a class
  2. How Java code gets executed
  3. Types
    1. Variables and Constants
    2. Primitive and Reference Types
      1. Primitive: for storing simple values
      2. Reference: for storing complex objects
    3. Casting
      1. Implicit Casting
      2. Explicit Casting
      3. Wrapper classes
    4. Numbers, Strings and Arrays
      1. Arithmetic Expressions
      2. String is also a reference type
      3. Arrays
    5. Formatting Numbers
    6. Read Input

(Code 在 code docs 里)

# Java Function in a class

Example:

// main class

// access modifier is public

// ParscalNamingConvention
public class Main {
    
    // main function 
    // camelNamingConvention
    public void main() {
    
    } 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
image-20220706151818885

# How Java code gets executed

  • Compilation
    • Java compiler (comes with JDK) compiles source code to byte code
    • Use the key word: javac (stands for java compiler)
image-20211123095419368
  • Execution
    • OS/platform independent
    • Use the key word: java (using the java virtual machine)
image-20211125110523165

# Types

# Variables and Constants

// int: identifier 
int age = 30;

int final LIFE_SPAN = 31;
1
2
3
4

# Primitive and Reference Types

image-20220706155718087 image-20220706160130611

# Primitive: for storing simple values

image-20211126091617379

Example:

int age = 30;

int viewsCount = 123_456_789; // use underscore to make the number more readable

long viewsCount = 3_123_456_789L; // 'L' is not case-sensitive; 'L' is necessary, otherwise the machine will treat it as an int 

float price = 10.99F; // F is not case-sensitive 

char letter = 'A';

boolean isEligible = false; 
1
2
3
4
5
6
7
8
9
10
11
image-20211126093009848
int x = 1;
int y = x;

x = 2; 

System.out.println(y); 
// the result is still 1, because x and y are stored in different space of memory, and they are completely independent of each other
1
2
3
4
5
6
7

# Reference: for storing complex objects

  • Always needs to allocate memory by using new
byte age = 30; // don't need to allocate memory using 'new'
Date now = new Date(); // a new instance of the Date class; need to allocate new memory
now.getTime(); // the reference type has members 
1
2
3
image-20211126093311474
Point point1 = new Point(1, 1);
Point point2 = point; // they point to the same memory location, have the same address/reference   

point1.x = 2; 

System.out.println(point2.x) // becomes 2
1
2
3
4
5
6

# Casting

# Implicit Casting

  • It happens whenever we are not going to lose data (there is no chance for data loss)
  • byte --> short --> int --> long --> double --> float
// implicit casting: 
short x = 1; // 2 bytes 
int y = x + 2; // int: 4 bytes; x gets implicit casting to int
// It happens when no data is going to lost
System.out.println(y); // y = 3

double x = 1.1;
int y = x + 2; // cannot do this, int is smaller than double 
double y = x + 2; // this is correct, 2 gets auto casted to double  
1
2
3
4
5
6
7
8
9

# Explicit Casting

  • Float - double - long - int - short - byte
double x = 1.1;
int y = (int) x + 2; // x becomes 1
1
2

# Wrapper classes

  • Casting happens among compatible types (string cannot be casted to a number)
  • Therefore, wrapper classes is needed to convert a string to numerical value
  • Since input are often string, these classes are pretty useful
String x = "1";
int y = Integer.parseInt(x) + 2; // Integer is the wrapper class for the int primitive type

String x = "1.1";
double y = Double.parseDouble(x);
...
1
2
3
4
5
6

# Numbers, Strings and Arrays

# Arithmetic Expressions

int result = 10 / 3; // 3
double result = (double) 10 / (double) 3; // 3.3333333

int x = 1;
	int x = x++; // x = 1;
// or 
	int x = ++x; // x = 2;
1
2
3
4
5
6
7
image-20211126102210298
int result = Math.round(3.3F); // 2; float to int or double to int
int result = (int) Math.ceil(3.3F); // 4, need to explicit cast
int result = (int) Math.floor(3.3F); // 3, need to explicit cast
int result = Math.max(1, 2); // 2
int result = Math.min(1, 2); // 1
double result = Math.random(); // a double 0~1
double result = Math.round(Math.random() * 100); // an integer 0~100
int result = (int) (Math.random() * 100); // an integer 0~100
1
2
3
4
5
6
7
8

# String is also a reference type

// from java.lang, auto imported
String message = new String ("Hello World"); // however, new String is redundent

String message = "Hello World" + "!!"; // this is the shorthand way

message.endsWith("!!"); // true
message.startsWith("!!"); // false
message.length(); // 12
message.indexOf("e"); // 1
message.indexOf("sky"); // -1, check to see if it exists
message.replace("!", "*");// target and replacement are parameters
// ! and * are arguments;
// Also, the original string is not changed, it’s just return a new stirng, because strings are immutable 
message.toLowerCase();
message.trim() // delete unnecessary white spaces in the beginning or in the end of the string
    
// Escape sequences 
String message = new String ("\"Hello World\""); 
String message = new String ("c:\\Hello World\\..."); 
String message = new String ("Hello World\n"); 
String message = new String ("\tHello World"); 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# Arrays

int [] numbers = new int[5]; // size is 5
int [] numbers = {1,2,3,4,5}; // direct assignment
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));

// Multi-dimensional Arrays
// 2-d array
int [][] matrix = new int [2][3];
int [][] matrix = {{1,2,3}, {4,5,6}}
System.out.println(Arrays.deepToString(matrix));

1
2
3
4
5
6
7
8
9
10
11

# Formatting Numbers

// NumberFormat is an abstract class
// cannot create an instance of this class 
NumberFormat currency = NumberFormat.getCurrencyInstance();
String result = currency.format(123_456_7.891);
System.out.println(result); // $1,234,567.89

NumberFormat percent = NumberFormat.getPercentInstance();
String result = percent.format(0.1); 
System.out.println(result); // 10%

// method chainning 
String result = NumberFormat.getPercentInstance().format(0.1); 
1
2
3
4
5
6
7
8
9
10
11
12

# Read Input

Scanner scanner = new Scanner(System.in);

System.out.print("Age: ");
byte age = scanner.nextByte();
System.out.println(age); // age to string

System.out.print("Name: ");
String name = scanner.next(); // just read one word 
String name = scanner.nextLine().trim(); // read a line
1
2
3
4
5
6
7
8
9
上次更新: 2022/07/21, 12:07:30
Java基础知识&面试题总结
Control Flow

← Java基础知识&面试题总结 Control Flow→

最近更新
01
day01-Java基础语法
08-31
02
1
08-29
03
路线
08-01
更多文章>
Theme by Vdoing | Copyright © 2019-2022 Evan Xu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×