Programming Articles - Page 2431 of 3363

Java Integer compareTo() method

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

8K+ 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

Alshifa Hasnain
Updated on 06-Jan-2025 19:37:54

3K+ Views

Integer compare() Method The compare() method in the Integer class is a part of the Java interface and is used to compare two integers. It provides a way to compare two Integer objects or primitive int values and determine their relative ordering. This method is particularly useful when sorting or working with collections that involve integer values. Syntax public static int compare(int x, int y);Where − x: The first integer to be compared. y: The second integer to be compared. Return Value The compare() method returns an integer value that ... Read More

Java Integer byteValue() method

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

239 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

229 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

858 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

550 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

Convert a Map to JSON using the Gson library in Java?

Aishwarya Naglot
Updated on 22-Apr-2025 15:54:04

4K+ Views

Converting a map to a JSON object is our task here. Let's see how to convert a map to a JSON object in Java using the Gson library. Converting a Map to JSON using the Gson library JSON is a simple and lightweight data interchange format. It stores data in key-value pairs. A map is also a collection of key-value pairs. So, we can easily convert a map to a JSON object. If you are not familiar with JSON, refer JSON. And if you want to know more about Map, refer Map. Gson is developed by Google. It is an ... Read More

Convert a list of objects to JSON using the Gson library in Java?

Aishwarya Naglot
Updated on 22-Apr-2025 15:58:08

8K+ Views

In this article, we will be discussing how to convert a list of objects to JSON using the Gson library in Java. JSON is an interchangeable format that is used for data exchange. Values in JSON are represented as key-value pairs. To know more about JSON, refer JSON. Java GSON Library: Converting a list of objects to JSON Gson is a third-party Java library developed by Google. It is used for converting Java objects to JSON and vice versa. In the Gson library, we can convert a list of objects to JSON using the toJson() method. We cannot use the ... Read More

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

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

513 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

Program for EMI Calculator in C program

Sunidhi Bansal
Updated on 20-Sep-2019 11:48:47

892 Views

Given with certain values the program will develop an EMI calculator to generate the needed output. EMI stands for Equated Monthly Installment. So this calculator will generate monthly EMI amount for the user.ExampleInput-: principal = 2000    rate = 5    time = 4 Output-: Monthly EMI is= 46.058037The formula used in the below program is −EMI : (P*R*(1+R)T)/(((1+R)T)-1)where, P indicates loan amount or the Principal amount.R indicates interest rate per monthT indicates loan time period in yearApproach used below is as followsInput principal, rate of interest and time in float variableApply the formula to calculate the EMI amountPrint the ... Read More

Advertisements