Karthikeya Boyini has Published 2383 Articles

Java Program to add integers and check for overflow

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 10:41:47

658 Views

To check for Integer overflow, we need to check the Integer.MAX_VALUE with the added integers result, Here, Integer.MAX_VALUE is the maximum value of an integer in Java.Let us see an example wherein integers are added and if the sum is more than the Integer.MAX_VALUE, then an exception is thrown.Example Live Demopublic ... Read More

Boolean Literals in Java

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 10:40:43

577 Views

The Boolean literals have two values i.e. True and False.The following is an example to display Boolean Literals.Example Live Demopublic class Demo {    public static void main(String[] args) {       System.out.println("Boolean Literals");       boolean one = true;       System.out.println(one);       one = ... Read More

Integer.signum() method in Java

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 10:39:47

90 Views

The method returns the signum function of the specified int value. Let us now see the syntax.int signum(int i)Here is the parameter.i − This is the int valueThe return value is -1 if the specified value is negative, 0 if the specified value is zero and 1 if the specified ... Read More

Convert Java Boolean Primitive to Boolean object

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 10:38:52

3K+ Views

To convert Boolean Primitive to Boolean object, use the valueOf() method in Java.Firstly, let us take a boolean primitive.boolean val = false;To convert it into an object, use the valueOf() method and set the argument as the boolean primitive.Boolean res = Boolean.valueOf(val);Let us see the complete example to learn how ... Read More

Create a Boolean object from Boolean value in Java

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 10:37:57

2K+ Views

To create a Boolean object from Boolean value is quite easy. Create an object with Boolean and set the Boolean value “true” or “false”, which are the Boolean literals.Let us now see how “true” value is added.Boolean bool = new Boolean("true");In the same way, “False” value is added.Boolean bool = ... Read More

Java Program to convert Boolean to String

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 10:37:05

10K+ Views

To convert Boolean to String in Java, use the toString() method. For this, firstly, we have declared two booleans.boolean bool1 = false; boolean bool2 = true;Now, convert Boolean to String using the toString() method in Java as shown below −String str1 = new Boolean(bool1).toString(); String str2 = new Boolean(bool2).toString();Now, when ... Read More

Convert Short to numeric primitive data types in Java

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 10:35:31

249 Views

To convert Short to numeric primitive data types, use the following methods −byteValue() shortValue() intValue() longValue() floatValue()Firstly, let us declare a Short.Short shortObj = new Short("40");Now, let us see how to convert it to long type, for a simple example.long val5 = shortObj.longValue(); System.out.println("Long: "+val5);In the same way, you can ... Read More

Java Program to convert string to byte

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 10:34:18

804 Views

Use the valueOf() method to convert string to byte. Firstly, let us take a string.String str = "65";Now, use the valueOf() method to convert string to byte.byte res = Byte.valueOf(str);Let us see the complete example.Example Live Demopublic class Demo {    public static void main(String[] args) {       String ... Read More

Java Program to display Bit manipulation in Integer

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 10:29:38

183 Views

Let’s say we have the following decimal number, which is binary 100100110.int dec = 294;To perform bit manipulation, let us count the number of 1 bits in it. We have used bitCount() method for this purpose.Integer.bitCount(dec);The following is an example to display bit manipulation in given Integer.Example Live Demopublic class Demo ... Read More

Integer.numberOfLeadingZeros() method in Java

karthikeya Boyini

karthikeya Boyini

Updated on 26-Jun-2020 10:28:42

72 Views

This Integer.numberOfLeadingZeros() method in Java returns the number of zero bits preceding the highest-order ("leftmost") one-bit in the two's complement binary representation of the specified int value.We have the following decimal as an example.int dec = 294;Calculated binary using Integer.toBinaryString() as shown below −Integer.toBinaryString(dec);Now let us see the implementation of ... Read More

Advertisements