Found 4338 Articles for Java 8

Java Integer compareTo() method

AmitDiwan
Updated on 20-Sep-2019 11:05:01

7K+ Views

The java.lang.Integer.compareTo() method compares two Integer objects numerically. This method returns the value 0 if this Integer is equal to the argument Integer, a value less than 0 if this Integer is numerically less than the argument Integer and a value greater than 0 if this Integer is numerically greater than the argument Integer.At first, set two Integer objects −Integer obj1 = new Integer("100"); Integer obj2 = new Integer("200");Now, compare those object −int res = obj1.compareTo(obj2);Following is an example to implement the compareTo() method in Java −Examplepublic class Main {    public static void main(String[] args) {       Integer obj1 ... Read More

Java Integer compare() method

AmitDiwan
Updated on 20-Sep-2019 11:03:00

588 Views

The compare() method is used to compare two integer values numerically. The syntax is −int compare(int val1, int val2)  Above, a and b are the two integer values to be compared. If the return value is -1, therefore val1 is less than val2. Return value is 1, when val1 == val 2. The return value is 1, when val1 is greater than val2.At first, declare int values to be compared −int val1 = 200; int val2 = 250; int val3 = 200;Now, compare the values −System.out.println(Integer.compare(val1, val2)); System.out.println(Integer.compare(val1, val3));Following is an example to implement the compare() method in Java −Examplepublic ... Read More

Java Integer byteValue() method

AmitDiwan
Updated on 20-Sep-2019 11:01:11

159 Views

The byteValue() method returns the value of this Integer as a byte.Following is an example to implement the byteValue() method in Java −Examplepublic class Main {    public static void main(String[] args) {       Integer val = new Integer(10);       byte res = val.byteValue();       System.out.println("Value = " + res);    } }OutputValue = 10Let us see another example −Exampleimport java.util.*; public class Main {    public static void main(String[] args) {       Byte b = new Byte("10");       byte res = b.byteValue();       System.out.println("Byte = " + b );       System.out.println("Primitive byte = "+ res);    } }OutputByte = 80 Primitive byte = 80

Java Integer bitCount() method

AmitDiwan
Updated on 20-Sep-2019 10:58:54

123 Views

The java.lang.Integer.bitCount() method returns the number of one-bits in the two's complement binary representation of the specified int value.At first, set an int value −int val = 210;Now, find the number of one-bits −Integer.bitCount(val)Following is an example to implement the bitCount() method in Java −Examplepublic class Main {    public static void main(String[] args) {       int val = 210;       System.out.println("Number = " + val);       System.out.println("Binary = " + Integer.toBinaryString(val));       // returns the number of one-bits       System.out.println("Number of one bits = " + Integer.bitCount(val));    } }OutputNumber = 210 Binary = 11010010 Number of one bits = 4

Check if a value is present in an Array in Java

AmitDiwan
Updated on 20-Sep-2019 10:57:19

688 Views

At first sort the array −int intArr[] = {55, 20, 10, 60, 12, 90, 59}; // sorting array Arrays.sort(intArr);Now, set the value to be searched in an int variable −int searchVal = 12;Check for the presence of a value in an array −int retVal = Arrays.binarySearch(intArr, searchVal); boolean res = retVal > 0 ? true : false;Following is an example to check if a value is present in an array −Exampleimport java.util.Arrays; public class Main {    public static void main(String[] args) {       // initializing unsorted int array       int intArr[] = {55, 20, 10, ... Read More

Check if a String starts with any of the given prefixes in Java

AmitDiwan
Updated on 20-Sep-2019 10:54:48

396 Views

Let’s say the string is −String str = "Malyalam";Let’s say the prefixes are in an array −String[] prefixArr = { "Ga", "Ma", "yalam" };Now, to check whether the string starts with any of the abive prefix, use the startsWith() −if (Stream.of(prefixArr)    .anyMatch(str::startsWith))    System.out.println("TRUE"); else    System.out.println("FALSE");Following is an example to check is a string starts with any of the given prefixes −Exampleimport java.util.stream.Stream; class Main {    public static void main(String[] args) {       String str = "Malyalam";       String[] prefixArr = { "Ga", "Ma", "yalam" };       if (Stream.of(prefixArr)     ... Read More

Check if a string contains only alphabets in Java using ASCII values

AmitDiwan
Updated on 20-Sep-2019 08:29:26

388 Views

Let’s say we have set out inut string in myStr variable. Now loop through until the length of string and check for alphabets with ASCII values −for (int i = 0; i < myStr.length(); i++) {    char c = myStr.charAt(i);    if (!(c >= 'A' && c = 'a' && c = 'A' && c = 'a' && c

Check if a string contains only alphabets in Java using Regex

AmitDiwan
Updated on 20-Sep-2019 08:24:32

846 Views

At first, convert the string into character array. Here, name is our string −char[] ch = name.toCharArray();Now, loop through and find whether the string contains only alphabets or not. Here, we are checking for not equal to a letter for every character in the string −for (char c : ch) {    if(!Character.isLetter(c)) {       return false;    }Following is an example to check if a string contains only alphabets using RegexExample Live Demopublic class Main {    public static boolean checkAlphabet(String name) {       char[] ch = name.toCharArray();       for (char c : ch) { ... Read More

Check if a string contains only alphabets in Java using Lambda expression

AmitDiwan
Updated on 20-Sep-2019 08:19:04

597 Views

Let’s say our string is −String str = "Amit123";Now, using allMatch() method, get the boolean result whether the string has only alphabets or now −boolean result = str.chars().allMatch(Character::isLetter);Following is an example to check if a string contains only alphabets using Lambda Expressions −Exampleclass Main {    public static void main(String[] args) {       String str = "Amit123";       boolean result = str.chars().allMatch(Character::isLetter);       System.out.println("String contains only alphabets? = "+result);    } }OutputLet us see another example with a different input −String contains only alphabets? = falseExampleclass Main {    public static void main(String[] args) ... Read More

Insert a String into another String in Java

AmitDiwan
Updated on 20-Sep-2019 08:16:38

2K+ Views

Let’s say we have a string “That’s good!” and within that we need to insert the text “no”. Therefore, the resultant string should be “That’s no good!” −String str = "That's good!"; String newSub = "no ";Now, the index where the new sub string will get inserted −int index = 6;Insert the new substring now −StringBuffer resString = new StringBuffer(str); resString.insert(index + 1, newSub);Let us now see an example to insert a string into another −Example Live Demoimport java.lang.*; public class Main {    public static void main(String[] args) {       String str = "That's good!";       ... Read More

Advertisements