- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 4338 Articles for Java 8
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
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
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
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
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
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
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
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
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
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