What I learned with Java Silver

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.

1. Java basics

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();
    }
}

2. Java data type manipulation

3. Use of operators and judgment structures

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
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/

4. Creating and using arrays

int[] array = new int[2]{ 2, 3}; //Compile error
int[][] array = new int[]{}; //Compile error
int[] d;
d = {2,3}; //Compile error

5. Use of loop structure

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");
    }
}

6. Method encapsulation operations

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]);
    }
}
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

7. Inheritance operation

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 {

}

8. Handling exceptions

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;
	}
    }

}

9. Manipulating major Java API classes

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

What I learned with Java Silver
What I learned with Java Gold
What i learned
What I learned from Java monetary calculation
What I learned ② ~ Mock ~
What I learned from doing Java work with Visual Studio Code
What I researched about Java 8
What I researched about Java 7
What I learned about Kotlin
What I researched about Java 5
What I learned in Java (Part 3) Instruction execution statement
What I learned when building a server in Java
Java lambda expressions learned with Comparator
What I learned from studying Rails
What I learned in Java (Part 4) Conditional branching and repetition
I fell into Java Silver (crying)
I tried to interact with Java
I tried UDP communication with Java
What is thread safe (with Java)
What you learned when you acquired Java SE 8 Silver and Gold
What I researched about Java learning
[Note] What I learned in half a year from inexperienced (Java)
[Note] What I learned in half a year from inexperienced (Java) (1)
[Note] What I learned in half a year from inexperienced (Java) (3)
Take what you've learned about Java reflection
What I learned through the swift teacher
Java SE 8 Silver (Java SE 8 Programmer I) Pass Note
I tried using OpenCV with Java + Tomcat
What is java
Java Silver memo
What is Java <>?
What is Java
Study Java Silver 1
I tried to make Basic authentication with Java
Summary of what I learned about Spring Boot
I want to use java8 forEach with index
What I did when I converted java to Kotlin
I tried to break a block with java (1)
Summary of what I learned in Spring Batch
Install java with Homebrew
I tried what I wanted to try with Stream softly.
Passed Java SE8 Silver
java bronze silver passed
I first touched Java ②
I tried to implement TCP / IP + BIO with JAVA
Change seats with java
Install Java with Ansible
[Java 11] I tried to execute Java without compiling with javac
Java Silver Study Day 1
I first touched Java ③
Java Silver passing experience
Summarize Java inheritance (Java Silver 8)
I first touched Java ④
What is Java Encapsulation?
[java] Java SE 8 Silver Note
I will write what I learned about docker anyway (second)
[Java Silver] About initialization
Why can I develop Java with Visual Studio Code?
Switch java with direnv
About inheritance (Java Silver)
I dealt with Azure Functions not working in Java