3-Third commit for articles:
* to core-java-lang: * https://www.baeldung.com/java-lang-system * https://www.baeldung.com/java-type-erasure * https://www.baeldung.com/java-assert * https://www.baeldung.com/java-pass-by-value-or-pass-by-reference * https://www.baeldung.com/java-variable-method-hiding * https://www.baeldung.com/java-access-modifiers * https://www.baeldung.com/java-super * https://www.baeldung.com/java-this * https://www.baeldung.com/java-immutable-object * https://www.baeldung.com/java-classnotfoundexception-and-noclassdeffounderror * https://www.baeldung.com/infinite-loops-java (changes for this were included in commit 1, with other loop-related examples)
This commit is contained in:
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.accessmodifiers;
|
||||
|
||||
public class Public {
|
||||
public Public() {
|
||||
SuperPublic.publicMethod(); // Available everywhere.
|
||||
SuperPublic.protectedMethod(); // Available in the same package or subclass.
|
||||
SuperPublic.defaultMethod(); // Available in the same package.
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.accessmodifiers;
|
||||
|
||||
public class SubClass extends SuperPublic {
|
||||
public SubClass() {
|
||||
SuperPublic.publicMethod(); // Available everywhere.
|
||||
SuperPublic.protectedMethod(); // Available in the same package or subclass.
|
||||
SuperPublic.defaultMethod(); // Available in the same package.
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package com.baeldung.accessmodifiers;
|
||||
|
||||
//Only public or default access modifiers are permitted
|
||||
public class SuperPublic {
|
||||
// Always available from anywhere
|
||||
static public void publicMethod() {
|
||||
System.out.println(SuperPublic.class.getName() + " publicMethod()");
|
||||
}
|
||||
|
||||
// Available within the same package
|
||||
static void defaultMethod() {
|
||||
System.out.println(SuperPublic.class.getName() + " defaultMethod()");
|
||||
}
|
||||
|
||||
// Available within the same package and subclasses
|
||||
static protected void protectedMethod() {
|
||||
System.out.println(SuperPublic.class.getName() + " protectedMethod()");
|
||||
}
|
||||
|
||||
// Available within the same class only
|
||||
static private void privateMethod() {
|
||||
System.out.println(SuperPublic.class.getName() + " privateMethod()");
|
||||
}
|
||||
|
||||
// Method in the same class = has access to all members within the same class
|
||||
private void anotherPrivateMethod() {
|
||||
privateMethod();
|
||||
defaultMethod();
|
||||
protectedMethod();
|
||||
publicMethod(); // Available in the same class only.
|
||||
}
|
||||
}
|
||||
|
||||
// Only public or default access modifiers are permitted
|
||||
class SuperDefault {
|
||||
public void publicMethod() {
|
||||
System.out.println(this.getClass().getName() + " publicMethod()");
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.accessmodifiers.another;
|
||||
|
||||
import com.baeldung.accessmodifiers.SuperPublic;
|
||||
|
||||
public class AnotherPublic {
|
||||
public AnotherPublic() {
|
||||
SuperPublic.publicMethod(); // Available everywhere.
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package com.baeldung.accessmodifiers.another;
|
||||
|
||||
import com.baeldung.accessmodifiers.SuperPublic;
|
||||
|
||||
public class AnotherSubClass extends SuperPublic {
|
||||
public AnotherSubClass() {
|
||||
SuperPublic.publicMethod(); // Available everywhere.
|
||||
SuperPublic.protectedMethod(); // Available in subclass. Let's note different package.
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.accessmodifiers.another;
|
||||
|
||||
import com.baeldung.accessmodifiers.SuperPublic;
|
||||
|
||||
public class AnotherSuperPublic {
|
||||
public AnotherSuperPublic() {
|
||||
SuperPublic.publicMethod(); // Available everywhere. Let's note different package.
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package com.baeldung.assertion;
|
||||
|
||||
/**
|
||||
* Simple demonstration of using Java assert keyword.
|
||||
*/
|
||||
public class Assertion {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Assertion assertion = new Assertion();
|
||||
assertion.setup();
|
||||
}
|
||||
|
||||
public void setup() {
|
||||
Object conn = getConnection();
|
||||
assert conn != null : "Connection is null";
|
||||
|
||||
// continue with other setup ...
|
||||
}
|
||||
|
||||
// Simulate failure to get a connection; using Object
|
||||
// to avoid dependencies on JDBC or some other heavy
|
||||
// 3rd party library
|
||||
public Object getConnection() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.baeldung.immutableobjects;
|
||||
|
||||
public final class Currency {
|
||||
|
||||
private final String value;
|
||||
|
||||
private Currency(String currencyValue) {
|
||||
value = currencyValue;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public static Currency of(String value) {
|
||||
return new Currency(value);
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package com.baeldung.immutableobjects;
|
||||
|
||||
// 4. Immutability in Java
|
||||
public final class Money {
|
||||
private final double amount;
|
||||
private final Currency currency;
|
||||
|
||||
public Money(double amount, Currency currency) {
|
||||
this.amount = amount;
|
||||
this.currency = currency;
|
||||
}
|
||||
|
||||
public Currency getCurrency() {
|
||||
return currency;
|
||||
}
|
||||
|
||||
public double getAmount() {
|
||||
return amount;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package com.baeldung.keyword;
|
||||
|
||||
import com.baeldung.keyword.superkeyword.SuperSub;
|
||||
import com.baeldung.keyword.thiskeyword.KeywordUnitTest;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/14/2018.
|
||||
*/
|
||||
public class KeywordDemo {
|
||||
|
||||
public static void main(String[] args) {
|
||||
KeywordUnitTest keyword = new KeywordUnitTest();
|
||||
|
||||
SuperSub child = new SuperSub("message from the child class");
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package com.baeldung.keyword.superkeyword;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/14/2018.
|
||||
*/
|
||||
public class SuperBase {
|
||||
|
||||
String message = "super class";
|
||||
|
||||
public SuperBase() {
|
||||
}
|
||||
|
||||
public SuperBase(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public void printMessage() {
|
||||
System.out.println(message);
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package com.baeldung.keyword.superkeyword;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/15/2018.
|
||||
*/
|
||||
public class SuperSub extends SuperBase {
|
||||
|
||||
String message = "child class";
|
||||
|
||||
public SuperSub(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public SuperSub() {
|
||||
super.printMessage();
|
||||
printMessage();
|
||||
}
|
||||
|
||||
public void getParentMessage() {
|
||||
System.out.println(super.message);
|
||||
}
|
||||
|
||||
public void printMessage() {
|
||||
System.out.println(message);
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package com.baeldung.keyword.thiskeyword;
|
||||
|
||||
public class KeywordUnitTest {
|
||||
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
public KeywordUnitTest() {
|
||||
this("John", 27);
|
||||
this.printMessage();
|
||||
printInstance(this);
|
||||
}
|
||||
|
||||
public KeywordUnitTest(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public void printMessage() {
|
||||
System.out.println("invoked by this");
|
||||
}
|
||||
|
||||
public void printInstance(KeywordUnitTest thisKeyword) {
|
||||
System.out.println(thisKeyword);
|
||||
}
|
||||
|
||||
public KeywordUnitTest getCurrentInstance() {
|
||||
return this;
|
||||
}
|
||||
|
||||
class ThisInnerClass {
|
||||
|
||||
boolean isInnerClass = true;
|
||||
|
||||
public ThisInnerClass() {
|
||||
KeywordUnitTest thisKeyword = KeywordUnitTest.this;
|
||||
String outerString = KeywordUnitTest.this.name;
|
||||
System.out.println(this.isInnerClass);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "KeywordTest{" +
|
||||
"name='" + name + '\'' +
|
||||
", age=" + age +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.baeldung.noclassdeffounderror;
|
||||
|
||||
public class ClassWithInitErrors {
|
||||
static int data = 1 / 0;
|
||||
}
|
||||
-14
@@ -1,14 +0,0 @@
|
||||
package com.baeldung.noclassdeffounderror;
|
||||
|
||||
public class NoClassDefFoundErrorExample {
|
||||
public ClassWithInitErrors getClassWithInitErrors() {
|
||||
ClassWithInitErrors test;
|
||||
try {
|
||||
test = new ClassWithInitErrors();
|
||||
} catch (Throwable t) {
|
||||
System.out.println(t);
|
||||
}
|
||||
test = new ClassWithInitErrors();
|
||||
return test;
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package com.baeldung.parameterpassing;
|
||||
|
||||
public class NonPrimitives {
|
||||
public static void main(String[] args) {
|
||||
FooClass a = new FooClass(1);
|
||||
FooClass b = new FooClass(1);
|
||||
|
||||
System.out.printf("Before Modification: a = %d and b = %d ", a.num, b.num);
|
||||
modify(a, b);
|
||||
System.out.printf("\nAfter Modification: a = %d and b = %d ", a.num, b.num);
|
||||
}
|
||||
|
||||
public static void modify(FooClass a1, FooClass b1) {
|
||||
a1.num++;
|
||||
|
||||
b1 = new FooClass(1);
|
||||
b1.num++;
|
||||
}
|
||||
}
|
||||
|
||||
class FooClass {
|
||||
public int num;
|
||||
|
||||
public FooClass(int num) {
|
||||
this.num = num;
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.parameterpassing;
|
||||
|
||||
public class Primitives {
|
||||
public static void main(String[] args) {
|
||||
int x = 1;
|
||||
int y = 2;
|
||||
|
||||
System.out.printf("Before Modification: x = %d and y = %d ", x, y);
|
||||
modify(x, y);
|
||||
System.out.printf("\nAfter Modification: x = %d and y = %d ", x, y);
|
||||
}
|
||||
|
||||
public static void modify(int x1, int y1) {
|
||||
x1 = 5;
|
||||
y1 = 10;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.scope.method;
|
||||
|
||||
|
||||
public class BaseMethodClass {
|
||||
|
||||
public static void printMessage() {
|
||||
System.out.println("base static method");
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.scope.method;
|
||||
|
||||
|
||||
public class ChildMethodClass extends BaseMethodClass {
|
||||
|
||||
public static void printMessage() {
|
||||
System.out.println("child static method");
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.baeldung.scope.method;
|
||||
|
||||
public class MethodHidingDemo {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ChildMethodClass.printMessage();
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.scope.variable;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/7/2018.
|
||||
*/
|
||||
public class ChildVariable extends ParentVariable {
|
||||
|
||||
String instanceVariable = "child variable";
|
||||
|
||||
public void printInstanceVariable() {
|
||||
System.out.println(instanceVariable);
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.baeldung.scope.variable;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/6/2018.
|
||||
*/
|
||||
public class HideVariable {
|
||||
|
||||
private String message = "this is instance variable";
|
||||
|
||||
HideVariable() {
|
||||
String message = "constructor local variable";
|
||||
System.out.println(message);
|
||||
}
|
||||
|
||||
public void printLocalVariable() {
|
||||
String message = "method local variable";
|
||||
System.out.println(message);
|
||||
}
|
||||
|
||||
public void printInstanceVariable() {
|
||||
System.out.println(this.message);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.scope.variable;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/7/2018.
|
||||
*/
|
||||
public class ParentVariable {
|
||||
|
||||
String instanceVariable = "parent variable";
|
||||
|
||||
public void printInstanceVariable() {
|
||||
System.out.println(instanceVariable);
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.baeldung.scope.variable;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/6/2018.
|
||||
*/
|
||||
public class VariableHidingDemo {
|
||||
public static void main(String[] args) {
|
||||
HideVariable variable = new HideVariable();
|
||||
variable.printLocalVariable();
|
||||
variable.printInstanceVariable();
|
||||
|
||||
ParentVariable parentVariable = new ParentVariable();
|
||||
ParentVariable childVariable = new ChildVariable();
|
||||
|
||||
parentVariable.printInstanceVariable();
|
||||
childVariable.printInstanceVariable();
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.system;
|
||||
|
||||
import java.awt.event.WindowEvent;
|
||||
|
||||
/**
|
||||
* Note: This class is not meant for unit-testing since it uses system
|
||||
* features at low level and that it uses 'System' gc() which suggests
|
||||
* JVM for garbage collection. But the usage below demonstrates how the
|
||||
* method can be used.
|
||||
*/
|
||||
public class ChatWindow {
|
||||
public void windowStateChanged(WindowEvent event) {
|
||||
if (event.getNewState() == WindowEvent.WINDOW_DEACTIVATED ) {
|
||||
System.gc(); // if it ends up running, great!
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package com.baeldung.system;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class DateTimeService {
|
||||
|
||||
// One hour from now
|
||||
public long nowPlusOneHour() {
|
||||
return System.currentTimeMillis() + 3600 * 1000L;
|
||||
}
|
||||
|
||||
// Human-readable format
|
||||
public String nowPrettyPrinted() {
|
||||
return new Date(System.currentTimeMillis()).toString();
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package com.baeldung.system;
|
||||
|
||||
public class EnvironmentVariables {
|
||||
public String getPath() {
|
||||
return System.getenv("PATH");
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.baeldung.system;
|
||||
|
||||
/**
|
||||
* Note: This class is not meant for unit-testing since it uses system
|
||||
* features at low level and that it uses 'System' standard error stream
|
||||
* methods to show output on screen. Also unit-tests in CI environments
|
||||
* don't have console output for user to see messages. But the usage below
|
||||
* demonstrates how the methods can be used.
|
||||
*/
|
||||
public class SystemErrDemo {
|
||||
public static void main(String[] args) {
|
||||
// Print without 'hitting' return
|
||||
System.err.print("some inline error message");
|
||||
|
||||
// Print and then 'hit' return
|
||||
System.err.println("an error message having new line at the end");
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.baeldung.system;
|
||||
|
||||
/**
|
||||
* Note: This class is not meant for unit-testing since it uses system
|
||||
* features at low level and that it uses 'System' exit() which will
|
||||
* exit the JVM. Also unit-tests in CI environments are not meant to
|
||||
* exit unit tests like that. But the usage below demonstrates how the
|
||||
* method can be used.
|
||||
*/
|
||||
public class SystemExitDemo {
|
||||
public static void main(String[] args) {
|
||||
boolean error = false;
|
||||
|
||||
// do something and set error value
|
||||
|
||||
if (error) {
|
||||
System.exit(1); // error case exit
|
||||
} else {
|
||||
System.exit(0); // normal case exit
|
||||
}
|
||||
|
||||
// Will not do anything after exit()
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package com.baeldung.system;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.PrintStream;
|
||||
|
||||
/**
|
||||
* Note: This class is not meant for unit-testing since it uses system
|
||||
* features at low level and that it uses 'System' standard output stream
|
||||
* methods to show output on screen. Also unit-tests in CI environments
|
||||
* don't have console output for user to see messages. But the usage below
|
||||
* demonstrates how the methods can be used.
|
||||
*/
|
||||
public class SystemOutDemo {
|
||||
|
||||
public static void main(String[] args) throws FileNotFoundException {
|
||||
// Print without 'hitting' return
|
||||
System.out.print("some inline message");
|
||||
|
||||
// Print and then 'hit' return
|
||||
System.out.println("a message having new line at the end");
|
||||
|
||||
// Changes output stream to send messages to file.
|
||||
System.setOut(new PrintStream("file.txt"));
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package com.baeldung.system;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.Console;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
/**
|
||||
* Note: This class is not meant for unit-testing since it uses system
|
||||
* features at low level and that it uses 'System' standard input stream
|
||||
* methods to read text from user. Also unit-tests in CI environments
|
||||
* don't have console input for user to type in text. But the usage below
|
||||
* demonstrates how the methods can be used.
|
||||
*/
|
||||
public class UserCredentials {
|
||||
|
||||
public String readUsername(int length) throws IOException {
|
||||
byte[] name = new byte[length];
|
||||
System.in.read(name, 0, length); // by default, from the console
|
||||
return new String(name);
|
||||
}
|
||||
|
||||
public String readUsername() throws IOException {
|
||||
BufferedReader reader =
|
||||
new BufferedReader(new InputStreamReader(System.in));
|
||||
return reader.readLine();
|
||||
}
|
||||
|
||||
public String readUsernameWithPrompt() {
|
||||
Console console = System.console();
|
||||
|
||||
return console == null ? null : // Console not available
|
||||
console.readLine("%s", "Enter your name: ");
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package com.baeldung.typeerasure;
|
||||
|
||||
public class ArrayContentPrintUtil {
|
||||
|
||||
public static <E> void printArray(E[] array) {
|
||||
for (E element : array) {
|
||||
System.out.printf("%s ", element);
|
||||
}
|
||||
}
|
||||
|
||||
public static <E extends Comparable<E>> void printArray(E[] array) {
|
||||
for (E element : array) {
|
||||
System.out.printf("%s ", element);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package com.baeldung.typeerasure;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class BoundStack<E extends Comparable<E>> {
|
||||
|
||||
private E[] stackContent;
|
||||
private int total;
|
||||
|
||||
public BoundStack(int capacity) {
|
||||
this.stackContent = (E[]) new Object[capacity];
|
||||
}
|
||||
|
||||
public void push(E data) {
|
||||
if (total == stackContent.length) {
|
||||
resize(2 * stackContent.length);
|
||||
}
|
||||
stackContent[total++] = data;
|
||||
}
|
||||
|
||||
public E pop() {
|
||||
if (!isEmpty()) {
|
||||
E datum = stackContent[total];
|
||||
stackContent[total--] = null;
|
||||
return datum;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void resize(int capacity) {
|
||||
Arrays.copyOf(stackContent, capacity);
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return total == 0;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.typeerasure;
|
||||
|
||||
public class IntegerStack extends Stack<Integer> {
|
||||
|
||||
public IntegerStack(int capacity) {
|
||||
super(capacity);
|
||||
}
|
||||
|
||||
public void push(Integer value) {
|
||||
System.out.println("Pushing into my integerStack");
|
||||
super.push(value);
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package com.baeldung.typeerasure;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Stack<E> {
|
||||
|
||||
private E[] stackContent;
|
||||
private int total;
|
||||
|
||||
public Stack(int capacity) {
|
||||
this.stackContent = (E[]) new Object[capacity];
|
||||
}
|
||||
|
||||
public void push(E data) {
|
||||
System.out.println("In base stack push#");
|
||||
if (total == stackContent.length) {
|
||||
resize(2 * stackContent.length);
|
||||
}
|
||||
stackContent[total++] = data;
|
||||
}
|
||||
|
||||
public E pop() {
|
||||
if (!isEmpty()) {
|
||||
E datum = stackContent[total];
|
||||
stackContent[total--] = null;
|
||||
return datum;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void resize(int capacity) {
|
||||
Arrays.copyOf(stackContent, capacity);
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return total == 0;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user