Found 9321 Articles for Object Oriented Programming

Convert Java String Object to Boolean Object

Samual Sam
Updated on 26-Jun-2020 10:38:28

1K+ Views

A string object can be created in Java using the string literal.String myStr = “Amit”;Another way to create a string object is using the new keyword.String myStr = new String("Hello!");We have used the first method to create a string object.String str = "true";Now, use the valueOf() method to convert String Object to Boolean Object. We have used this method on Boolean object.Boolean bool = Boolean.valueOf(str);Let us now see the complete example to show how to convert String Object to Boolean Object.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "true";   ... Read More

Convert Java Boolean Primitive to Boolean object

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 to convert Boolean Primitive to Boolean object.Example Live Demopublic class Demo {    public static void main(String[] args) {       boolean val = false;       // converting to object       Boolean res = Boolean.valueOf(val);       System.out.println(res);    } }OutputFalse

Integer.reverseBytes() method in Java

Samual Sam
Updated on 26-Jun-2020 10:39:16

60 Views

The method returns the value obtained by reversing the order of the bytes in the two's complement representation of the specified int value. Let us now see the syntax.int reverseBytes(int i)The following is the parameter.i − This is the int valueExample Live Demopublic class Demo {    public static void main(String []args) {       System.out.println(Integer.reverseBytes(0));       System.out.println(Integer.reverseBytes(-87));       System.out.println(Integer.reverseBytes(98));    } }Output0 -1442840577 1644167168

Integer.signum() method in Java

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

91 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 value is positive.Let us now see an example.Example Live Demopublic class Demo {    public static void main(String []args) {       System.out.println("Returns = "+Integer.signum(0));       System.out.println("Returns = "+Integer.signum(-99));       System.out.println("Returns = "+Integer.signum(678));    } }OutputReturns = 0 Returns = -1 Returns = 1

Integer.rotateLeft() method in Java

Samual Sam
Updated on 26-Jun-2020 10:40:18

85 Views

The Integer.rotateLeft() method returns the value obtained by rotating the two's complement binary representation of the specified int value i left by the specified number of bits. The following is the syntax.int rotateLeft(int i, int distance)Here are the parameters.i − This is the int value.distance − This is the rotation distance.Example Live Demopublic class Demo {    public static void main(String []args) {       int val = 1;       for (int i = 0; i < 4; i++) {          val = Integer.rotateLeft(val, 1);          System.out.println(val);       }    } }Output2 4 8 16

Boolean Literals in Java

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

578 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 = false;       System.out.println(one);    } }OutputBoolean Literals true falseIn the above program, we have declared a boolean value and assigned the boolean literal “true”.boolean one = true;In the same way, we have added another boolean literal “false”.one = false;Both of the above values are displayed, which are the boolean literals.

Display default initial values of DataTypes in Java

Samual Sam
Updated on 26-Jun-2020 10:27:05

2K+ Views

To display default initial values of a datatype, you need to just declare a variable of the same datatype and display it.The following is a Java program to display initial values of DataTypes.Example Live Demopublic class Demo {    boolean t;    byte b;    short s;    int i;    long l;    float f;    double d;    void Display() {       System.out.println("boolean (Initial Value) = " + t);       System.out.println("byte (Initial Value) = " + b);       System.out.println("short (Initial Value) = " + s);       System.out.println("int (Initial Value) = " ... Read More

Display the limits of DataTypes in Java

karthikeya Boyini
Updated on 26-Jun-2020 10:27:50

527 Views

Every data type in Java has a minimum as well as maximum range, for example, for Integer.Minimum = -2147483648 Maximum = 2147483647Let’s say for Integer, if the value extends the maximum range display above, it leads to Overflow. However, if the value is less than the minimum range displayed above, it leads to Underflow.The following program displays the limits on datatypes in Java.Example Live Demopublic class Demo {    public static void main(String[] args) {       System.out.println("Limits of primitive DataTypes");       System.out.println("Byte Datatype values...");       System.out.println("Min = " + Byte.MIN_VALUE);       System.out.println("Max = ... Read More

Integer.numberOfTrailingZeros() method in Java

Samual Sam
Updated on 26-Jun-2020 10:28:15

93 Views

The Integer.numberOfTrailingZeros() method returns the number of zero bits following the lowest-order ("rightmost") one-bit in the two's complement binary representation of the specified int value.We have the following decimal as an example.int dec = 199;Calculated binary using Integer.toBinaryString() as shown below −Integer.toBinaryString(dec);Now let us see the implementation of Integer.numberOfTrailingZeros() method.Example Live Demopublic class Demo {    public static void main(String []args) {       int dec = 199;       System.out.println("Binary = " + Integer.toBinaryString(dec));       System.out.println("Count of one bits = " + Integer.bitCount(dec));       System.out.println("Number of trailing zeros : " + Integer.numberOfTrailingZeros(dec));    } ... Read More

Integer.numberOfLeadingZeros() method in Java

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

73 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 Integer.numberOfLeadingZeros() method.Example Live Demopublic class Demo {    public static void main(String []args) {       int dec = 294;       System.out.println("Decimal = " + dec);       System.out.println("Binary = " + Integer.toBinaryString(dec));       System.out.println("Count of one bits = " + Integer.bitCount(dec));       ... Read More

Advertisements