Java Articles

Page 357 of 450

What is the difference between non-static methods and abstract methods in Java?

radhakrishna
radhakrishna
Updated on 19-Dec-2019 2K+ Views

Following are the notable differences between non-static methods and abstract methods.Non-static (normal) methodsAbstract methodsThese methods contain a body.Abstract methods don’t have body these are ended with a semicolonYou can use normal method directly.You cannot use abstract methods directly, to use them you need to inherit them and provide body to these methods and use them.Example:public void display() {    System.out.println("Hi"); }Example:public void display();

Read More

How to make a MySQL Connection in JAVA? What is the port number to set on locahost?

AmitDiwan
AmitDiwan
Updated on 16-Dec-2019 513 Views

You need to use port number 3306 in the URL. The syntax is as follows −jdbc:mysql://localhost:3306Exampleimport java.sql.Connection; import java.sql.DriverManager; public class MySQLConnectionToJava {    public static void main(String[] args) {       String JDBCURL="jdbc:mysql://localhost:3306/sample?useSSL=false";       Connection con=null;       try {          con = DriverManager.getConnection(JDBCURL,"root","123456");         if(con!=null) {             System.out.println("MySQL connection is successful with port 3306.");           }     }     catch(Exception e) {       e.printStackTrace();     } } }OutputMySQL connection is successful with port 3306.

Read More

Can I learn Selenium without knowing Java?

Adiya Dua
Adiya Dua
Updated on 11-Dec-2019 692 Views

This Question comes to many professionals who are not actually into core technical and want to pursue their career in Selenium Automation. The term coding makes the non-programmers a bit scare to even start off with something like automation. There is a perception that a non-programmer cannot excel in Automation, but it is only in head. Many deserving and capable manual testers shy away from Selenium just thinking that it require some special skills.There are various many languages in which Selenium Scripts are designed such as Python, Ruby, C#, JavaScript and Java is one such of them. Knowing the popularity ...

Read More

How to write the comparator as a lambda expression in Java?

raja
raja
Updated on 06-Dec-2019 5K+ Views

A lambda expression is an anonymous method and doesn't execute on its own in java. Instead, it is used to implement a method defined by the functional interface. A lambda expression used with any functional interface and Comparator is a functional interface. The Comparator interface has used when sorting a collection of objects compared with each other.In the below example, we can sort the employee list by name using the Comparator interface.Exampleimport java.util.ArrayList; import java.util.Collections; import java.util.List; class Employee { int id; String name; double salary; public ...

Read More

What is the data type of a lambda expression in Java?

raja
raja
Updated on 02-Dec-2019 2K+ Views

The lambda expressions have a very simple, precise syntax and provide flexibility to specify the datatypes for the function parameters. Its return type is a parameter -> expression body to understand the syntax, we can divide it into three parts.Parameters : These are function method parameters and match with the signature of a function defined in the functional interface. Defining the data-type of parameters is optional but the number of parameters can match with the defined signatures in the interface.Expression Body : This is either a single statement or collection of statements that represent the function definition. Defining the data-type ...

Read More

How to remove non-ASCII characters from strings

Maruthi Krishna
Maruthi Krishna
Updated on 21-Nov-2019 2K+ Views

The Posix character class \p{ASCII} matches the ASCII characters and the meta character ^ acts as negation.i.e. The following expression matches all the non-ASCII characters."[^\p{ASCII}]"The replaceAll() method of the String class accepts a regular expression and a replacement-string and, replaces the characters of the current string (matching the given pattern) with the specified replacement-string.Therefore, You can remove the matched characters by replacing them with the empty string “, using the replaceAll() method.Example 1import java.util.Scanner; public class Exp {    public static void main( String args[] ) {       Scanner sc = new Scanner(System.in);       String regex = ...

Read More

How to match the regex meta characters in java as literal characters.

Maruthi Krishna
Maruthi Krishna
Updated on 21-Nov-2019 329 Views

The compile method of the patter class accepts two parameters −A string value representing the regular expression.An integer value a field of the Pattern class.The filed LITERAL of the enables literal parsing of the pattern. i.e. all the regular expression metacharacters and escape sequences don’t have any special meaning they are treated as literal characters. Therefore, If you need to match the regular expression metacharacters as normal characters you need to pass this as a flag value to the compile() method along with the regular expression.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main(String[] ...

Read More

How match a string irrespective of case using Java regex.

Maruthi Krishna
Maruthi Krishna
Updated on 21-Nov-2019 308 Views

The compile method of the patter class accepts two parameters −A string value representing the regular expression.An integer value a field of the Pattern class.This CASE_INSENSITIVE field of the Pattern class matches characters irrespective of case. Therefore, if you pass as flag value to the compile() method along with your regular expression, characters of both cases will be matched.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main( String args[] ) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input data: ");       String input = sc.nextLine(); ...

Read More

How to extract numbers from a string using regular expressions?

Maruthi Krishna
Maruthi Krishna
Updated on 21-Nov-2019 16K+ Views

You can match numbers in the given string using either of the following regular expressions −“\d+” Or, "([0-9]+)"Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ExtractingDigits {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter sample text: ");       String data = sc.nextLine();       //Regular expression to match digits in a string       String regex = "\d+";       //Creating a pattern object       Pattern pattern = Pattern.compile(regex);       //Creating a Matcher object       Matcher ...

Read More

How to replace multiple spaces in a string using a single space using Java regex?

Maruthi Krishna
Maruthi Krishna
Updated on 21-Nov-2019 13K+ Views

The metacharacter “\s” matches spaces and + indicates the occurrence of the spaces one or more times, therefore, the regular expression \S+ matches all the space characters (single or multiple). Therefore, to replace multiple spaces with a single space.Match the input string with the above regular expression and replace the results with single space “ ”.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ReplaceAllExample {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a String");       Scanner sc = new Scanner(System.in);       String input = ...

Read More
Showing 3561–3570 of 4,498 articles
« Prev 1 355 356 357 358 359 450 Next »
Advertisements