How to compare strings in Java?
String comparison in Java == performs a reference equality check, whether the 2 objects (strings in this case) refer to the same object in the memory. The equals() method will …
Read Morelearn to code by examples
In this category there are examples of code and articles on java.
Choose the section you need and start learning programming.
String comparison in Java == performs a reference equality check, whether the 2 objects (strings in this case) refer to the same object in the memory. The equals() method will …
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 MoreWhat most annoying programmers besides reading someone else’s code? That’s right, formatting! In a large team it is difficult to impart to everyone the same requirements for the style of …
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); } |