How to compare strings in Java?
String comparison in Java In Java, string comparison is used to compare two string values for equality or inequality. There are two main ways to compare strings in Java: using …
Read Morelearn to code by examples
String comparison in Java In Java, string comparison is used to compare two string values for equality or inequality. There are two main ways to compare strings in Java: using …
Read MoreSuppose you have the string ‘qwe’ and you need to repeat string ‘n’ times. Here’s how to do it with StringBuilder: Using StringBuilder and loop
1 2 3 4 5 6 7 8 |
int n = 3; String in = "qwe"; StringBuilder b = new StringBuilder(n * in.length()); for (int i = 0; i < n; i += 1) { b.append(in); } String newString = b.toString(); |
Imagine that you have implemented your java jar library and you need to connect it. Here are some examples of how to do it.
Read MoreSince Java 5 you can use Arrays.toString(arr) or Arrays.deepToString(arr) for arrays within arrays. Note that the Object[] version calls .toString() on each object in the array. The output is even …
Read MoreGoogle GSON library Let’s assume you have a class Book with just a title.
1 2 3 4 5 6 7 |
private class Book { public String title; public Book(String title) { this.title = title; } } |
Create maven project and add dependencies.
1 2 3 4 5 |
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency> |
Then write this code to transform JSON into class: …
Read MoreI decided to collect code style examples from the well-known company in the java community. There are both direct links to setting up the checkstyle and examples of setting the …
Read MoreToday we will continue the topic of generating office documents from the template. To generate a docx document, we use the apache poi library.
Read MoreSuppose you write a project that must have mandatory support for java 1.6 and higher, but you only have java 9 installed and also want the compiled classes to be …
Read MoreLet’s take a simple example. It’s easier, probably, and can not be – create a list of numbers and display it on the screen through the simplest cycle:
1 2 3 4 5 |
List <Integer> numbers = Arrays.asList (1, 2, 3, 4, 5, 6); for (int number: numbers) { System.out.println (number); } |