java learning
Java Basic
learning web: https://ocw.mit.edu/ans7870/6/6.005/s16/index.html
Introduction to Java
While there are many IDEs with built-in execution capabilities (Eclipse and IntelliJ for example), Java can also be run directly from the command line.
Before you can run the program from the command line, you must compile it. Open your terminal or command prompt (depending on OS), and navigate to the directory where the file you want to run is located. Once there, use javac
and the filename to compile:
1 | javac MyClass.java |
This creates the .class
file that can be executed. However, if there are any bugs found in your program, they will be flagged at this point, and the executable .class
file will not be created. You won’t be able to run the file until it compiles with no issues.
Once you have your executable file, use java
and the name of the class to run it:
1 | java MyClass |
Note: Do not include the .java
or .class
suffixes, only use the name of the class.
Let’s say we had the following Java class:
1 | public class HelloWorld { |
We would compile this using:
1 | javac HelloWorld.java |
Then run it using:
1 | java HelloWorld |
It would output the following:
1 | Hello world! |
Details
Language type
Statically typed languages
A language is statically typed if the type of a variable is known at compile time. For some languages this means that you as the programmer must specify what type each variable is; other languages (e.g.: Java, C, C++) offer some form of type inference, the capability of the type system to deduce the type of a variable (e.g.: OCaml, Haskell, Scala, Kotlin).
The main advantage here is that all kinds of checking can be done by the compiler, and therefore a lot of trivial bugs are caught at a very early stage.
Examples: C, C++, Java, Rust, Go, Scala
Dynamically typed languages
A language is dynamically typed if the type is associated with run-time values, and not named variables/fields/etc. This means that you as a programmer can write a little quicker because you do not have to specify types every time (unless using a statically-typed language with type inference).
Examples: Perl, Ruby, Python, PHP, JavaScript, Erlang
Most scripting languages have this feature as there is no compiler to do static type-checking anyway, but you may find yourself searching for a bug that is due to the interpreter misinterpreting the type of a variable. Luckily, scripts tend to be small so bugs have not so many places to hide.
Both Interpreter and compiler
java has both interpreter and compiler.
If you have no idea what the differences are, click the link below:
JVM structure
https://dzone.com/articles/jvm-architecture-explained
2021年 java虚拟机jvm性能调优实战+面试_哔哩哔哩_bilibili
Details supplement
: Why Static methods can’t call the non-static methods? (Also answered the question “What do we actually mean when we say that we load class, store the methods”)
【新面试题】Java中的静态方法为什么不能调用非静态方法 - 知乎 (zhihu.com)
Java Syntax
Variable
1 | int x; // declaration |
String
difference between String.equals
and ==
1 | String s1 = new String("java"); |
String methods
1 | String.length() |
Loop
Java is an object-oriented programming language, which means it consists of classes and their methods. Unlike some other programming languages, Java only has methods (which are associated with classes), and no functions.
but a for-each
loop is simpler and more straightforward:
1 | for (String s : myArray) { |
Scanner
difference between scanner.next
and scanner.nextLine
When encountering the space、tab or \n
, scanner.next
just stop reading in, and leave the blank character in the input buffer.
The scanner.nextLine
end reading until \n
comes up, and it just throw away that \n
.
scanner.next
will first skip the blank character and read in the normal character until encounter the blank again.
Whereas scanner.nextLine
may get stuck in the beginning of reading.
1 | input:ab cd |
Wrapper class
wrapper class
= provides a way to use primitive data types as reference date types
reference data types contain useful methods can be used with collections (ex. ArrayList)
1 | // primitive //wrapper |
autoboxing
= the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes.
unboxing
= the reverse of autoboxing. Automatic conversion of wrapper class to primitive classes.
Array
An array is a fixed collection of values with the same data type.
Only after initialization, can we use the array with the index.
1 | int[] lottoNumbers; // declaration |
ArrayList
ArrayList
= a resizable array that only store reference data types.
Elements can be added and removed after compilation phase.
2D ArrayList
= a dynamic list of lists.
You can change the size of thses lists during runtime.
1 | import java.util.*; |
Overloaded methods
overloaded methods
= methods that share the same name but have different parameters
method name + parameters = method signature.
1 | static int add(int a, int b){ |
Object Oriented Programming(OOP)
object
= an instance of a class that may contain attributes and methods.
Constructor
constructor
= special method that is called when an object is instantiated(created).
1 | public class Pizza{ |
ToString
1 | public String toString() { |
So, when we call the method or just print the object like this:
1 | System.out.println(objectname) |
And we can rewrite it using overloaded method:
1 | public String toString(){ |
Static
static
= modifier. A single copy of a variable/method is created and shared.
The class “owns” the static member.
Static variable
Static method
继承 + 文件、流 + 线程 + jvm详细