Found 9326 Articles for Object Oriented Programming

Display the maximum of three integer values in Java

karthikeya Boyini
Updated on 26-Jun-2020 10:08:31

161 Views

The following is an example displaying the maximum of three integer values.Example Live Demopublic class Demo {    public static void main(String[] args) {       int val1 = 10;       int val2 = 20;       int val3 = 30;       System.out.println("Number 1 = "+val1);       System.out.println("Number 2 = "+val2);       System.out.println("Number 3 = "+val3);       if (val2 > val1) {          val1 = val2;       }       if (val3 > val1) {          val1 = val3;   ... Read More

Display Minimum and Maximum values of datatype int in Java

Samual Sam
Updated on 26-Jun-2020 10:09:07

149 Views

To get the minimum value of datatype int in Java, use the following −Integer.MIN_VALUETo get the maximum value of datatype int in Java, use the following −Integer.MAX_VALUELet us now implement this in our example.Example Live Demopublic class Demo {    public static void main(String[] args) {       int val1 = 20;       int val2 = 3000;       System.out.println("Value1: "+val1);       System.out.println("Value2: "+val2);       System.out.println("Maximum value: "+Integer.MIN_VALUE);       System.out.println("Minimum value: "+Integer.MAX_VALUE);    } }OutputValue1: 20 Value2: 3000 Maximum value: -2147483648 Minimum value: 2147483647

Java Program to check for Integer overflow

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

5K+ Views

To check for Integer overflow, we need to check the Integer.MAX_VALUE, which 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 class Demo {    public static void main(String[] args) {       int val1 = 9898989;       int val2 = 6789054;       System.out.println("Value1: "+val1);       System.out.println("Value2: "+val2);       long sum = (long)val1 + (long)val2;       if (sum > Integer.MAX_VALUE) {          throw ... Read More

Java Program to convert from decimal to binary

Samual Sam
Updated on 18-Jun-2024 15:20:46

21K+ Views

To convert decimal to binary, Java has a method Integer.toBinaryString(). The method returns a string representation of the integer argument as an unsigned integer in base 2.Let us first declare and initialize an integer variable.int dec = 25;Convert it to binary.String bin = Integer.toBinaryString(dec);Now display the “bin” string, which consists of the Binary value. Here is the complete example.Example Live Demopublic class Demo {    public static void main( String args[] ) {       int dec = 25;       // converting to binary and representing it in a string       String bin = Integer.toBinaryString(dec);       System.out.println(bin);    } }Output11001

Java Program to perform an XOR on a set of Booleans

karthikeya Boyini
Updated on 26-Jun-2020 10:11:38

131 Views

To perform XOR on a set of Booleans, firstly let us consider the following Boolean array.boolean[] arr = { true, true, false };Let us now create a nested loop and within that perform XOR operation.for (boolean one : arr) {    for (boolean two: arr) {       // XOR       boolean res = one ^ two;    } }Here is the entire example to displayXOR on a set of Booleans.Example Live Demopublic class Demo {    public static void main(String[] args) {       // boolean array       boolean[] arr = { true, true, ... Read More

Java Program to convert an int to a boolean specifying the conversion values

Samual Sam
Updated on 26-Jun-2020 10:12:14

92 Views

To convert int to boolean, let us first take the following int.int one = 1; int two = 1; int three = 0;We have nested if-else statement to display the true or false values. Here, the value “one” is the same as “two” i.e. 1; therefore, the following works −else if (one.equals(two)) {    System.out.println(true); }The above display “true” and in this way we converted int to boolean.Let us now see the complete example to learn how to convert int to Boolean.Example Live Demopublic class Demo {    public static void main(String[] args) {       int one = 1; ... Read More

Java Program to convert integer to boolean

karthikeya Boyini
Updated on 26-Jun-2020 10:12:38

10K+ Views

To convert integer to boolean, firstly let us initialize an integer.int val = 100;Now we will declare a variable with primitive boolean. While declaration, we will initialize it with the val value comparing it with an integer using the == operator. If the value matches, the value “True” is returned, else “False” is returned.boolean bool = (val == 100);Let us now see the complete example displaying how to convert integer to boolean.Example Live Demopublic class Demo {    public static void main(String[] args) {       int val = 100;       System.out.println("Integer: "+val);       boolean bool ... Read More

Java Program to compare two Boolean Arrays

Samual Sam
Updated on 26-Jun-2020 10:03:06

151 Views

Use Arrays.equals() method to compare two Boolean Arrays. Let us first create some arrays to be compared. For using this method, you need to import the following package −import java.util.Arrays;We have 4 arrays and value “true” and “false” are assigned to them.boolean[] myArr1 = new boolean[] { true, false, true, true, true }; boolean[] myArr2 = new boolean[] { true, false, true, true , true }; boolean[] myArr3 = new boolean[] { false, false, true, true, true }; boolean[] myArr4 = new boolean[] { true, false, false, true , false };Now, let us compare Boolean arrays 1 and 2.Arrays.equals(myArr1, myArr2);In ... Read More

String representation of Boolean in Java

karthikeya Boyini
Updated on 26-Jun-2020 10:03:36

416 Views

To get the string representation of Boolean, use the toString() method.Firstly, we have used the valueOf() method for Boolean object and set a string value.Boolean val = Boolean.valueOf("false");The same is then represented using “toString() method.val.toString();Let us see the complete example that prints the string representation of Boolean (True and False) in Java.Example Live Demopublic class Demo {    public static void main(String[] args) {       // false       Boolean val = Boolean.valueOf("false");       System.out.println("Displaying the string representation of Boolean");       System.out.println(val.toString());       // true       val = Boolean.valueOf("true");   ... Read More

Parse a string to a Boolean object in Java

Samual Sam
Updated on 26-Jun-2020 10:04:00

195 Views

The valueOf() method returns the relevant Number Object holding the value of the argument passed. The argument can be a primitive data type, String, Boolean, etc. Therefore, to parse a string to a Boolean object, use the Java valueOf() method.The following is an example showing how to parse a string to a Boolean object in Java.Example Live Demopublic class Demo {    public static void main(String[] args) {       System.out.println("Parsing a string to a Boolean object...");       Boolean val = Boolean.valueOf("true");       System.out.println("Value: "+val);    } }OutputParsing a string to a Boolean object... Value: trueBoolean ... Read More

Advertisements