Java Basic Knowledge
Published:
Simple Introduction to Java
JRE includes {JVM + Runtime Library}
JDK includes JRE + {Compiler,debugger,etc.}
Variable
short: 2 bytes;
int: 4 bytes;
long: 8 bytes;
float: 4 bytes;
double: 8 bytes;
char: 2 bytes
There are two types of java variables: basic type and reference type
int
float, double
boolean
char
String a = “hello”; (reference type)
Calculation
There might be some errors when calculating two float numbers:
public class Main{
public static void main(String[] args){
double x = 1.0/10;
double y = 1-9.0/10;
double r = Math.abs(x-y);
if(r<0.00001){
System.out.println("x=y");
}
else{
System.out.println("x!=y");
}
}
}
HW1: The way to calculate a Quadratic Equation of One Variable:
public class Main {
public static void main(String[] args) {
double a = 1.0;
double b = 3.0;
double c = -4.0;
double r1 = 0;
double r2 = 0;
r1 = (-b + Math.sqrt(b*b-4*a*c))/(2*a);
r2 = (-b - Math.sqrt(b*b-4*a*c))/(2*a);
System.out.println(r1);
System.out.println(r2);
System.out.println(r1 == 1 && r2 == -4 ? "right" : "wrong");
}
}
The new way to represent a char:
char a = '\u3e3d' // Use Hex to represent
Array
public class Main {
public static void main(String[] args){
int[] ns = new int[5];
ns[0] = 1;
ns[1] = 2;
ns[3] = 3;
ns[4] = 4;
ns[5] = 5;
System.out.println(ns.length)
}
}
To be more simplify:
public class Main {
public static void main(String[] args){
int[] ns = {1,2,3,4,5};
System.out.println(ns.length)
}
}
The important thing about the array is that there is a ns points to the first element of the array; If using String array:
String[] names = {
"ABC", "XYZ", "ZOO"
};
There is a “pointer” names pointing at an array, the elements of the array is the address of the string
Java Input and Output
Formalized Input and Output:
public class Main {
public static void main(String[] args) {
double d = 3.1425926;
System.out.printf("%.2f\n",d); //The result is 3.14
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in); //Create a scanner object
System.out.print("Input your name: ");
String name = scanner.nextLine(); //Read a line of input and get the string
System.out.print("Input your age: ");
int age = scanner.nextInt(); //Read a line of input and get the integer
System.out.printf("Hi, %s, you are %d\n",name,age);
}
}
Traverse the Array in the reverse way
public class Main{
public static void main(String[] args){
int[] ns = {1,2,3,4,5};
for (int i = 0; i<5; i++){
System.out.println(ns[4-i]);
}
}
}
Sort the array
Despite there are a lot of ways to implement the array sorting
import java.util.Arrays;
public class Main {
public static void main(String[] args){
int[] ns = {1,2,3,4,5};
Arrays.sort(ns);
System.out.println(Arrays.toString(ns));
}
}
Object Oriented Programming
public class Main {
public static void main(String[] args) {
Person p = new Person("Xiao Ming", 15);
System.out.println(p.getName());
System.out.println(p.getAge());
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
}
Overload
The name of the methods are the same, but the parameters are different
class Hello {
public void hello(){
System.out.println("Hello, world!");
}
public void hello(String name){
System.out.println("Hello,"+name+"!");
}
}
extends: If the student class has construction method, the person needs to have construction method and there should be super() in the student class.
The name of the variable cannot be the same, the name and parameter of the methods can be the same, the latter case is also called override.
Override
class Person {
public void run() {
System.out.println("Person.run");
}
}
class Student extends Person {
@Override
public void run() {
System.out.println("Student.run");
}
}
Abstract class
public class Main {
public static void main(String[] args) {
Person p = new Student();
p.run();
}
}
abstract class Person {
public abstract void run();
}
class Student extends Person {
@Override
public void run() {
System.out.println("Student.run");
}
}
Static Field and Static Method
Static Field and Static Methods have their own space
Java Core Class
String and Coding
public class Main {
public static void main(String[] args){
String s = "Hello";
String s1 = "Hello";
a = s.toUpperCase(); // Change the String to Upper Case; s.toLowerCase(): Change the String to Lower Case;
b = s1.equals(s); // To see if s1 is equal to s
"Hello".contains("H"); // To see if "Hello" contains "H"
"Hello".substring(1,3); // The substring of "s[1],s[2]"
"\u3000Hello\u3000".strip(); // The thing printed out: "Hello"
}
}
String to Char:
public class Main {
public static void main(String[] args) {
char[] cs = "Hello".toCharArray();
String s = new String(cs);
System.out.println(s);
cs[0] = 'X';
System.out.println(s);
}
}
Java StringBuilder (The way to splice the string)
public class Main {
public static void main(String[] args) {
var s = new StringBuilder(1024);
s.append("Mr ")
.append("Bob")
.append("!")
.insert(0, "Hello, ");
System.out.println(s.toString());
}
}
The thing print out is: Hello, Mr Bob!
Java StringJoiner
public class Main {
public static void main(String[] args) {
String[] names = {"Bob", "Alice", "Grace"};
var sj = new StringJoiner(", ", "Hello ", "!");
for (String name : names) {
sj.add(name);
}
System.out.println(sj.toString());
}
}
Print out Hello Bob, Alice, Grace!
Java Exceptions and Errors
Use try and catch to get the Exceptions and Errors Use throw to make an error or Exception
try…catch example:
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
try{
return(x)
} catch(UnsupportedEncodingException e){
System.out.println(e);
}
Java Input and Output
List all the directory and files, then print them
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
File currentDir = new File("F:\\javaProject\\Documents");
String tree = "";
listDir(currentDir.getCanonicalFile(), tree);
}
static void listDir(File dir, String t) {
// TODO: 递归打印所有文件和子文件夹的内容
File[] fs = dir.listFiles();
if (fs != null) {
for (File f : fs) {
if(f.isDirectory()) {
System.out.println(t + f.getName() + "/");
listDir(f, t + " ");
} else {
System.out.println(t + f.getName());
listDir(f, t + " ");
}
}
}
}
}
}
