Hello world
Look at the code https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/lang/System.java to learn the coding style of java.
All primitive types, such as int
, char
, and double
begin with a lowercase.
All other types, such as String
and Math
, begin with an uppercase.
package java.lang;
// Class names looks like: MyClass
public final class System {}
// method name
private static native void registerNatives();
// constructor
private System() {}
// member variables
public static final InputStream in = null;
private static String notSupportedJnuEncoding;
// constants
private static final int NEVER = 1;
It using //
and /**/
as comments, same as C++.
Hello.java
// Usage 1:
// java Hello.java
// Usage 2:
// javac Hello.java
// java Hello
//
// Note:
// - "javac Hello.java" generates a file "Hello.class"
// - "java Hello" takes as input "Hello.class" and executes it
//
// The class name Hello must match the filename Hello.java
// By convention, the class name looks like `MyClass`.
class Hello {
// Note: How is array defined in java
public static void main(String[] args) {
System.out.println("hello world");
}
} // There is no ';' here
// Every line of runnable code must be in some `class`
EqualTest.java
class EqualTest {
public int i;
public EqualTest(int a) {
this.i = a;
}
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof EqualTest) {
return this.i == ((EqualTest) anObject).i;
}
return false;
}
public static void main(String[] args) {
EqualTest e1 = new EqualTest(10);
EqualTest e2 = new EqualTest(10);
System.out.println(e1 == e2); // false, compare the reference
System.out.println(e1 != e2); // true
System.out.println(e1.equals(e2)); // true, compare the contained value
}
}
Variables.java
class Variables {
public static void main(String[] args) {
String msg = "Hello world";
int i = 10;
i++; // ok
++i; // ok
// i-- // error, should be --i
--i;
int ii = i % 2;
i = 10;
ii = i / 3; // truncated
System.out.println(ii); // 3
ii += 2;
System.out.println(ii); // 5
// also has:
// -=, &=, |=, >>=, <<=, ^=, /=, *=
// Similar to C/C++
/* float f = 1.25; // error: possible lossy conversion from double to float */
// Caution: We have to use `1.25f` instead of `1.25` to assign a float variable
float f = 1.25f;
double d = 1.25;
d = 1.25d; // also ok
// f = d; // error: possible lossy conversion from double to float
f = (float) d; // ok, explicit cast
d = f; // ok
char c = 'h'; // a character
i = 10;
System.out.println(msg);
System.out.println(i); // 10
System.out.println(f); // 1.25
System.out.println(d); // 1.25
System.out.println(c); // h
// define multiple variable in the same line
int i1 = 1, i2;
// System.out.println(i2); // variable i2 might not have been initialized
i2 = 3;
System.out.println(i2); // 3
boolean b = false;
System.out.println(b); // false
b = true;
// b = 1; // error: int cannot be converted to boolean
System.out.println(b); // true
// byte b0 = 128; // error: lossy conversion from int to byte
byte b0 = 127; // ok
/* b0 = -129; // error: lossy conversion from int to type */
b0 = -128; // ok
// short s0 = 32768; // error: lssoy conversion from int to short
short s0 = 32767; // ok
// s0 = -32769; // error: lossy conversion fro int to short
s0 = -32768; // ok
// other types: int, long, float, double, boolean, char
// Note: char has 2-byte
// boolean has 1-bit
// There are no unsigned integers !!
// ternary operator :?
System.out.println(2 > 3 ? "yes" : "no"); // no
}
}
Strings.java
class Strings {
public static void main(String[] args) {
String s = "abc";
System.out.println(s.length()); // 3
System.out.println(s.toUpperCase()); // ABC
System.out.println(s.toUpperCase().toLowerCase()); // abc
System.out.println(s.indexOf("ab")); // 0
System.out.println(s.indexOf("a")); // 0
System.out.println(s.indexOf("bc")); // 1
System.out.println(s.indexOf("bca")); // when not found, it is -1
System.out.println(s.charAt(0) == 'a'); // true
System.out.println(s + "123" == "abc123"); // false, == compares the reference
System.out.println((s + "123").equals("abc123")); // true, compare the content
System.out.println(s.concat("123").equals("abc123")); // true, compare the content
System.out.println((s + 123).equals("abc123")); // true, compare the content
System.out.println((123 + s).equals("123abc")); // true, compare the content
s = "a\"b";
}
}
;
MathTest.java
class MathTest {
public static void main(String[] args) {
System.out.println(Math.max(2, 3)); // 3
System.out.println(Math.max(2, 3.5)); // 3.5
System.out.println(Math.min(2, 3.5)); // 2
System.out.println(Math.sqrt(6.25)); // 2.5
System.out.println(Math.sqrt(9)); // 3.0, note it is a double
System.out.println(Math.abs(-9)); // -9
System.out.println(Math.abs(-9.0)); // -9.0
System.out.println(Math.random()); // a number in the range [0, 1)
}
}
ArrayTest.java
class ArrayTest {
public static void main(String[] args) {
int[] a = {1, 2, 3};
for (int i : a) {
System.out.println(i);
}
/*
1
2
3
*/
System.out.println(a[0]); // 1
System.out.println(a.length); // 3
for (int i = 0; i != a.length; ++i) {
System.out.println(a[i]);
}
/*
1
2
3
*/
}
}
TODOs
How to define an array? How to iterate an array?
What methods does the
String
class haveWhat are final variables?
How to define a package?
What is static import?
What is a lambda expression?
How to write comments using javadoc?
How to generate documentation from javadoc?
How to use a for-each loop?
How to import a class from another file?