- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9326 Articles for Object Oriented Programming
21K+ Views
The maximum among three numbers can be found using an if else statement. A Java program that demonstrates this is given as follows.Example Live Demopublic class Example { public static void main(String args[]) { int num1 = 15; int num2 = -5; int num3 = 7; if (num1 >= num2 && num1 >= num3) System.out.println( num1 + " is the maximum number."); else if (num2 >= num1 && num2 >= num3) System.out.println( num2 + " is the ... Read More
26K+ Views
To check if a string is null or empty in Java, use the == operator.Let’s say we have the following strings.String myStr1 = "Jack Sparrow"; String myStr2 = "";Let us check both the strings now whether they are null or empty. Result will be a boolean.res = (myStr1 == null || myStr1.length() == 0); res = (myStr2 == null || myStr2.length() == 0); Example public class Demo { public static void main(String[] args) { String myStr1 = "Jack Sparrow"; String myStr2 = ""; boolean res; ... Read More
128 Views
Firstly, we have considered the following two dates.SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd"); Date d1 = s.parse("2018-10-15"); Date d2 = s.parse("2018-11-10");Now, use the compareTo() method to compare both the dates. The results are displayed on the basis of the return value.if (d1.compareTo(d2) > 0) { System.out.println("Date1 is after Date2!"); } else if (d1.compareTo(d2) < 0) { System.out.println("Date1 is before Date2!"); } else if (d1.compareTo(d2) == 0) { System.out.println("Date1 is equal to Date2!"); } else { System.out.println("How to get here?"); }Example Live Demoimport java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class ... Read More
175 Views
Let’s say we have the following zip code.String zipStr = "12345";Now, set the following regular expression to match zip codes in America.String reg = "^[0-9]{5}(?:-[0-9]{4})?$";Example Live Demopublic class Demo { public static void main(String[] args) { String zipStr = "12345"; // regular expression String reg = "^[0-9]{5}(?:-[0-9]{4})?$"; boolean res = zipStr.matches(reg); System.out.println("Is it a valid zip code in US? "+res); } }OutputIs it a valid zip code in US? True
2K+ Views
Firstly, let us consider a string with quotesString originalStr = "\"Demo Text\"";Now, consider the following logic for the beginning quote.if (originalStr.startsWith("\"")) { originalStr = originalStr.substring(1, originalStr.length()); }Now, consider the following logic for the ending quote.if (originalStr.endsWith("\"")) { originalStr = originalStr.substring(0, originalStr.length() - 1); }Example Live Demopublic class Demo { public static void main(String[] args) { String originalStr = "\"Demo Text\""; System.out.println("String with double quotes= "+originalStr); if (originalStr.startsWith("\"")) { originalStr = originalStr.substring(1, originalStr.length()); } if (originalStr.endsWith("\"")) { ... Read More
73 Views
To shift bits in a BigInteger, use the shiftLeft() or shiftRight() method.shiftLeft() methodThe java.math.BigInteger.shiftLeft(int n) returns a BigInteger whose value is (this > n). Sign extension is performed. The shift distance, n, may be negative, in which case this method performs a left shift. It computes floor(this / 2n).Example Live Demoimport java.math.*; public class Demo { public static void main(String[] args) { BigInteger one; one = new BigInteger("25"); one = one.shiftRight(3); System.out.println("Result: " +one); } }OutputResult: 3
102 Views
To flip a bit in a BigInteger in Java, use the flipBit() method. This method returns a BigInteger whose value is equivalent to this BigInteger with the designated bit flipped.Example Live Demoimport java.math.*; public class Demo { public static void main(String[] args) { BigInteger one, two; one = new BigInteger("7"); one = one.flipBit(3); System.out.println("Result: " +one); } }OutputResult: 15Let us see another example.Example Live Demoimport java.math.*; public class Demo { public static void main(String[] args) { BigInteger bi1, bi2; bi1 = ... Read More
189 Views
To clear a bit in a BigInteger in Java, use the clearBit() method. It returns a BigInteger whose value is equivalent to this BigInteger with the designated bit cleared.Example Live Demoimport java.math.*; public class BigIntegerDemo { public static void main(String[] args) { BigInteger one, two; one = new BigInteger("7"); two = one.clearBit(2); System.out.println("Result: " +two); } }OutputResult: 3Let us see another example.Example Live Demoimport java.math.*; public class Demo { public static void main(String[] args) { BigInteger bi1, bi2; bi1 = new ... Read More
138 Views
The setBit() method is used in Java to return a BigInteger whose value is equivalent to this BigInteger with the designated bit set.Example Live Demoimport java.math.*; public class BigIntegerDemo { public static void main(String[] args) { BigInteger one, two; one = new BigInteger("7"); two = one.setBit(3); System.out.println("Result: " +two); } }OutputResult: 15Let us see another example.Example Live Demoimport java.math.*; public class Demo { public static void main(String[] args) { BigInteger bi1, bi2; bi1 = new BigInteger("9"); // setbit ... Read More