Found 9326 Articles for Object Oriented Programming

Difference between equals() vs equalsIgnoreCase() in Java

Samual Sam
Updated on 26-Jun-2020 14:56:37

4K+ Views

Use equals() in Java to check for equality between two strings.Use equalsIgnoreCase() in Java to check for equality between two strings ignoring the case.Let’s say the following are our two strings −String one = "qwerty"; String two = "Qwerty";Both are equal, but the case is different. Since the method ignores case, both of these strings would be considered equal using equalsIgnoreCase() method.Here, we are checking the same −if(one.equalsIgnoreCase(two)) {     System.out.println("String one is equal to two (ignoring the case) i.e. one==two"); }else{     System.out.println("String one is not equal to String two (ignoring the case) i.e. one!=two"); }However, under ... Read More

equals() vs == in Java

Deepti S
Updated on 29-Aug-2023 14:42:50

446 Views

There are two ways to determine if two objects are equal in Javal: the.equals() method and the == operator. The.equals() function compares the contents of two objects. The == operator compares the references of two objects. When you create an object with the new operator, it gets assigned a specified memory location in the heap. Tak, for example, two objects are having the same data. Even if the two objects kept in different sections of memory, the.equals() method will return true. The == operator returns true if two objects get stored in memory at the exact same location. Differences between ... Read More

Display the Operating System name in Java

karthikeya Boyini
Updated on 26-Jun-2020 15:01:44

3K+ Views

Use the System.getProperty() method in Java to get the Operating System name.It’s syntax is −String getProperty(String key)Above, the key is the name of the system property. Since, we want the OS name, therefore we will add the key as −os.nameExample Live Demopublic class Demo {     public static void main(String[] args) {        System.out.print("Operating System: ");        System.out.println(System.getProperty("os.name"));     } }OutputOperating System: Linux

Get the default system properties in Java

Samual Sam
Updated on 26-Jun-2020 15:04:28

562 Views

To return all the default systems properties in Java, firstly use the System.getProperties() method.java.util.Properties prop = System.getProperties();After that, use the list() method to list all of them.prop.list(System.out);Example Live Demopublic class Demo {     public static void main(String[] args) {        java.util.Properties prop = System.getProperties();        System.out.println("Here are the Properties:");        prop.list(System.out);     } }OutputHere are the Properties: java.runtime.name=OpenJDK Runtime Environment sun.boot.library.path=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0... java.vm.version=25.141-b16 java.vm.vendor=Oracle Corporation java.vendor.url=http://java.oracle.com/ path.separator=: java.vm.name=OpenJDK 64-Bit Server VM file.encoding.pkg=sun.io user.country=US sun.java.launcher=SUN_STANDARD sun.os.patch.level=unknown java.vm.specification.name=Java Virtual Machine Specification user.dir=/home/cg/root/3757524 java.runtime.version=1.8.0_141-b16 java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment java.endorsed.dirs=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0... os.arch=amd64 java.io.tmpdir=/tmp line.separator= java.vm.specification.vendor=Oracle Corporation os.name=Linux sun.jnu.encoding=UTF-8 java.library.path=/home/cg/root/GNUstep/Library/Librari... java.specification.name=Java Platform API Specification java.class.version=52.0 sun.management.compiler=HotSpot 64-Bit Tiered Compilers os.version=3.10.0-862.9.1.el7.x86_64 user.home=? user.timezone= java.awt.printerjob=sun.print.PSPrinterJob ... Read More

Return all system properties in Java

Samual Sam
Updated on 26-Jun-2020 14:43:51

1K+ Views

