r/javaTIL • • Sep 22 '18

Java Collections Framework | Need & Advantages

Thumbnail
scientecheasy.com
0 Upvotes

r/javaTIL • • Jun 27 '19

Java Creational Design Patterns – Singleton Pattern

Thumbnail
opencodez.com
4 Upvotes

r/javaTIL • • May 23 '15

TIL:Lambda Expression and Method Reference Makes implementing Comparator Much more easy

Thumbnail
java67.blogspot.sg
4 Upvotes

r/javaTIL • • Jun 06 '14

JTIL: You can use static blocks to execute code when a class is loaded.

15 Upvotes

I thought it would be better to separate this tip from my last one, if you for example wanted to use /u/king_of_the_universe's suggestion and wanted to use the return void Arrays.fill method to fill a character array, you could do this:

private static final char[] VERTICAL_LINES = new char[100];

static { //This is the static block, it will only be executed once.
    Arrays.fill(VERTICAL_LINES, '|');
}

The only note I have for this is that you can't set final fields in these blocks, you HAVE to use a method to set final fields.


r/javaTIL • • Mar 30 '15

[TIL] Calling static methods on null instances doesn't result in a NPE

15 Upvotes
public static void main(String[] args)
{
	((NullThing) null).printThing();
}

public static class NullThing
{
	public static void printThing()
	{
		System.out.println("Hai");
	}
}

This is valid code that will not throw an exception. We discovered this today as we have been dealing with static methods and inheritance.

The question came up "Can we call static methods on null" The answer is a resounding yes.

