Found 9326 Articles for Object Oriented Programming

Read integers from console in Java

Samual Sam
Updated on 26-Jun-2020 10:36:26

17K+ Views

To read integers from console, use Scanner class.Scanner myInput = new Scanner( System.in );Allow a use to add an integer using the nextInt() method.System.out.print( "Enter first integer: " ); int a = myInput.nextInt();In the same way, take another input in a new variable.System.out.print( "Enter second integer: " ); Int b = myInput.nextInt();Let us see the complete example.Exampleimport java.util.Scanner; public class Demo {    public static void main( String args[] ) {       Scanner myInput = new Scanner( System.in );       int a;       int b;       int sum;       System.out.print( ... Read More

Java Program to convert Boolean to String

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 you will display the values “str1” and “str2”, the output would be in String.Let us now see the complete example to convert Boolean to String.Example Live Demopublic class Demo {    public static void main(String[] args) {       boolean bool1 = false;       boolean bool2 = true; ... Read More

Java Program to convert String to Boolean

Samual Sam
Updated on 21-Jun-2024 13:09:42

15K+ Views

To convert String to Boolean, use the parseBoolean() method in Java. The parseBoolean() parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".Firstly, declare a string.String str = "false";Now, use the Boolean.parseBoolean() method to convert the above declared String to Boolean.boolean bool = Boolean.parseBoolean(str);Let us now see the complete example to learn how to convert String to Boolean.Example: Convert String to Boolean Live Demopublic class Demo {    public static void main(String[] args) {       // string     ... Read More

Create a Boolean object from Boolean value in Java

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 = new Boolean("false");The following is an example displaying how to create a Boolean object from Boolean value.Example Live Demopublic class Demo {    public static void main(String[] args) {       Boolean bool = new Boolean("true");       System.out.println(bool);       bool = new Boolean("false");       System.out.println(bool); ... Read More

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

59 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

88 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

84 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

576 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.

Advertisements