Archives: Basics

Java – Convert String to Long

Output:

String to long conversion using valueOf :2323232
String to long conversion example using Long object: 4444444444444444444
String to long conversion using parseLong() method: 999999999999999999
String to long conversion using decode() method: -1023454
String to long example in hex format decode() method: 65535
String to long example in octal format decode() method: 511

Java – Create Immutable Class Example

[code lang=”java”]// declare class as final
final class Immutable {

// declare all variables as final
private final int val1;
private final String val2;

// provide constructor to set the values
public Immutable(int a, String s) {
val1 = a;
val2 = s;
}

// provide getters only
public int getVal1() {
return val1;
}

public String getVal2() {
return val2;
}
}[/code]
[code lang=”java”]public class ImmutableClassDemo {
public static void main(String[] args) {
Immutable obj = new Immutable(10, "Immutable Class Example");
System.out.println(obj.getVal1());
System.out.println(obj.getVal2());
}
}[/code]


Java – Run Garbage Collection Process