Found 9321 Articles for Object Oriented Programming

Java Program to perform an XOR on a set of Booleans

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

132 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

93 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

152 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

418 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

197 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

Boolean Type in Java

karthikeya Boyini
Updated on 26-Jun-2020 10:04:35

13K+ Views

To display Boolean type, firstly take two variables and declare them as boolean.boolean val1, val2;Then one by one assign values to both of them, one of them is shown below −val1 = true;Now, use if statement to check and display the Boolean true value.if(val1) System.out.println("This is true and will get displayed!");Let us now see the complete example to work with Boolean Type in Java.Example Live Demopublic class Demo {    public static void main(String[] args) {       boolean val1, val2;       System.out.println("Boolean Type in Java");       val1 = true;       if(val1)     ... Read More

Overflow of DataTypes in Java

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

838 Views

Overflow occurs when the given value is more than the maximum prescribed size of a data type. The overflow condition can result to an error or now the implementation of the programming language handles it on its own.To display overflow of datatypes, I have taken an example of float datatype. Float data type is a single-precision 32-bit IEEE 754 floating point.The range of a float datatype is −approximately ±3.40282347E+38FThe following program display overflow of datatypes in Java.Example Live Demopublic class Demo {    public static void main(String[] args) {       System.out.println("Displaying Overflow... ");       float val1 = ... Read More

Display the minimum and maximum value of primitive data types in Java

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

3K+ Views

Every data type in Java has a minimum as well as maximum range, for example, for Float.Min = 1.4E-45 Max = 3.4028235E38Let’s say for Float, if the value extends the maximum range displayed above, it leads to Overflow.However, if the value is less than the minimum range displayed above, it leads to Underflow.The following is the Java Program to display the minimum and maximum value of primitive data types.Example Live Demopublic class Demo {    public static void main(String[] args) {       System.out.println("Integer Datatype values...");       System.out.println("Min = " + Integer.MIN_VALUE);       System.out.println("Max = " ... Read More

Default value of primitive data types in Java

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

13K+ Views

Primitive datatypes are predefined by the language and named by a keyword in Java. Here is an example to display the default value of primitive data types.Example Live Demopublic class Demo {    static boolean val1;    static double val2;    static float val3;    static int val4;    static long val5;    static String val6;    public static void main(String[] args) {       System.out.println("Default values.....");       System.out.println("Val1 = " + val1);       System.out.println("Val2 = " + val2);       System.out.println("Val3 = " + val3);       System.out.println("Val4 = " + val4);   ... Read More

Advertisements