To return all the system properties in Java, firstly use the System.getProperties() method.java.util.Properties prop = System.getProperties();After that, use the list() method to list all of them.prop.list(System.out);Example Live Demopublic class MainClass {     public static void main(String[] args) {        java.util.Properties prop = System.getProperties();        prop.list(System.out);     } }Outputjava.runtime.name=OpenJDK Runtime Environment sun.boot.library.path=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0... java.vm.version=25.141-b16 java.vm.vendor=Oracle Corporation java.vendor.url=http://java.oracle.com/ path.separator=: java.vm.name=OpenJDK 64-Bit Server VM file.encoding.pkg=sun.io user.country=US sun.java.launcher=SUN_STANDARD sun.os.patch.level=unknown java.vm.specification.name=Java Virtual Machine Specification user.dir=/home/cg/root/3757524 java.runtime.version=1.8.0_141-b16 java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment java.endorsed.dirs=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0... os.arch=amd64 java.io.tmpdir=/tmp line.separator= java.vm.specification.vendor=Oracle Corporation os.name=Linux sun.jnu.encoding=UTF-8 java.library.path=/home/cg/root/GNUstep/Library/Librari... java.specification.name=Java Platform API Specification java.class.version=52.0 sun.management.compiler=HotSpot 64-Bit Tiered Compilers os.version=3.10.0-862.9.1.el7.x86_64 user.home=? user.timezone= java.awt.printerjob=sun.print.PSPrinterJob file.encoding=UTF-8 java.specification.version=1.8 user.name=? java.class.path=/home/cg/root/GNUstep/Library/Librari... java.vm.specification.version=1.8 sun.arch.data.model=64 java.home=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0... sun.java.command=MainClass java.specification.vendor=Oracle Corporation user.language=en ... Read More

Java Regex to extract maximum numeric value from a string

karthikeya Boyini
Updated on 26-Jun-2020 14:46:46

475 Views

The maximum numeric value is extracted from an alphanumeric string. An example of this is given as follows −String = abcd657efgh234 Maximum numeric value = 657A program that demonstrates this is given as follows −Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {  public static void main (String[] args) {     String str = "123abc874def235ijk999";     System.out.println("The string is: " + str);     String regex = "\d+";     Pattern ptrn = Pattern.compile(regex);     Matcher match = ptrn.matcher(str);     int maxNum = 0;     while(match.find()) {        int num = Integer.parseInt(match.group());        if(num > maxNum) {           maxNum = num;        }     }     System.out.println("The maximum numeric value in above string is: " + maxNum);  } }OutputThe string is: 123abc874def235ijk999 ... Read More

Java Program to get a character located at the String's specified index

Samual Sam
Updated on 26-Jun-2020 14:49:13

372 Views

Use the charAt() method in Java to get a character located at a specified index.Let’s say the following is our string.String str = "Laptop...";Finding character at 3rd position.str.charAt(2);The following is the final example.Example Live Demopublic class Demo {     public static void main(String[] args) {        String str = "Laptop...";        System.out.println("String: "+str);        System.out.println("Character at position 3 = " +str.charAt(2));     } }OutputString: Laptop... Character at position 3 = p

Java Program to compare string using compareTo() method

karthikeya Boyini
Updated on 26-Jun-2020 14:51:24

96 Views

The compareTo(obj) method compares this String to another Object.The value 0 is returned if the argument is a string lexicographically equal to this string; a value less than 0 if the argument is a string lexicographically greater than this string; and a value greater than 0 if the argument is a string lexicographically less than this string.We have the following two strings −String str1 = "tom"; String str2 = "tim";Let us check them for all the return values.if(str1.compareTo(str2) > 0) {  System.out.println("First string is greater!"); } if(str1.compareTo(str2) == 0) {  System.out.println("First string is equal to Second string!"); } if(str1.compareTo(str2)  0) {           System.out.println("First string ... Read More

Java Program to check for equality between two strings ignoring the case

Samual Sam
Updated on 26-Jun-2020 14:54:14

170 Views

Use equalsIgnoreCase() in Java to check for equality between two strings ignoring the case.Let’s say the following are our two strings.String one = "rocky"; String two = "Rocky";Both are equal, but the case is different. Since the method ignore case, both of these strings would be considered equal.Here, we are checking the same.if(one.equalsIgnoreCase(two)) {     System.out.println("String one is equal to two (ignoring the case) i.e. one==two"); }else{     System.out.println("String one is not equal to String two (ignoring the case) i.e. one!=two"); }The following is the complete example.Example Live Demopublic class Demo {     public static void main(String[] args) {        String one = "rocky";       ... Read More

Java Program to compare strings for equality

karthikeya Boyini
Updated on 26-Jun-2020 14:31:46

176 Views

To compare string for equality in Java, use the equals() method. Let us see some examples wherein we have checked for same as well as different string values.Example Live Demopublic class Demo {     public static void main(String[] args) {        String one = "x";        String two = "x";        if(one.equals(two)) {           System.out.println("String one is equal to two i.e. one==two");        }else{           System.out.println("String one is not equal to String two i.e. one!=two");        }     } }OutputString one is equal to two i.e. one==twoLet us see another example.Example Live Demopublic class Demo {     public static void main(String[] args) { ... Read More

Advertisements