Java Methods (1)
Java Methods (1)
package StringAllMethods;
// 1. length()
System.out.println("Length: " + str.length());
// 2. charAt()
System.out.println("Character at index 4: " + str.charAt(4));
// 3. concat()
String concatStr = str.concat(" - Java");
System.out.println("Concatenated String: " + concatStr);
// 4. equals()
System.out.println("Equals: " + str.equals("Hello World"));
// 5. equalsIgnoreCase()
System.out.println("Equals Ignore Case: " + str.equalsIgnoreCase(str2));
// 6. compareTo()
System.out.println("Compare To: " + str.compareTo("Hello Java"));
/*
*/
// 7. compareToIgnoreCase()
System.out.println("Compare To Ignore Case: " + str.compareToIgnoreCase(str2));
// 8. substring()
System.out.println("Substring from index 6: " + str.substring(6));
System.out.println("Substring from 0 to 5: " + str.substring(0, 5));
// 9. indexOf()
System.out.println("Index of 'o': " + str.indexOf('o'));
// 10. lastIndexOf()
System.out.println("Last Index of 'o': " + str.lastIndexOf('o'));
// 11. contains()
System.out.println("Contains 'World': " + str.contains("World"));
// 12. replace()
System.out.println("Replace 'World' with 'Java': " + str.replace("World", "Java"));
// 13. replaceAll()
System.out.println("Replace all 'o' with 'x': " + str.replaceAll("o", "x"));
// 14. replaceFirst()
System.out.println("Replace first 'o' with 'x': " + str.replaceFirst("o", "x"));
// 15. toLowerCase()
System.out.println("Lower Case: " + str.toLowerCase());
// 16. toUpperCase()
System.out.println("Upper Case: " + str.toUpperCase());
// 17. trim()
String strWithSpaces = " Hello World ";
System.out.println("Trimmed: '" + strWithSpaces.trim() + "'");
// 18. split()
String[] fruits = str3.split(",");
System.out.println("Split (fruits): ");
for (String fruit : fruits) {
System.out.println(fruit);
}
// 19. isEmpty()
System.out.println("Is Empty: " + str.isEmpty());
// 20. startsWith()
System.out.println("Starts with 'Hello': " + str.startsWith("Hello"));
// 21. endsWith()
System.out.println("Ends with 'World': " + str.endsWith("World"));
// 22. valueOf()
int num = 100;
String numStr = String.valueOf(num);
System.out.println("Value of int: " + numStr);
// 23. hashCode()
System.out.println("Hash Code: " + str.hashCode());
// 24. matches()
System.out.println("Matches regex 'Hello.*': " + str.matches("Hello.*"));
// 25. intern()
String internedStr = str.intern();
System.out.println("Interned String: " + internedStr);
// 26. getBytes()
byte[] byteArray = str.getBytes();
System.out.println("Bytes of String:");
for (byte b : byteArray) {
System.out.print(b + " ");
}
System.out.println();
// 27. toCharArray()
char[] charArray = str.toCharArray();
System.out.println("To Char Array:");
for (char c : charArray) {
System.out.print(c + " ");
}
System.out.println();
// 28. format()
String formattedStr = String.format("Welcome %s to the %s!", "John", "Java World");
System.out.println("Formatted String: " + formattedStr);
// 29. codePointAt()
System.out.println("Code point at index 0: " + str.codePointAt(0));
// 30. codePointBefore()
System.out.println("Code point before index 1: " + str.codePointBefore(1));
// 31. codePointCount()
System.out.println("Code point count from 0 to 5: " + str.codePointCount(0, 5));
// 32. regionMatches()
String anotherStr = "World of Java";
boolean regionMatches = str.regionMatches(6, anotherStr, 0, 5);
System.out.println("Region matches: " + regionMatches);
}
}
/*
1) Length: 11
2) Character at index 4: o
3) Concatenated String: Hello World - Java
4) Equals: true
5) Equals Ignore Case: true
6) Compare To: 1
7) Compare To Ignore Case: 0
8) Substring from index 6: World
Substring from 0 to 5: Hello
9) Index of 'o': 4
10) Last Index of 'o': 7
11) Contains 'World': true
12) Replace 'World' with 'Java': Hello Java
13) Replace all 'o' with 'x': Hellx Wxrld
14) Replace first 'o' with 'x': Hellx World
15) Lower Case: hello world
16) Upper Case: HELLO WORLD
17) Trimmed: 'Hello World'
18) Split (fruits):
apple
banana
orange
19) Is Empty: false
20) Starts with 'Hello': true
21) Ends with 'World': true
22) Value of int: 100
23) Hash Code: -862545276
24) Matches regex 'Hello.*': true
25) Interned String: Hello World
26) Bytes of String:
72 101 108 108 111 32 87 111 114 108 100
27) To Char Array:
Hello World
28) Formatted String: Welcome John to the Java World!
29) Code point at index 0: 72
30) Code point before index 1: 72
31) Code point count from 0 to 5: 5
32) Region matches: true
*/
package StringAllMethods;
// append()
sb.append(" World");
System.out.println("After append: " + sb);
// insert()
sb.insert(6, "Java ");
System.out.println("After insert: " + sb);
// delete()
sb.delete(6, 11);
System.out.println("After delete: " + sb);
// deleteCharAt()
sb.deleteCharAt(5);
System.out.println("After deleteCharAt: " + sb);
// replace()
sb.replace(6, 11, "Java");
System.out.println("After replace: " + sb);
// reverse()
sb.reverse();
System.out.println("After reverse: " + sb);
// substring()
String substring = sb.substring(6);
System.out.println("Substring from index 6: " + substring);
// charAt()
char charAtIndex = sb.charAt(6);
System.out.println("Character at index 6: " + charAtIndex);
// indexOf()
int indexOfJava = sb.indexOf("Java");
System.out.println("Index of 'Java': " + indexOfJava);
// lastIndexOf()
int lastIndexOfJava = sb.lastIndexOf("Java");
System.out.println("Last index of 'Java': " + lastIndexOfJava);
// length()
int length = sb.length();
System.out.println("Length of StringBuilder: " + length);
// capacity()
int capacity = sb.capacity();
System.out.println("Capacity of StringBuilder: " + capacity);
// ensureCapacity()
sb.ensureCapacity(50);
System.out.println("Capacity after ensureCapacity: " + sb.capacity());
// trimToSize()
sb.trimToSize();
System.out.println("Capacity after trimToSize: " + sb.capacity());
// toString()
String sbToString = sb.toString();
System.out.println("StringBuilder toString: " + sbToString);
// StringBuffer Example
StringBuffer sbf = new StringBuffer("Hello");
System.out.println("\nInitial StringBuffer: " + sbf);
// append()
sbf.append(" World");
System.out.println("After append: " + sbf);
// insert()
sbf.insert(6, "Java ");
System.out.println("After insert: " + sbf);
// delete()
sbf.delete(6, 11);
System.out.println("After delete: " + sbf);
// deleteCharAt()
sbf.deleteCharAt(5);
System.out.println("After deleteCharAt: " + sbf);
// replace()
sbf.replace(6, 11, "Java");
System.out.println("After replace: " + sbf);
// reverse()
sbf.reverse();
System.out.println("After reverse: " + sbf);
// substring()
String substringSbf = sbf.substring(6);
System.out.println("Substring from index 6: " + substringSbf);
// charAt()
char charAtIndexSbf = sbf.charAt(6);
System.out.println("Character at index 6: " + charAtIndexSbf);
// indexOf()
int indexOfJavaSbf = sbf.indexOf("Java");
System.out.println("Index of 'Java': " + indexOfJavaSbf);
// lastIndexOf()
int lastIndexOfJavaSbf = sbf.lastIndexOf("Java");
System.out.println("Last index of 'Java': " + lastIndexOfJavaSbf);
// length()
int lengthSbf = sbf.length();
System.out.println("Length of StringBuffer: " + lengthSbf);
// capacity()
int capacitySbf = sbf.capacity();
System.out.println("Capacity of StringBuffer: " + capacitySbf);
// ensureCapacity()
sbf.ensureCapacity(50);
System.out.println("Capacity after ensureCapacity: " + sbf.capacity());
// trimToSize()
sbf.trimToSize();
System.out.println("Capacity after trimToSize: " + sbf.capacity());
// toString()
String sbfToString = sbf.toString();
System.out.println("StringBuffer toString: " + sbfToString);
}
}
/*
*/
import java.util.Arrays;
// 1. copyOf()
// 2. copyOfRange()
// 3. equals()
Arrays.fill(filledArray, 7);
// 5. hashCode()
// 6. parallelSort()
Arrays.parallelSort(parallelSortedArray);
// 7. sort()
Arrays.sort(sortedArray);
// 8. binarySearch()
// 9. toString()
Arrays.spliterator(numbers).forEachRemaining(System.out::print);
System.out.println();
Arrays.stream(numbers).forEach(System.out::print);
System.out.println();
Index of 5 in sortedArray: 2
import java.util.*;
import java.util.stream.Collectors;
// 1. addAll()
// 2. binarySearch()
// 4. clone() - Note: clone() is not in Collections class, it's a method from Object class
// 5. disjoint()
// 7. enumeration()
System.out.print("Enumeration: ");
while (enumeration.hasMoreElements()) {
System.out.print(enumeration.nextElement() + " ");
System.out.println();
// 8. fill()
Collections.fill(numbers, 0);
// 9. frequency()
// 10. nCopies()
// 11. reverse()
Collections.reverse(numbers);
// 12. rotate()
Collections.rotate(numbers, 2);
// 13. shuffle()
Collections.shuffle(numbers);
Collections.sort(numbers);
// 15. swap()
Collections.swap(numbers, 0, 1);
// 19. replaceAll()
synchronizedCollection.forEach(System.out::println);
synchronizedList.forEach(System.out::println);
synchronizedSet.forEach(System.out::println);
try {
unmodifiableCollection.add("NewElement");
} catch (UnsupportedOperationException e) {
try {
unmodifiableList.add("NewElement");
} catch (UnsupportedOperationException e) {
Collections.copy(dest, numbers);
Collections.sort(customSortedList, Comparator.reverseOrder());
Collections.fill(fillList, "filled");
Collections.reverse(reverseList);
Checked List: []
Empty List: []
Empty Map: {}
Empty Set: []
Enumeration: 5 2 9 1 5 6
Frequency of 0: 6
Max value: 5
Min value: 1
Synchronized Collection: 0
Synchronized Collection: 0
Synchronized Collection: 0
Synchronized Collection: 0
Synchronized Collection: 0
Synchronized Collection: 0
Synchronized List: 0
Synchronized List: 0
Synchronized List: 0
Synchronized List: 0
Synchronized List: 0
Synchronized List: 0
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
// Create a collection
// 1. add(E e)
collection.add("Apple");
collection.add("Banana");
collection.add("Cherry");
newFruits.add("Date");
newFruits.add("Elderberry");
collection.addAll(newFruits);
// 3. clear()
collection.clear();
// 4. contains(Object o)
collection.add("Fig");
// 5. containsAll(Collection<?> c)
checkCollection.add("Fig");
// 6. isEmpty()
collection.add("Grape");
collection.add("Honeydew");
while (iterator.hasNext()) {
System.out.println();
// 8. remove(Object o)
collection.remove("Grape");
// 9. removeAll(Collection<?> c)
removeCollection.add("Honeydew");
collection.removeAll(removeCollection);
// 10. retainAll(Collection<?> c)
retainCollection.add("Fig");
collection.retainAll(retainCollection);
// 12. toArray()
System.out.println();
// 13. toArray(T[] a)
array2 = collection.toArray(array2);
System.out.println();
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
// Create a list
list.add("Apple");
list.add("Banana");
list.add("Cherry");
moreFruits.add("Date");
moreFruits.add("Elderberry");
System.out.println("List after addAll(int index, Collection<? extends E> c): " + list);
// 3. get(int index)
// 4. indexOf(Object o)
// 5. lastIndexOf(Object o)
// 6. listIterator()
System.out.println();
// 7. listIterator(int index)
while (listIteratorAtIndex.hasNext()) {
System.out.println();
// 8. remove(int index)
List<String> subList = list.subList(1, 3); // Get sublist from index 1 to 2 (toIndex is exclusive)
}
List after add(int index, E element): [Apple, Blueberry, Banana, Cherry]
List after addAll(int index, Collection<? extends E> c): [Apple, Blueberry, Date, Elderberry, Banana, Cherry]
Index of 'Cherry': 5
List elements using listIterator(): Apple Blueberry Date Elderberry Banana Cherry
List after set(int index, E element): [Apple, Fig, Elderberry, Banana, Cherry]
import java.util.HashSet;
import java.util.Set;
import java.util.Iterator;
// Create a set
// 1. add(E e)
set.add("Apple");
set.add("Banana");
set.add("Cherry");
moreFruits.add("Date");
moreFruits.add("Elderberry");
set.addAll(moreFruits);
// 3. clear()
set.clear();
// 4. contains(Object o)
set.add("Fig");
// 5. containsAll(Collection<?> c)
checkSet.add("Fig");
// 6. isEmpty()
set.add("Grape");
set.add("Honeydew");
while (iterator.hasNext()) {
System.out.println();
// 8. remove(Object o)
set.remove("Grape");
// 9. removeAll(Collection<?> c)
removeSet.add("Honeydew");
set.removeAll(removeSet);
// 10. retainAll(Collection<?> c)
retainSet.add("Fig");
set.retainAll(retainSet);
// 12. toArray()
System.out.println();
// 13. toArray(T[] a)
array2 = set.toArray(array2);
System.out.println();
import java.util.LinkedList;
import java.util.Queue;
// Create a queue
// 1. add(E e)
queue.add("Apple");
queue.add("Banana");
queue.add("Cherry");
queue.offer("Date");
queue.offer("Elderberry");
// 3. remove()
// 4. poll()
// 5. peek()
// 6. element()
}
Queue after add: [Apple, Banana, Cherry]
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Collection;
import java.util.function.BiFunction;
import java.util.function.Function;
// Create a map
map.put("B", "Banana");
map.put("C", "Cherry");
additionalMap.put("D", "Date");
additionalMap.put("E", "Elderberry");
map.putAll(additionalMap);
// 3. remove(Object key)
// 4. clear()
map.clear();
// 5. containsKey(Object key)
map.put("F", "Fig");
// 6. containsValue(Object value)
// 8. isEmpty()
// 9. keySet()
// 10. values()
// 11. entrySet()
// 12. size()
map.put("G", "Grape");
map.compute("H", (key, value) -> (value == null) ? "Mango" : value + " and Mango");
// 18. merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)
Map after replace with oldValue check: {A=Apple, C=Cherry, D=Date, E=Elderberry, G=Grape}
Map after computeIfPresent: {A=Apple, C=Cherry, D=Date, E=Elderberry, G=Grape, H=Honeydew Melon}
Map after compute: {A=Apple, C=Cherry, D=Date, E=Elderberry, G=Grape, H=Honeydew Melon and Mango}
Map after merge: {A=Apple, C=Cherry, D=Date, E=Elderberry, G=Grape, H=Honeydew Melon and Mango Mixed}
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
// 1. toList
// 2. toSet
// 3. toMap
// 4. joining
.collect(Collectors.groupingBy(
TreeMap::new,
Collectors.toSet()
));
// 10. partitioningBy
// 12. counting
// 13. summarizingInt
// 14. summarizingLong
// 15. summarizingDouble
DoubleSummaryStatistics doubleStats =
words.stream().mapToDouble(String::length).collect(Collectors.summarizingDouble());
// 16. averagingInt
// 17. averagingLong
// 19. reducing
// 22. mapping
.collect(Collectors.mapping(String::length, Collectors.toList()));
// 23. flatMapping
list.add("Extra");
return list;
}));
.collect(Collectors.toMap(String::length, String::toUpperCase));
list.add("End");
return list.stream().map(String::toUpperCase).collect(Collectors.toList());
}));
.collect(Collectors.collectingAndThen(Collectors.toList(),
joining: applebananaapricotblueberryavocado
joining with delimiter: apple, banana, apricot, blueberry, avocado
joining with delimiter, prefix, and suffix: [apple, banana, apricot, blueberry, avocado]
counting: 5
averagingInt: 5.6
averagingLong: 5.6
averagingDouble: 5.6
reducing: applebananaapricotblueberryavocado
mapping: [5, 6, 7, 9, 7]
flatMapping: [APPLE, apple, BANANA, banana, APRICOT, apricot, BLUEBERRY, blueberry, AVOCADO, avocado]
groupingBy with mapping and downstream collector: {a=[APPLE, APRICOT, AVOCADO], b=[BANANA, BLUEBERRY]}
partitioningBy with mapping and downstream collector: {false=[APPLE, BANANA, APRICOT], true=[BLUEBERRY,
AVOCADO]}
flatMapping with mapping: [APPLE, apple, BANANA, banana, APRICOT, apricot, BLUEBERRY, blueberry, AVOCADO,
avocado]
collectingAndThen with complex transformation: [APPLE, BANANA, APRICOT, BLUEBERRY, AVOCADO, END]
combined collectors result: [apple, APPLE, banana, BANANA, apricot, APRICOT, blueberry, BLUEBERRY, avocado,
AVOCADO]
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
// 1. forEach
System.out.print("forEach: ");
System.out.println();
// 2. forEachOrdered
System.out.print("forEachOrdered: ");
System.out.println();
// 3. iterator
while (iterator.hasNext()) {
System.out.println();
// 4. spliterator
System.out.print("spliterator: ");
spliterator.forEachRemaining(System.out::print);
System.out.println();
// 5. close
stream.forEach(System.out::print);
} catch (Exception e) {
e.printStackTrace();
System.out.println();
// 6. filter
// 7. map
// 9. distinct
// 10. sorted
// 11. peek
// 12. limit
// 13. skip
// 14. reduce
Optional<String> reduced = words.stream().reduce((s1, s2) -> s1 + s2);
// 15. collect
// 16. min
// 17. max
// 18. count
// 19. anyMatch
// 20. allMatch
// 22. findFirst
// 23. findAny
// 24. toArray
// 25. boxed
// 26. flatMapToInt
// 27. flatMapToLong
// 28. flatMapToDouble
// 29. asDoubleStream
// 30. asLongStream
// 31. asIntStream
// 32. mapToInt
// 33. mapToLong
// 35. summaryStatistics
// 36. join
// 37. collectingAndThen
list.add("Extra");
return list;
}));
// 38. toList
// 39. toSet
spliterator: applebananaapricotblueberryavocado
map: [5, 6, 7, 9, 7]
flatMap: [apple, APPLE, banana, BANANA, apricot, APRICOT, blueberry, BLUEBERRY, avocado, AVOCADO]
peek: applebananaapricotblueberryavocado
reduce: applebananaapricotblueberryavocado
min: apple
max: blueberry
count: 5
anyMatch: true
allMatch: false
noneMatch: true
findFirst: apple
findAny: apple
boxed: [1, 2, 3]
flatMapToInt: [5, 6, 7, 9, 7]
flatMapToLong: [5, 6, 7, 9, 7]
asLongStream: [5, 6, 7, 9, 7]
asIntStream: [5, 6, 7, 9, 7]
mapToInt: [5, 6, 7, 9, 7]
mapToLong: [5, 6, 7, 9, 7]
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
// 1. empty
// 2. of
// 3. ofNullable
// 4. isPresent
// 5. ifPresent
// 6. ifPresentOrElse
ofOptional.ifPresentOrElse(
);
ofNullableOptional.ifPresentOrElse(
);
// 7. orElse
// 8. orElseGet
// 9. orElseThrow
try {
} catch (Exception e) {
}
// 10. map
// 11. flatMap
// 12. filter
// 13. or
// 14. toString
// 15. equals
// 17. get
try {
} catch (Exception e) {
try {
} catch (Exception e) {
empty: Optional.empty
of: Optional[Hello]
ifPresent: Hello
ifPresent (null)
ifPresentOrElse: Hello
map: Optional[5]
flatMap: Optional[HELLO]
filter: Optional[Hello]
equals: true
hashCode: 67369324
import java.util.function.*;
public class FunctionalInterfacesExample {
// Consumer<T>
consumer.accept("Hello, Consumer!");
// Function<T, R>
// Supplier<T>
// Predicate<T>
BiConsumer<String, Integer> biConsumer = (s, i) -> System.out.println("BiConsumer: " + s + ", " + i);
biConsumer.accept("Age", 30);
// BiFunction<T, U, R>
BiFunction<String, Integer, String> biFunction = (s, i) -> s + " is " + i + " years old";
// ObjIntConsumer<T>
objIntConsumer.accept("Score", 95);
// ObjLongConsumer<T>
objLongConsumer.accept("ID", 123456789L);
// ObjDoubleConsumer<T>
objDoubleConsumer.accept("Value", 9.99);
// IntConsumer
intConsumer.accept(42);
// LongConsumer
longConsumer.accept(123456789L);
// DoubleConsumer
doubleConsumer.accept(3.14);
// IntFunction<R>
// LongFunction<R>
// DoubleFunction<R>
// IntToDoubleFunction
// IntToLongFunction
// LongToDoubleFunction
// LongToIntFunction
// DoubleToIntFunction
// DoubleToLongFunction
// DoubleToDoubleFunction
// ToIntFunction<T>
// ToLongFunction<T>
// ToDoubleFunction<T>
ToDoubleFunction<String> toDoubleFunction = s -> s.length();
Function: Number: 10
Predicate: true
UnaryOperator: 10
BinaryOperator: 7
BiConsumer: Age, 30
ObjIntConsumer: Score, 95
IntConsumer: 42
LongConsumer: 123456789
DoubleConsumer: 3.14
IntToDoubleFunction: 15.0
IntToLongFunction: 10000
LongToDoubleFunction: 50.0
LongToIntFunction: 89
DoubleToIntFunction: 3
DoubleToLongFunction: 7
DoubleToDoubleFunction: 11.0
ToIntFunction: 5
ToLongFunction: 5
ToDoubleFunction: 5.0
// Byte
System.out.println("Byte methods:");
System.out.println();
// Short
System.out.println("Short methods:");
System.out.println();
// Integer
System.out.println();
// Long
System.out.println("Long methods:");
System.out.println();
// Float
System.out.println("Float methods:");
System.out.println();
// Double
System.out.println("Double methods:");
System.out.println();
// Character
System.out.println("Character methods:");
System.out.println();
// Boolean
System.out.println("Boolean methods:");
Byte methods:
byteValue(): 10
shortValue(): 10
intValue(): 10
longValue(): 10
floatValue(): 10.0
doubleValue(): 10.0
compareTo(Byte): 0
equals(Byte): true
hashCode(): 10
toString(): 10
parseByte("20"): 20
valueOf("20"): 20
valueOf(byte 20): 20
decode("0x14"): 20
Short methods:
shortValue(): 100
intValue(): 100
longValue(): 100
floatValue(): 100.0
doubleValue(): 100.0
compareTo(Short): 0
equals(Short): true
hashCode(): 100
toString(): 100
parseShort("200"): 200
valueOf("200"): 200
decode("0xC8"): 200
Integer methods:
intValue(): 1000
longValue(): 1000
floatValue(): 1000.0
doubleValue(): 1000.0
compareTo(Integer): 0
equals(Integer): true
hashCode(): 1000
toString(): 1000
parseInt("1000"): 1000
valueOf("1000"): 1000
decode("0x3E8"): 1000
Long methods:
longValue(): 100000
floatValue(): 100000.0
doubleValue(): 100000.0
compareTo(Long): 0
equals(Long): true
hashCode(): 100000
toString(): 100000
parseLong("100000"): 100000
valueOf("100000"): 100000
decode("0x186A0"): 100000
Float methods:
floatValue(): 3.14
doubleValue(): 3.14
compareTo(Float): 0
equals(Float): true
hashCode(): 1078523331
toString(): 3.14
parseFloat("3.14"): 3.14
valueOf("3.14"): 3.14
Double methods:
doubleValue(): 3.14159
compareTo(Double): 0
equals(Double): true
hashCode(): 1079255761
toString(): 3.14159
parseDouble("3.14159"): 3.14159
valueOf("3.14159"): 3.14159
Character methods:
charValue(): A
compareTo(Character): 0
equals(Character): true
hashCode(): 65
toString(): A
isLetter('A'): true
isDigit('5'): true
isLetterOrDigit('A'): true
digit('5', 10): 5
forDigit(5, 10): 5
Boolean methods:
booleanValue(): true
compareTo(Boolean): 1
equals(Boolean): true
hashCode(): 1231
toString(): true
parseBoolean("true"): true
valueOf("true"): true
// Sample values
System.out.println("Math methods:");
// abs
// acos
// atan
// atan2
// ceil
// copySign
// cos
// cosh
// exp
// expm1
// fma
// fmod
// hypot
// log
// log10
// log1p
// max
// nextAfter
// nextUp
// pow
// random
// round
// signum
// sin
System.out.println("sin(30 degrees): " + Math.sin(angle));
// sinh
// sqrt
// tan
// tanh
// toDegrees
// toRadians
// ulp
}
Math methods:
abs(int 10): 10
acos(0.5): 1.0471975511965979
asin(0.5): 0.5235987755982989
atan(1.0): 0.7853981633974483
ceil(15.7): 16.0
cosh(1.0): 1.5430806348152437
exp(1.0): 2.718281828459045
expm1(1.0): 1.718281828459045
floor(15.7): 15.0
log(10.0): 2.302585092994046
log10(100.0): 2.0
log1p(1.0): 0.6931471805599453
max(10, 20): 20
nextUp(15.7): 15.700000000000001
random(): 0.7128546987948636
round(15.7f): 16
round(15.7): 16
signum(-15.7): -1.0
sinh(1.0): 1.1752011936438014
sqrt(16.0): 4.0
tanh(1.0): 0.7615941559557649
toDegrees(π/6): 30.0
ulp(15.7): 1.421085471520234e-14
try {
System.out.println("Thread started.");
Thread.sleep(2000);
System.out.println("Thread yielding...");
Thread.yield();
if (Thread.currentThread().isInterrupted()) {
} else {
System.out.println("Thread was not interrupted.");
Thread.currentThread().interrupt();
if (Thread.currentThread().isInterrupted()) {
} catch (InterruptedException e) {
System.out.println("Thread ended.");
});
thread.setName("ExampleThread");
thread.setPriority(Thread.MAX_PRIORITY);
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
Thread Priority: 10
Thread started.
Thread Name: ExampleThread
Thread Priority: 10
Thread yielding...
Thread ended.
@Override
System.out.println("Thread is running.");
try {
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
}
Thread is running.