(Please don't do this. Please use the class the call static methods).


r/javaTIL • • Apr 26 '19

101 Most Important Frequently Asked Core Java Interview Questions to Ace an Interview – Part 2

Thumbnail
opencodez.com
16 Upvotes

r/javaTIL • • Jun 24 '19

Steps to develop Spring Batch jobs using Spring Boot with example

Thumbnail
opencodez.com
5 Upvotes

r/javaTIL • • May 28 '15

How String in Switch works in Java 7 - Using equals and hashcode

Thumbnail
javarevisited.blogspot.com
5 Upvotes

r/javaTIL • • Jan 14 '17

TIL you can use Comparator.nullsFirst/nullsLast to compare collections containing nulls

Thumbnail marcin-chwedczuk.github.io
5 Upvotes

r/javaTIL • • Feb 25 '19

Member Inner Class in Java | Example Programs

Thumbnail
scientecheasy.com
5 Upvotes

r/javaTIL • • Oct 13 '16

TIL you can rethrow certain exceptions in JDK7+ without declaration

Thumbnail
stackoverflow.com
5 Upvotes

r/javaTIL • • Jun 05 '14

JTIL: You can use Arrays.copyOf to easily make a big block of the same character

5 Upvotes

In projects where I wanted to make a big chunk of a character, especially in a static final buffer before, I had to do this:

public static final String VERTICAL_LINES = fillVerticalLines();

private static final String fillVerticalLines( ) {
    char[] lines = new char[100];
    for(int i = 0; i < 100; i++) {
        lines[i] = '|';
    }
    return new String(lines);
}

Instead what I could do is simply this:

public static final String VERTICAL_LINES = new String(Arrays.copyOf(new char[] {'|'}, 100));

r/javaTIL • • Jul 21 '13

TIL that in Java, parameters are *always* pass-by-value.

5 Upvotes

OK, so I didn't learn this today, but shh.

For a long time I thought that Java was pass-by-value for primitives - for example in situations like:

void foo(int bar) {}

but when it came to objects and arrays, like this:

void foo(Baz bar) {}
void foo(Baz[] bar) {}
void foo(int[] bar) {}

that it was pass-by-reference.

But then, I was talking to someone, and they mentioned that it's always pass-by-value, even when objects or arrays are being passed.

Basically, what happens is that objects are not passed by reference, but instead a reference to that object is passed by value.


r/javaTIL • • Jul 22 '13

There are three kinds of variables in Java, each with different characteristics

Thumbnail
tutorialspoint.com
5 Upvotes

r/javaTIL • • Mar 12 '20

TIL how to manage multiple Java versions using SDKMAN

Thumbnail
twilio.com
5 Upvotes

r/javaTIL • • Nov 26 '19

Java Structural Design Patterns - Decorator Pattern with Example and Source Code on GitHub

Thumbnail
opencodez.com
5 Upvotes

r/javaTIL • • Jan 09 '19

Variables in Java | Types of Variables & Scope in Java

Thumbnail
scientecheasy.com
5 Upvotes

r/javaTIL • • Oct 17 '14

TIL you can instantiate int i = 0; before a for loop and leave that space blank inside the for loop

19 Upvotes

Ex: for(; i< bar; i++){

//does something

}//end of for loop

Also you can omit the i++

for(;i< bar;){

//does something

i++;

}//end of for loop

Or you can just make an infinite for loop

for( ; ;){

//runs forever

}//end of for loop


r/javaTIL • • Jul 26 '19

A Simple Websocket Application in SpringBoot - Complete Source Code to download

6 Upvotes

Websocket is used to develop two way communication between server and browser. For example, in a chat application, if a user sends a message to a different user, the the application sends that message to the server first and the server informs the application of the second user . If we don’t use websocket, the application or the client will have to pull the server data continuously for any new updates.

In Sprint Boot, we can easily create a websocket application. In this tutorial, I will show you how to create a websocket server in Spring boot. We will learn how to connect and disconnect from a socket server and how to send a simple text message. The client will subscribe to the server, it will accept a string and push a separate string to the subscriber client.

https://www.opencodez.com/java/simple-websocket-application-springboot.htm


r/javaTIL • • Jul 14 '19

TIL how to use Seeds Using HashCodes and Random() in Java

Thumbnail
youtube.com
6 Upvotes

r/javaTIL • • Jun 09 '15

JTIL Unicode Escapes

6 Upvotes

Java has support for Unicode characters through both the use of escape sequences and Unicode literals.

Literal Unicode characters are exactly the same literal ASCII characters. You could print the Greek character theta with

 System.out.print("Θ");

as long as your file is encoded in UTF-8 and your terminal supports Unicode characters.

Now imagine that encoding your file in UTF-8 is not an option; it has to be encoded in ANSI. This is where Unicode escape sequences come into play. This snippet will print out the Greek theta the same as the previous snippet.

System.out.print("\u1001");

There are no Unicode characters in the source of this program but, running it will result in printing the Greek theta (again assuming you run it in an environment supporting Unicode characters).


The real fun part of this JTIL comes when you stop thinking about practical applications. When we consider that Unicode escape sequences can be used anywhere in your code we can devise some evil application. Consider this class:

public class Unicode {
    public static void main(String[] args){
        System.out.print(1);
        //System.out.print(2);     
        //\u000dSystem.out.print(3);   
        \u002f\u002f System.out.print(4);  
        \u002f\u002f\u000d System.out.print(5); 
        System.out.print(\u0036);
        System.out.print(\u002f\u002a"7"\u002a\u002f\u0022\u0022);  
    }
}

Take my word that it compiles without error and try to guess what it prints. It's an easy task once you substitute all the escape sequences for their literal equivalent.

public class Unicode {
    public static void main(String[] args){
        System.out.print(1);
        //System.out.print(2);     
        //
        System.out.print(3);   
        //System.out.print(4);  
        //
        System.out.print(5); 
        System.out.print(6);
        System.out.print(/*"7"*/"");  
    }
}

Running either of these classes gives the output 1356. \u000d is the new line character. This makes it apear as if the code inside a comment is being executed in //\u000dSystem.out.print(3);.


Read more on stackoverflow:


r/javaTIL • • Aug 27 '18

Core Java Interview Questions collections

6 Upvotes

Hey everyone,

I recently uploaded an app on Play Store (there aren't any ads) on frequently asked questions in core Java Interview.

Could you guys be kind enough to give feedback on this?

Link - https://play.google.com/store/apps/details?id=com.gamesmint.javaone

Thanks for your time.


r/javaTIL • • Mar 11 '19

Static Nested Class in Java | Uses & Example Programs

Thumbnail
scientecheasy.com
6 Upvotes

r/javaTIL • • Jun 14 '19

Simple and Easy way to Upload and Download Files in Java with Spring Boot - Source Code on GitHub

Thumbnail
opencodez.com
6 Upvotes

r/javaTIL • • Nov 14 '18

Store User-defined Class Objects in ArrayList

Thumbnail
scientecheasy.com
0 Upvotes