Found 9150 Articles for Object Oriented Programming

Reverse an Integer in Java

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

1K+ Views

To reverse an integer in Java, try the following code −Example Live Demoimport java.lang.*; public class Demo {    public static void main(String[] args) {       int i = 239, rev = 0;       System.out.println("Original: " + i);       while(i != 0) {          int digit = i % 10;          rev = rev * 10 + digit;          i /= 10;       }       System.out.println("Reversed: " + rev);    } }OutputOriginal: 239 Reversed: 932In the above program, we have the following int value, which we will reverse.int i = 239;Now, loop through until the value is 0. Find the remainder and perform the following operations to get the reverse of the given integer 239.while(i != 0) {    int digit = i % 10;    rev = rev * 10 + digit;    i /= 10; }

Compare Two Java int Arrays in Java

karthikeya Boyini
Updated on 26-Jun-2020 10:53:52

555 Views

To compare two Java short arrays, use the Arrays.equals() method.Let us first declare and initialize some int arrays.int[] arr1 = new int[] { 33, 66, 12, 56, 99 }; int[] arr2 = new int[] { 33, 66, 12, 56, 99 }; int[] arr3 = new int[] { 33, 66, 15, 56, 90 };Now let us compare any two of the above arrays.Arrays.equals(arr1, arr2));In the same way, work it for other arrays and compare themExample Live Demoimport java.util.*; public class Demo {    public static void main(String[] args) {       int[] arr1 = new int[] { 33, 66, 12, 56, ... Read More

Pass an integer by reference in Java

Samual Sam
Updated on 26-Jun-2020 10:54:21

2K+ Views

To pass an integer by reference in Java, try the following code −Example Live Demopublic class Demo {    public static void display(int[] arr) {       arr[0] = arr[0] + 50;;    }    public static void main(String[] args) {       int val = 50;       int[] myArr = { val };       display(myArr);       System.out.println(myArr[0]);    } }Output100In the above program, a custom method is created, which pass an int array.int val = 50; int[] myArr = { val }; display(myArr);In the method, we have performed mathematical operation on the value of array.public static void display(int[] arr) {    arr[0] = arr[0] + 50;; }The updated value is then displayed in the main.System.out.println(myArr[0]);

Parse and format to hexadecimal in Java

karthikeya Boyini
Updated on 26-Jun-2020 10:54:48

282 Views

To parse and format to hexadecimal in Java, we have used the BigInteger class. The java.math.BigInteger class provides operations analogues to all of Java's primitive integer operators and for all relevant methods from java.lang.Math.In the below example, we have taken BigInteger and created an object. With that, some values have been set by us in the arguments as the hexadecimal and the radix i.e. 16 for hexadecimal.BigInteger bi = new BigInteger("2ef", 16);Let us see an example.Example Live Demoimport java.math.*; public class Demo {    public static void main( String args[] ) {       BigInteger bi = new BigInteger("2ef", 16); ... Read More

Parse and format a number to decimal in Java

Samual Sam
Updated on 26-Jun-2020 10:55:20

455 Views

To parse and format a number to decimal, try the following code.Example Live Demopublic class Demo {    public static void main( String args[] ) {       int val = Integer.parseInt("5");       String str = Integer.toString(val);       System.out.println(str);    } }Output5In the above program, we have used the Integer.parseInt() and Integer.toString() method to convert a number to decimal.int val = Integer.parseInt("5"); String str = Integer.toString(val);The toString() method above represents a value in string and formats.

Java Program to wrap a Primitive DataType in a Wrapper Object

karthikeya Boyini
Updated on 26-Jun-2020 10:56:21

417 Views

Every Java primitive data type has a class dedicated to it. These classes wrap the primitive data type into an object of that class. Therefore, it is known as wrapper classes.The following is the program that displays a Primitive DataType in a Wrapper Object.Example Live Demopublic class Demo {    public static void main(String[] args) {       Boolean myBoolean = new Boolean(true);       boolean val1 = myBoolean.booleanValue();       System.out.println(val1);       Character myChar = new Character('a');       char val2 = myChar.charValue();       System.out.println(val2);       Short myShort ... Read More

Underflow of DataTypes in Java

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

292 Views

Underflow occurs when the given value is less than the maximum prescribed size of a data type. The underflow condition can result to an error or now the implementation of the programming language handles it on its own.To display underflow of datatypes, I have taken an example of double datatype. Double data type is a single-precision 64-bit IEEE 754 floating point.The following program display underflow of datatype in Java.Example Live Demopublic class Demo {    public static void main(String[] args) {       System.out.println("Displaying Underflow... ");       double val1 = 3.2187E-320;       System.out.println(val1/1000000);    } }OutputDisplaying ... Read More

Parse and format a number to octal in Java

karthikeya Boyini
Updated on 26-Jun-2020 10:46:33

739 Views

To parse and format a number to octal, try the following code −Example Live Demopublic class Demo {    public static void main( String args[] ) {       int val = Integer.parseInt("150", 8);       System.out.println(val);       String str = Integer.toString(val, 8);       System.out.println(str);    } }Output104 150In the above program, we have used the Integer.parseInt() and Integer.toString() method to convert a number to octal.int val = Integer.parseInt("150", 8); String str = Integer.toString(val, 8);The toString() method above represents a value in string and formats.We have set the radix as 8, since octal is represented by radix 8.

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

Samual Sam
Updated on 26-Jun-2020 10:47:23

239 Views

To convert an Integer to a boolean, we have taken the following Integer objects.Integer one = 1; Integer two = 1; Integer three = 0;We have taken 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 else-if works.else if (one.equals(two)) {    System.out.println(true); }The above display “true” and in this way we converted Integer to boolean.Let us now see the complete example to learn how to convert an Integer to Boolean.Example Live Demopublic class Demo {    public static void main(String[] args) {       ... Read More

Java Program to create a boolean variable from string

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

319 Views

Use the parseBoolean() method to create a boolean variable from a string. Here for boolean variable, firstly take a boolean and pass the string to it using Boolean.parseBoolean().boolean val1 = Boolean.parseBoolean("TRUE");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".Do the same for FALSE as well.boolean val2 = Boolean.parseBoolean("FALSE");Let us now see the complete example to form a boolean variable from string.Example Live Demopublic class Demo {    public static void main(String[] args) {       boolean val1 ... Read More

Advertisements