Java Silver Leave the knowledge gained in receiving Java SE 8 Programmer I. The book used for study is "Thorough capture Java SE 8 Silver problem collection [1Z0-808] compatible" https://www.amazon.co.jp/dp/B01ASGJYIE/ref=dp-kindle-redirect?_encoding=UTF8&btkr=1 Since it was, I will write it with the composition according to it.
class name.field
or class name.method
. If you perform static import, you can treat it as if it were a field or method defined in the class.
** Will this be used in actual development ?? **
As a note of writing, not static import
, but ʻimport static`.package tryAny;
public class staticImport {
public static int num = 0;
public static void print() {
System.out.println(num);
}
}
package tryAny.test;
import static tryAny.staticImport.*;
public class Main {
public static void main(String[] args) {
num = 13;
print();
}
}
_
are as follows.0
in octal is not a symbol. .. ** **Null
cannot be assigned to a variable of primitive type. Because null
is data that indicates that the variable does not refer to anything._
and currency symbol
. Also, the identifier must not start with a number.package tryAny;
import java.nio.ByteBuffer;
public class Cast {
public static void main(String[] args) {
//short digit overflow
long a = 32768;
int b = (int) a;
short c = (short) b;
byte[] byteShort = ByteBuffer.allocate(2).putShort(c).array();
System.out.println("Overflow of short digits");
for (byte b1 : byteShort) {
System.out.printf("%x ", b1);
}
System.out.println();
System.out.println("Int at 32768" + b);
System.out.println("Short at 32768" + c);
//int overflow
long aa = 2147483648L;
int bb = (int) aa;
short cc = (short) bb;
byte[] byteInt = ByteBuffer.allocate(4).putInt(bb).array();
System.out.println("int,Overflow of short digits");
for (byte b2 : byteInt) {
System.out.printf("%x ", b2);
}
System.out.println();
System.out.println("Int at the time of 2147483648" + bb);
System.out.println("Short at 2147483648" + cc);
}
}
Standard output
Overflow of short digits
80 0
Int at the time of 32768 32768
Short at 32768-32768
int,Overflow of short digits
80 0 0 0
Int at the time of 2147483648-2147483648
Short 0 at the time of 2147483648
and cast is required even if it does not exceed the range that can be expressed by int, such as ʻint a = 1 + 1L;
.<
<=
>
> =
can only be compared numerically. I think I can do something like true <false
, but I can't.String a =" const ";
, the string literal "const" is kept in the constant pool. Next, if there is a code like String b =" const ";
, it scans the constant pool and since "const" exists, b will hold the memory address. ..package tryAny;
public class ConstantPool {
public static void main(String[] args) {
String a1 = "constpool1";
String b1 = "constpool1";
System.out.println(a1 == b1);
String a2 = new String("constpool2");
String b2 = "constpool2";
System.out.println(a2 == b2);
}
}
https://qiita.com/liguofeng29/items/16d6dbec471bc5269f0e Was easy to understand.
Hey, I want to read this area too (but it seems difficult). https://dev.classmethod.jp/server-side/java/classfile-reading/
int[] array = new int[2]{ 2, 3}; //Compile error
int[][] array = new int[]{}; //Compile error
int[] d;
d = {2,3}; //Compile error
package tryAny;
public class For {
public static void main(String[] args) {
//Multiple initialization statements
for (int i = 0, j = 1; i < j; i++) {
System.out.println(i);
}
//Multiple update statements
for (int i = 0; i < 4; i++, tmp(), tmp2()) {
System.out.println(i);
}
}
private static void tmp() {
System.out.println("tmp");
}
private static void tmp2() {
System.out.println("tmp2");
}
}
package tryAny;
public class Overload {
public static void main(String[] args) {
System.out.println(ret(1, 2)); // 3
System.out.println(ret(1.0, 2));// 0
System.out.println(ret(1, 2.0));// 2
}
private static String ret(int a, int b) {
return Integer.toString(a + b);
}
public static int ret(int a, double b) {
return a * (int) b;
}
protected static int ret(double a, int b) {
return (int) a / b;
}
}
package tryAny;
public class InitializeBlock {
public static void main(String[] args) {
InitTest i1 = new InitTest(); // JavaSilver
InitTest i2 = new InitTest(" is dead");// JavaSilver is dead
InitTest i3 = new InitTest(100, new String[] { "dummy", "point" });// JavaSilver100point
}
}
class InitTest {
{
System.out.print("JavaSilver");
}
InitTest() {
System.out.println();
}
InitTest(String str) {
System.out.println(str);
}
InitTest(int a, String... str) {
System.out.println(a + str[1]);
}
}
If you want to call another overloaded constructor using this in a constructor, ** must be written first **.
A description of access modifiers is below. Protected, none, it's easy to forget about it.
Modifier | Description |
---|---|
public | Accessable from all classes |
protected | Only accessible from subclasses that belong to or inherit from the same package |
None | Only accessible from classes belonging to the same package |
private | Only accessible from within the class |
package tryAny;
public class Extends {
public static void main(String[] args) {
Tmp tmp = new Tmp2();
System.out.println(tmp.ret(1));
}
}
abstract class Tmp {
abstract Object ret(int i);
}
class Tmp2 extends Tmp {
@Override
Integer ret(int i) {
return i;
}
}
package tryAny;
public class Cast2 {
public static void main(String[] args) {
A a = new A();
B b = (B) a; //Compiles, but Run-time ClassCastException occurs
}
}
class A {
}
class B extends A {
}
package tryAny;
public class Exception {
public static void main(String[] args) {
int ret = test();
System.out.println(ret); // 20
}
private static int test() {
try {
int[] tmp = {};
int a = tmp[0];
} catch (RuntimeException e) {
return 10;
} finally {
return 20;
}
}
}
How to define an immutable object
Qualify all fields with private
Do not provide a method that can change the internal state of an object
Declare the class as final to prevent the method from being overridden (prevent changes from subclasses)
If you have a variable object inside, don't provide that object to the outside
The space removed by the string trim method is "\ u0000 ~ \ u0020" in the character code.
In the append method of StringBuilder, you can also take a char array as an argument.
package tryAny;
public class StringTest {
public static void main(String[] args) {
String tmp = null;
System.out.println(tmp + "a"); // nulla
try {
System.out.println(tmp.concat("a"));//Nullpo comes out
} catch (NullPointerException e) {
System.out.println("Nullpo outbreak");
}
StringBuilder sb = new StringBuilder();
System.out.println(sb.capacity());// 16
char[] char1 = { 'a', 'b', 'c', 'd' };
System.out.println(sb.append(char1));// abcd
}
}
package tryAny;
public class Lambda2 {
public static void main(String[] args) {
Algo plus = (int x, int y) -> {
return x + y;
};
//If there is only one lambda expression process and you omit the curly braces, you must omit return.
Algo minus = (int x, int y) -> x - y;
System.out.println(execCalc(plus, 2, 1));
System.out.println(execCalc(minus, 2, 1));
}
static int execCalc(Algo algo, int x, int y) {
return algo.calc(x, y);
}
@FunctionalInterface
interface Algo {
int calc(int x, int y);
}
}
All you have to do is take the exam. .. ..
Recommended Posts