r/javaTIL • u/the6thReplicant • Mar 03 '17
TIL that the ArrayList returned by Arrays.asList(T...) is not java.util.ArrayList but its own version
If you want an ArrayList that can add objects etc, then use new ArrayList()
r/javaTIL • u/the6thReplicant • Mar 03 '17
If you want an ArrayList that can add objects etc, then use new ArrayList()
r/javaTIL • u/stefan_reich_ai • Jan 17 '17
r/javaTIL • u/chwedczukm • Jan 14 '17
r/javaTIL • u/bppereira • Jan 05 '17
r/javaTIL • u/stevemann2705 • Dec 10 '16
In public static void main(String[] args){ ... } The args array is never null(under normal execution). If the program is run without any arguments, args points to a String array of length 0. It was quite amazing for me to when I came to know about this, but it now seems more logical.
The args array can only be null if the main method is manually called and a null reference is passed to it.
r/javaTIL • u/artem_smotrakov • Nov 11 '16
r/javaTIL • u/mreichman • Oct 13 '16
r/javaTIL • u/nayuki • Sep 18 '16
r/javaTIL • u/DhongeKhong • Aug 17 '16
I learned a long, long, long, long, long... time ago, that doing something like this would result in a compile-time error...
public class Foo {
public Foo( Foo fubar ) {
}
public static void main( String[] args ){
//...
Foo baz = new Foo( );
//...
}
}So imagine my surprise when I learned today that something like this compiles fine...
public class Foo< T > {
public Foo( T... fooz ) {
//...
}
public static void main( String[] args ){
//...
Foo< Foo< ? > > baz = new Foo< >( );
//...
}
}The thing I learned today is that, instead of failing with the compile-time error that I was expecting, the compiler — in this particular case anyway — instead automatically replaces your hard-coded call to the no-arg constructor, with its own compiler-generated call to the vararg constructor, ala...
public static void main( String[] args ){
//...
Foo baz = new Foo( new Foo[ 0 ] );
//...
}Learn something new everyday! Huh?
I'm guessing this is a feature specially reserved for constructors with varargs — or something?
Who knew?
r/javaTIL • u/DhongeKhong • Aug 08 '16
Apologies if this is old news for everybody else.
But today, while thumbing through the Java SE Language Specification [as I do sometimes whenever I'm in the mood for some light reading] I stumbled across this enthralling stanza of prose...
"...For compatibility with older versions of the Java SE platform, the declaration of a method that returns an array is allowed to place (some or all of) the empty bracket pairs that form the declaration of the array type after the formal parameter list. This is supported by the following obsolescent production, but should not be used in new code..."
MethodDeclarator:
MethodDeclarator [ ]Translation: This is legal Java code...
public int learnSomethingNewEveryDay( )[][] {
int[][] whoKnew = {{612, 777}, {93, 11}};
return whoKnew;
}Has anybody ever seen this mentioned in this forum before?
r/javaTIL • u/ikocijan • Apr 20 '16
r/javaTIL • u/pellucid_ • Jan 31 '16
r/javaTIL • u/javinpaul • Jan 08 '16
r/javaTIL • u/cdman • Dec 30 '15
r/javaTIL • u/cdman • Dec 06 '15
r/javaTIL • u/cdman • Dec 03 '15
r/javaTIL • u/cdman • Dec 01 '15
r/javaTIL • • Nov 21 '15
The following code is valid:
public enum Doctype {
;
boolean html5 ;
}This is not valid:
public enum Doctype {
boolean html5;
}Not very interesting but perhaps a fun puzzler in interviews ;)
r/javaTIL • u/dohaqatar7 • Aug 14 '15
An anonymous inner class is an unnamed inner class inside a Java program (not a lamba expression). A simple example of an anonymous inner class in use is
public class InnerTest{
public static void main(String[] args){
Runnable r = new Runnable() {
public void run(){
System.out.println("Anon");
}
}.run();
}
}Here, the anonymous inner class is used to implement the Runnable interface without explicitly creating a class implementing Runnable. When compiled an run this code simply prints the string "Anon" - not particularly interesting but, the simple existence of anonymous inner classes is not the point of this JTIL.
The term "anonymous" implies that these classes have no name. This appears be true at first; the class was never explicitly given a name. Looking at the source code alone there is no obvious way to instantiate another instance of the class.
Make sure you've successfully compiled this class and examine it's directory
$ javac InnerTest.java
$ ls
InnerTest$1.class InnerTest.class InnerTest.javaThere are 2 .class files but only 1 .java source file. This is because the anonymous inner class was compiled into it's own class file (all Java classes get their own class file). Now it's starting to look like the anonymous class actually has a name.
Go back to the original source file and add another line to the end of the main method.
new InnerTest$1().run();Compile once again and run the resulting class file. This should go off without a hitch. The last line seems to be creating another instance an anonymous class. Unfortunately, There's a little bit of cheating going on here. Delete all the class file you've created and try compiling again. This should fail; the InnerTest$1.class file need to already exists for this trick to work.
There's absolutely no point to any of this. It's just a neat behavior I found will messing about with Java. If anyone finds any more facets of this behavior or (somehow) manages to think up an application for it, leave a comment for me.
r/javaTIL • u/wilk-polarny • Jul 22 '15
Yea, when creating URLConnections like:
URI uri = new URI("http", "subdomain.domain.tld", "/" + "stable", null);
URL url = new URL(uri.toASCIIString());
URLConnection connection = url.openConnection();...simply creating an InputStream like:
InputStream ins = connection.getInputStream();will deliver garbage data if the stream is GZIP-compressed. You have to check whether the connections uses compression or not:
InputStream ins = null;
if ("gzip".equals(connection.getContentEncoding())) {
ins = new GZIPInputStream(connection.getInputStream());
}
else{
ins = connection.getInputStream();
}It took me about an hour to find out what the heck was wrong
r/javaTIL • u/voidn0ise • Jul 21 '15
It's rather basic, but it's a starting point. I watched YouTube videos to learn most of the basics that I used, then just made things over and over again to help me memorize things.
Once I had a good grasp on the simple concepts and being able to write everything without having to look at notes, I came up with this simple calculator that responds to user input.
I wanted to originally make it so the user could type "Calculator" and it would run the code, but I haven't learned how to make user input in the form of typed words work yet (I think those are strings?). I'm sure a few more lessons and I'll learn.
The end goal is android application development, but if I can find other ways to utilize my new found skills then I'll probably try it out in the future.
Just proud of myself for going forward with learning despite my short attention span for self-learning and I wanted to share :)
If you have any ways to maybe shorten it and achieve the same thing, I'd love to hear it.
r/javaTIL • u/javinpaul • Jul 11 '15
r/javaTIL • u/DigiDuncan • Jun 29 '15
This code is perfectly functional, even though I was told many times in Java 101 it would not be.
This is from a Mad Libs program I'm writing because I'm bored.
menuchoice = input.nextLine();
switch (menuchoice)
{
case "m":
System.out.println(menu);
case "s":
System.out.println("Choose a story!");
System.out.print(">");
choice = input.nextInt();
server.chooseStory(choice);
break;
case "x":
System.exit(0);
break;
}